2. AWS SAM — Serverless Application Model
AWS Serverless Application Model (SAM) is an open-source framework that simplifies building, testing, and deploying serverless applications on AWS. Built on top of AWS CloudFormation, SAM provides shorthand syntax specifically designed for serverless resources like Lambda functions, APIs, event mappings, and permissions.
Sam is fully compatible with standard CloudFormation templates. You can mix SAM shorthand syntax with native CloudFormation resources in the same template.
Prerequisites
Before diving into AWS SAM, ensure you have the following installed and configured:
- AWS CLI v2 configured with appropriate credentials
- Docker Desktop (required for local Lambda testing)
- Node.js 18+ or Python 3.9+ (depending on your runtime)
- AWS SAM CLI (installed via
brew install aws-sam-clior pip)
sam --version # Expected output: SAM CLI, version 1.105.0 aws --version
Initializing a Project
Start by scaffolding a new SAM project. The CLI provides several templates out of the box:
sam init --runtime nodejs18.x --name aevum-serverless-api --app-template hello-world
This creates a standardized directory structure:
aevum-serverless-api/
├── .aws-sam/
├── events/
│ └── event.json
├── hello_world/
│ ├── app.py
│ └── requirements.txt
├── samconfig.toml
├── template.yaml
└── tests/
└── unit/
└── test_handler.py
Understanding the Template
The template.yaml file is the heart of your SAM application. It defines resources, events, and permissions using simplified syntax:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Aevum API Gateway + Lambda example
Resources:
GetDataFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.9
Handler: app.lambda_handler
CodeUri: hello_world/
Events:
ApiEvent:
Type: Api
Properties:
Path: /data/{id}
Method: get
Policies:
- DynamoDBReadPolicy:
TableName: !Ref DataStore
DataStore:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: id
Type: String
Use AWS::Serverless::SimpleTable instead of raw AWS::DynamoDB::Table to automatically get a standard string primary key and pay-per-request billing.
Building & Testing Locally
SAM leverages Docker to emulate the Lambda execution environment locally. This ensures near-identical behavior to production:
# Build dependencies into container images sam build # Start local API Gateway emulator sam local start-api # Test endpoint curl http://localhost:3000/data/test123
The local emulator supports API Gateway v1 & v2, event mapping, and VPC networking simulation. Use --warm-containers EAGER to avoid cold start delays during rapid iteration.
Deploying to AWS
Once validated, deploy your application with the guided CLI command. It automatically creates or updates a CloudFormation stack, provisions IAM roles, and outputs your endpoint URLs:
sam deploy --guided
Never hardcode AWS credentials or secrets in your template. Use AWS Secrets Manager, Parameter Store, or environment variables injected at deploy time via --parameter-overrides.
Best Practices
- Use
Policiesshorthand: SAM's policy templates reduce IAM misconfigurations by following least-privilege defaults. - Enable X-Ray tracing: Add
Tracing: Activeto functions for distributed tracing without boilerplate. - Pin runtime versions: Avoid
latesttags in production. Usepython3.11,nodejs20.x, etc. - Separate environments: Use
samconfig.tomlprofiles for dev, staging, and production deployments. - Leverage layers: Package shared dependencies in Lambda Layers to reduce deployment package size and cold start times.
Next Steps
Now that you understand the core workflow of AWS SAM, proceed to the next module to learn how to containerize your functions and run advanced local simulations with Docker networking and VPC emulation.