AWS SAM

Also known as: AWS Serverless Application Model

On this page 6

AWS SAM, the AWS Serverless Application Model, is an open-source framework for defining, building, testing and deploying serverless applications on Amazon Web Services. It consists of two parts: a template specification that extends AWS CloudFormation with concise serverless resource types, and the SAM CLI, a command-line tool that packages code, emulates functions locally and runs deployments. In the Serverless topic of Cloud computing, SAM is one of the main ways to describe AWS Lambda functions and their event sources as code.

SAM does not replace CloudFormation. Every SAM template is transformed into a standard CloudFormation template during deployment, so stacks, change sets, rollbacks and drift detection all behave exactly as they do for any other CloudFormation stack.

How the AWS SAM transform works

A SAM template is a YAML or JSON file that begins with the line Transform: AWS::Serverless-2016-10-31. That declaration tells CloudFormation to run the SAM macro, which expands each short serverless resource into the full set of underlying resources. A single AWS::Serverless::Function with an API event, for example, becomes a Lambda function, an IAM execution role, an API Gateway REST API, a deployment, a stage and the permission that lets API Gateway invoke the function.

This expansion is the main value of SAM: a definition of roughly 20 lines can replace well over 100 lines of raw CloudFormation. The Globals section reduces repetition further by setting shared properties, such as runtime, memory size, timeout and environment variables, once for every function in the template.

The table lists the serverless resource types the specification provides and what each expands into.

Resource typeDefinesTypical generated resources
AWS::Serverless::FunctionA Lambda function and its triggersFunction, IAM role, event mappings
AWS::Serverless::ApiA REST API in API GatewayREST API, deployment, stage
AWS::Serverless::HttpApiA lower-cost HTTP APIHTTP API, stage, integrations
AWS::Serverless::SimpleTableA DynamoDB table with one keyDynamoDB table
AWS::Serverless::StateMachineA Step Functions workflowState machine, IAM role
AWS::Serverless::LayerVersionA shared Lambda layerLayer version
AWS::Serverless::ConnectorPermissions between two resourcesScoped IAM policies
AWS SAM resource types and their purpose

The SAM CLI development workflow

The SAM CLI turns the template into a repeatable workflow from a laptop to production. sam init creates a project from a template, sam build resolves dependencies and compiles code for the target runtime, and sam deploy uploads artifacts to Amazon S3 or Amazon ECR and creates or updates the CloudFormation stack. The --guided option stores answers in a samconfig.toml file so later deployments need no prompts.

Local testing relies on Docker containers that mimic the Lambda execution environment. These commands cover most of the daily loop and differ in how close they come to the real cloud.

CommandWhat it doesWhen to use it
sam buildInstalls dependencies, prepares artifactsBefore every local test or deploy
sam local invokeRuns one function once in DockerTesting a handler with a sample event
sam local start-apiServes API routes on localhostExercising HTTP endpoints
sam sync –watchPushes code changes to the cloud quicklyFast iteration in a development account
sam logsFetches CloudWatch logs for a functionDebugging deployed code
sam pipeline initGenerates CI/CD pipeline configurationSetting up multi-stage delivery
Main SAM CLI commands and when to use them

Local emulation is useful but not a perfect replica: IAM permissions, VPC networking, service quotas and managed event sources such as Amazon SQS behave differently or are absent locally. Teams therefore combine local tests with deployments to an isolated cloud account, which sam sync makes quick by updating function code without a full stack deployment.

AWS SAM compared with other serverless tooling

AWS SAM sits among several infrastructure-as-code options for serverless workloads, and the choice usually depends on language preference, multi-cloud needs and team habits. SAM is the most direct path for AWS-only teams that already accept CloudFormation.

ToolDefinition styleCloud scopeMain trade-off
AWS SAMDeclarative YAML or JSONAWS onlyConcise, but bound to CloudFormation limits
AWS CloudFormationDeclarative YAML or JSONAWS onlyFull control, very verbose
AWS CDKTypeScript, Python, Java, othersAWS onlyProgrammable, steeper abstraction
Serverless FrameworkYAML with pluginsSeveral providersLarge plugin ecosystem, licensing changes
TerraformHCLMany providersBroad scope, less serverless shorthand
AWS SAM and alternative frameworks for serverless deployments

The tools can also be combined. The SAM CLI can test functions defined with the AWS CDK by reading the synthesized template, and many organizations manage shared networking with Terraform while each service team ships its functions with SAM.

Practical guidance and common pitfalls

Good SAM projects keep one stack per deployable service and pass values between stacks through parameters or exports rather than building a single giant template. Policy templates such as DynamoDBCrudPolicy grant least-privilege permissions with one line, and should be preferred over broad managed policies like full administrator access.

  • Stack resource limit: CloudFormation allows 500 resources per stack, and SAM expansion consumes that budget faster than the template suggests.
  • Cold starts: SAM can enable provisioned concurrency or SnapStart, but these settings add cost and must be justified by latency targets.
  • Safe deployments: the DeploymentPreference property uses AWS CodeDeploy for canary or linear traffic shifting with automatic rollback on alarms.
  • Drift: changes made in the console bypass the template, so all edits should flow through version control and redeployment.

A frequent misunderstanding is that SAM is a runtime or hosting service. It is purely a definition and deployment tool; once deployed, the application runs on Lambda, API Gateway and other managed services, and SAM plays no part in request handling.

In everyday language

AWS SAM is a shorthand for describing small cloud programs and everything they need, such as web addresses, storage and permissions. Writing all of that out in full would take pages of settings. SAM lets developers write a short description, and Amazon expands it into the complete version automatically.

It also gives developers a tool to try their code on their own computer before sending it to the cloud. The key takeaway is that SAM saves time and reduces mistakes when building serverless applications on AWS.

Seen in the real world

A small team at a logistics company needs an API that receives delivery scans from handheld devices, stores them in DynamoDB and sends an alert when a parcel is marked as damaged. The team writes one SAM template with three Lambda functions, an HTTP API, a DynamoDB table and an SQS queue, using connectors to grant each function access only to the resources it touches.

Developers test handlers locally with sam local invoke and sample scan events, then use sam sync --watch against a development account. Production releases run through a pipeline generated by sam pipeline init, with a 10 percent canary for five minutes before full traffic shifts. When a faulty release raised the error rate, the CloudWatch alarm triggered an automatic rollback within minutes, and couriers never noticed the failed version.