Module Overview
Estimated Time: 3-4 hours | Difficulty: Intermediate | Prerequisites: Lambda, CloudFormation basics
- SAM template structure and syntax
- Local development and testing
- Deployment and CI/CD integration
- Common serverless patterns
- SAM CLI commands and workflows
Why SAM?
Simplified Syntax
Write less CloudFormation—SAM handles the boilerplate
Local Testing
Test Lambda functions and APIs locally before deployment
Best Practices Built-in
Automatic IAM policies, API Gateway configuration, and more
Portable
Deploy anywhere—SAM transforms to standard CloudFormation
SAM Template Structure
SAM Resource Types
AWS::Serverless::Function
AWS::Serverless::Api
AWS::Serverless::HttpApi
Other SAM Resources
SAM CLI Commands
Local Development
Deployment
Debugging and Monitoring
Project Structure
samconfig.toml
Policy Templates
SAM provides policy templates for common use cases:Testing
Unit Tests
Integration Tests
CI/CD Integration
GitHub Actions
Best Practices
Use Globals
Define common settings in Globals section to reduce repetition
Layer Dependencies
Put shared dependencies in Lambda Layers for faster deployments
Use Policy Templates
SAM policy templates follow least privilege by default
Local Testing
Always test locally before deploying—it’s faster and free
🎯 Interview Questions
Q1: SAM vs CDK vs Terraform?
Q1: SAM vs CDK vs Terraform?
SAM:
- Best for pure serverless applications (Lambda + API Gateway + DynamoDB)
- Simplified syntax, built-in best practices, least boilerplate
- Excellent local testing with
sam local - Limitation: Awkward when you mix serverless with traditional resources (VPCs, ECS)
- Programmatic (TypeScript, Python) — real programming constructs, loops, conditionals
- Better for complex infrastructure that mixes serverless and non-serverless
- Steeper learning curve but more flexible
- A senior engineer would say: “Use CDK when your SAM template starts needing CloudFormation escape hatches everywhere”
- Multi-cloud support — the only real option if you deploy to AWS AND GCP/Azure
- Established ecosystem with mature state management
- More verbose for serverless, but consistent across all infrastructure
- Common mistake: Choosing Terraform for a pure-serverless AWS project just because your team knows it — SAM or CDK will be 3x less code
Q2: How does SAM local invoke work?
Q2: How does SAM local invoke work?
SAM CLI:
- Builds Docker container matching Lambda runtime
- Mounts your code into container
- Invokes handler with event
- Returns response
Next Module
AWS GuardDuty
Intelligent threat detection for your AWS environment