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 type | Defines | Typical generated resources |
|---|---|---|
| AWS::Serverless::Function | A Lambda function and its triggers | Function, IAM role, event mappings |
| AWS::Serverless::Api | A REST API in API Gateway | REST API, deployment, stage |
| AWS::Serverless::HttpApi | A lower-cost HTTP API | HTTP API, stage, integrations |
| AWS::Serverless::SimpleTable | A DynamoDB table with one key | DynamoDB table |
| AWS::Serverless::StateMachine | A Step Functions workflow | State machine, IAM role |
| AWS::Serverless::LayerVersion | A shared Lambda layer | Layer version |
| AWS::Serverless::Connector | Permissions between two resources | Scoped IAM policies |
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.
| Command | What it does | When to use it |
|---|---|---|
| sam build | Installs dependencies, prepares artifacts | Before every local test or deploy |
| sam local invoke | Runs one function once in Docker | Testing a handler with a sample event |
| sam local start-api | Serves API routes on localhost | Exercising HTTP endpoints |
| sam sync –watch | Pushes code changes to the cloud quickly | Fast iteration in a development account |
| sam logs | Fetches CloudWatch logs for a function | Debugging deployed code |
| sam pipeline init | Generates CI/CD pipeline configuration | Setting up multi-stage delivery |
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.
| Tool | Definition style | Cloud scope | Main trade-off |
|---|---|---|---|
| AWS SAM | Declarative YAML or JSON | AWS only | Concise, but bound to CloudFormation limits |
| AWS CloudFormation | Declarative YAML or JSON | AWS only | Full control, very verbose |
| AWS CDK | TypeScript, Python, Java, others | AWS only | Programmable, steeper abstraction |
| Serverless Framework | YAML with plugins | Several providers | Large plugin ecosystem, licensing changes |
| Terraform | HCL | Many providers | Broad scope, less serverless shorthand |
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
DeploymentPreferenceproperty 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.