1. What is SAM?
AWS SAM is an open-source framework for building serverless applications. It extends CloudFormation with simplified syntax for defining Lambda functions, API Gateway APIs, DynamoDB tables, and event sources.
Core Concept SAM = simplified CloudFormation for serverless. Instead of writing 50+ lines of CF to define a Lambda function + API Gateway + IAM role + log group, SAM does it in ~10 lines. SAM templates are CloudFormation templates with a Transform header that enables the shorthand syntax. SAM also provides a CLI for local testing.
2. SAM Template
# SAM Template (template.yaml)
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: My Serverless API
Globals:
Function:
Timeout: 30
Runtime: nodejs18.x
MemorySize: 128
Resources:
GetOrdersFunction:
Type: AWS::Serverless::Function
Properties:
Handler: src/orders.getAll
CodeUri: ./src
Events:
GetOrders:
Type: Api
Properties:
Path: /orders
Method: get
CreateOrderFunction:
Type: AWS::Serverless::Function
Properties:
Handler: src/orders.create
CodeUri: ./src
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref OrdersTable
Events:
CreateOrder:
Type: Api
Properties:
Path: /orders
Method: post
OrdersTable:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: orderId
Type: StringWhat SAM Auto-Creates
- The above template automatically creates: 2 Lambda functions, API Gateway REST API with /orders routes, IAM execution roles for each Lambda, CloudWatch Log Groups, DynamoDB table
- In raw CloudFormation, this would be 200+ lines. In SAM, it’s ~40 lines.
3. SAM Resource Types
4 SAM Event Sources
- Api: API Gateway REST API trigger
- HttpApi: API Gateway HTTP API trigger
- Schedule: EventBridge scheduled event (cron/rate)
- S3: S3 bucket event notification
- SQS: SQS queue polling (event source mapping)
- SNS: SNS topic subscription
- Kinesis: Kinesis stream polling
- DynamoDB: DynamoDB Streams polling
- CloudWatchEvent: EventBridge rule trigger
- IoTRule: IoT Core rule trigger
5 SAM Policy Templates
SAM provides pre-built IAM policy templates for common patterns, so you don’t write IAM policies manually.
6. SAM CLI
SAM Local Testing sam local invoke and sam local start-api use Docker to simulate Lambda and API Gateway on your local machine. This enables testing serverless applications without deploying to AWS. Extremely useful for rapid development and debugging.
7. CloudFormation vs CDK vs SAM
- CDK and SAM can be used together: CDK can include SAM constructs, and sam can work with CDK apps
- All three ultimately use CloudFormation for provisioning
Exam Tip SAM: "Simplified serverless IaC" = SAM. "Transform: AWS::Serverless" = SAM template. "Local Lambda testing" = sam local invoke. "Local API testing" = sam local start-api. SAM auto-creates: Lambda + IAM role + API GW + log groups. Policy templates = pre-built IAM. SAM is a CloudFormation extension (not a replacement). "Any AWS resource IaC" = CloudFormation or CDK. "Serverless-specific IaC" = SAM.