SAM CLI
By Amazon Web Services
The AWS Serverless Application Model CLI, known as SAM CLI, is a command-line tool for building, testing, and deploying serverless applications on AWS. It works with the SAM template specification, a simplified extension of CloudFormation…
Definition
The AWS Serverless Application Model CLI, known as SAM CLI, is a command-line tool for building, testing, and deploying serverless applications on AWS. It works with the SAM template specification, a simplified extension of CloudFormation syntax purpose-built for resources like Lambda functions, API Gateway endpoints, and DynamoDB tables, and it can emulate parts of the AWS Lambda execution environment locally so developers can invoke and debug functions without deploying them first.
Overview
Building a serverless application directly in raw CloudFormation means writing verbose resource definitions for every Lambda function, its execution role, its event source mappings, and the API Gateway routes that trigger it. SAM CLI exists to shorten that cycle: its template format introduces shorthand resource types such as `AWS::Serverless::Function` that expand into the full set of underlying CloudFormation resources at deploy time, and its CLI adds local development commands that CloudFormation alone does not provide. Mechanically, a SAM template is transformed by AWS's SAM transform macro into a standard CloudFormation template before it is deployed, so the deployment itself still goes through CloudFormation's stack engine, change sets, and rollback behavior. The CLI layer adds commands like `sam build`, which packages function code and dependencies into a deployable artifact, and `sam local invoke` or `sam local start-api`, which run a Lambda function inside a Docker container that mimics the AWS Lambda runtime, letting a developer test event handling and debug output before ever touching a live AWS account. SAM CLI sits closer to the metal than frameworks like the Serverless Framework, which is a third-party, provider-agnostic tool with a similar goal of simplifying serverless deployment but that generates CloudFormation independently rather than relying on AWS's own SAM transform. It is also distinct from the AWS CDK, which authors infrastructure in a general-purpose programming language and can express serverless and non-serverless resources alike, whereas SAM's template syntax is YAML- or JSON-based and specifically optimized for the serverless resource types AWS ships most often. Teams use SAM CLI throughout the serverless development lifecycle: scaffolding a new project from a template, iterating locally against emulated Lambda and API Gateway behavior, running `sam deploy` to package code and push a CloudFormation stack update, and using `sam sync` for faster iterative deployments during active development. It is common among teams that are AWS-committed and want official tooling with first-party support for local Lambda testing. Because SAM's local emulation approximates rather than perfectly replicates the real Lambda execution environment, some runtime-specific or IAM-permission-related issues only surface after an actual deployment. SAM is also scoped tightly to AWS serverless patterns, so a project that mixes serverless functions with a broader set of non-serverless infrastructure, or that needs multi-cloud portability, often ends up pairing SAM with plain CloudFormation, the CDK, or Terraform for the parts SAM does not cover. Teams evaluating SAM against a general infrastructure-as-code tool should weigh how much of their application is genuinely serverless; a project dominated by non-Lambda resources may get more value from CloudFormation or Terraform directly, reserving SAM for the specific functions that benefit from its local testing workflow.
Key Features
- Simplified template syntax expands into full CloudFormation resources at deploy time
- Local Lambda invocation and debugging via a Docker-based runtime emulator
- `sam local start-api` emulates API Gateway for end-to-end local testing
- `sam build` packages function code and dependencies into deployable artifacts
- `sam sync` speeds up iterative deployments during active development
- Deploys through the standard CloudFormation stack engine and rollback behavior
- Official AWS-maintained tooling with first-party Lambda support