AWS in 1 Min · Episode 174

AWS SAM explained: serverless templates and the SAM CLI

Less YAML, same CloudFormation. Watch the one-minute video, then read the details.

David Gomez1:18 video · 2 min read

"Serverless means no infrastructure code." It still needs some. A single Lambda function needs an execution role, permissions, a log group, often an API Gateway endpoint and a table. Written in raw CloudFormation, that is pages of YAML. AWS SAM, the Serverless Application Model, is an open-source framework that makes it a few lines.

The template: shorthand for CloudFormation

A SAM template is a CloudFormation template with one extra line at the top:

Transform: AWS::Serverless-2016-10-31

That line lets you use short resource types such as AWS::Serverless::Function, AWS::Serverless::Api and AWS::Serverless::SimpleTable. During deployment, SAM expands them into the full set of CloudFormation resources: the function, its role, its permissions, the API stage and so on. Because the result is plain CloudFormation, you can mix SAM resources with any normal CloudFormation resource in the same template.

Connectors go one step further: you state "this function writes to this table", and SAM generates the IAM permissions for that intent.

The SAM CLI

The command line covers the whole loop:

  • sam init creates a project from a template: code, a template.yaml and config.
  • sam build installs dependencies and packages the code.
  • sam local invoke and sam local start-api run functions and APIs on your machine in a container (Docker or a compatible runtime is required).
  • sam deploy --guided deploys through CloudFormation and saves your answers for next time.
  • sam sync pushes code changes to a development stack quickly while you work.

A common mistake is testing only locally. The local container does not reproduce IAM permissions or service quotas, so also test in a real development account.

Safe releases built in

Add AutoPublishAlias and a DeploymentPreference to a function, and SAM creates the CodeDeploy setup for you: types such as Canary10Percent10Minutes or Linear10PercentEvery1Minute, pre- and post-traffic test functions, and CloudWatch alarms that trigger an automatic rollback. The first deployment must create a version to shift from, so turn this on from the second release.

Where AI fits

SAM is a quick way to scaffold a Lambda plus Amazon Bedrock app: sam init gives you a function and an API, you add a policy statement that allows the Bedrock invoke actions on the model you use, and sam deploy gives you a working AI endpoint you can version and canary like any other release. AI coding assistants can do the scaffolding too: the open-source AWS Serverless MCP Server from AWS Labs gives assistants tools such as sam_init, sam_build, sam_deploy and sam_local_invoke. It runs read-only by default; deploying requires turning on write access explicitly, which is the right default when an assistant acts in your account.

Quick recap

  • SAM is CloudFormation shorthand for serverless: one Transform line, short resource types.
  • Build, test locally, deploy with the SAM CLI.
  • Canary releases built in through DeploymentPreference and CodeDeploy.
AWS in 1 Min #174 · 1:18

Need an AWS expert on your team?

I'm David, an AWS expert and fractional cloud consultant. I work alongside your team to run your cloud and infrastructure: architecture, security, cost and day-to-day operations. You get senior AWS experience when you need it, without the cost of a full-time hire.

  • Architecture reviews and a clear plan for what to fix first
  • Security hardening: IAM, networking, audits
  • Lower AWS bills without slowing your team down
  • Hands-on help with AI workloads on AWS