AWS open-source framework for building serverless applications with simplified CloudFormation syntax, CLI for local development, and integrated deployment.
AWS SAM (Serverless Application Model) is an open-source framework that simplifies defining, building, and deploying serverless applications. It extends AWS CloudFormation with high-level resources specifically designed for serverless architectures, providing more concise and expressive syntax for defining Lambda functions, APIs, databases, and event flows.
SAM transforms declarative YAML templates into complete CloudFormation stacks, automatically generating resources like IAM roles, security policies, and API Gateway configurations. The framework includes a robust CLI that enables local development, testing, and deployment with a workflow optimized for rapid iteration.
Unlike other Infrastructure as Code frameworks, SAM is specifically optimized for common serverless patterns, significantly reducing the boilerplate needed to configure event-driven applications and REST APIs.
A typical SAM template defines serverless resources with simplified syntax:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Runtime: nodejs20.x
Timeout: 30
Environment:
Variables:
NODE_ENV: production
Parameters:
Environment:
Type: String
Default: dev
AllowedValues: [dev, staging, prod]
Resources:
# REST API with multiple endpoints
UserApi:
Type: AWS::Serverless::Api
Properties:
StageName: !Ref Environment
Cors:
AllowMethods: "'GET,POST,PUT,DELETE'"
AllowHeaders: "'Content-Type,Authorization'"
AllowOrigin: "'*'"
# Lambda function with HTTP events
GetUsersFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/handlers/users/
Handler: get.handler
Events:
GetUsers:
Type: Api
Properties:
RestApiId: !Ref UserApi
Path: /users
Method: get
# Function with DynamoDB Stream event
ProcessUserChanges:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/handlers/streams/
Handler: userChanges.handler
Events:
UserStream:
Type: DynamoDB
Properties:
Stream: !GetAtt UsersTable.StreamArn
StartingPosition: TRIM_HORIZON
BatchSize: 10
# DynamoDB table with stream enabled
UsersTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub "${Environment}-users"
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
KeySchema:
- AttributeName: userId
KeyType: HASH
BillingMode: PAY_PER_REQUEST
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
Outputs:
ApiEndpoint:
Description: "API Gateway endpoint URL"
Value: !Sub "https://${UserApi}.execute-api.${AWS::Region}.amazonaws.com/${Environment}/"
Export:
Name: !Sub "${AWS::StackName}-ApiEndpoint"SAM CLI provides a complete toolset for the development lifecycle:
# Create project from template
sam init --runtime nodejs20.x --name my-serverless-app
# Generated structure
my-serverless-app/
├── template.yaml # Infrastructure definition
├── src/
│ └── handlers/ # Lambda function code
├── events/ # Test events
└── tests/ # Unit and integration tests# Build application
sam build
# Invoke specific function locally
sam local invoke GetUsersFunction --event events/get-users.json
# Start local API Gateway
sam local start-api --port 3000
# Start local Lambda for debugging
sam local start-lambda --debug-port 5858# Guided deployment (first time)
sam deploy --guided
# Quick deployment
sam deploy
# SAM Accelerate for rapid iteration
sam sync --watch --stack-name my-app-devSAM Accelerate enables real-time synchronization during development, reducing feedback time from minutes to seconds:
# Watch mode with automatic sync
sam sync --watch --stack-name my-app-dev
# Selective sync by resources
sam sync --code --stack-name my-app-dev
# Sync with hot-reload for specific function
sam sync --watch --code --resource-id GetUsersFunctionAccelerate detects code changes and updates only modified functions, avoiding complete CloudFormation stack redeployments.
| Criteria | SAM | CDK | Recommendation |
|---|---|---|---|
| Project complexity | Pure serverless apps | Mixed/complex infrastructure | SAM for simple APIs, CDK for hybrid architectures |
| Team experience | Ops/DevOps with YAML | Developers with TypeScript/Python | Align with existing expertise |
| Local development | Native sam local | Third-party tools | SAM for frequent local testing |
| Code reusability | YAML templates | Programmatic constructs | CDK for reusable components |
| Deployment time | Fast with Accelerate | Slower, large stacks | SAM for rapid iteration |
| Ecosystem | AWS serverless specific | All AWS + community | CDK for broad integration |
| Debugging | Limited to serverless | Full debugging | Based on debugging needs |
Resources:
AuthStack:
Type: AWS::Serverless::Application
Properties:
Location: ./auth/template.yaml
Parameters:
Environment: !Ref Environment
ApiStack:
Type: AWS::Serverless::Application
Properties:
Location: ./api/template.yaml
Parameters:
AuthStackName: !Ref AuthStackResources:
SharedLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: shared-dependencies
ContentUri: layers/shared/
CompatibleRuntimes:
- nodejs20.x
Metadata:
BuildMethod: nodejs20.x
MyFunction:
Type: AWS::Serverless::Function
Properties:
Layers:
- !Ref SharedLayerSAM represents the optimal balance between simplicity and power for teams building serverless applications on AWS. While pure CloudFormation requires hundreds of lines to configure a simple API, SAM significantly reduces boilerplate while maintaining full AWS ecosystem power.
For organizations adopting event-driven architectures, SAM eliminates local development friction — a critical problem in serverless where debugging traditionally requires costly deployments. The ability to run sam local start-api and have a complete local API replica in seconds fundamentally changes development velocity.
The framework also solves the "configuration explosion" problem typical in mature serverless projects, where each Lambda function requires specific IAM roles, security policies, and event configurations. SAM automatically generates these configurations following AWS best practices, reducing error surface and improving security posture.
Cloud computing model where the provider manages infrastructure automatically, allowing code execution without provisioning or managing servers, paying only for actual usage.
AWS serverless compute service that runs code in response to events without provisioning or managing servers, automatically scaling from zero to thousands of concurrent executions.
AWS native service for defining and provisioning infrastructure as code using YAML or JSON templates, with state management and automatic rollback.
Continuous Integration and Continuous Delivery/Deployment — practices that automate code integration, testing, and delivery to production. Foundation of modern software engineering.
Practice of defining and managing infrastructure through versioned configuration files instead of manual processes. Foundation of modern operations automation.
Personal lab for serverless architecture experiments: prototypes, patterns, and learnings about event-driven applications on AWS.