Choosing a Tool
| Tool | Style | Best for |
|---|---|---|
| CloudFormation | YAML/JSON declarative | The engine under the others. Direct control |
| AWS SAM | Shorthand YAML extension of CloudFormation focused on serverless, plus CLI | Lambda, API Gateway, DynamoDB, Step Functions apps with local testing |
| AWS CDK | Real programming languages (TypeScript, Python, Java, C#, Go) that synthesize to CloudFormation | Any AWS infrastructure with loops, functions, reuse and types |
| Terraform / OpenTofu | HCL, multi-cloud, own state | Teams standardizing across clouds |
AWS SAM
A SAM template starts with Transform: AWS::Serverless-2016-10-31. Short resource types expand into the full CloudFormation resources (function, role, API, event mappings, permissions).
| SAM resource | Creates |
|---|---|
AWS::Serverless::Function | Lambda function + role + event source mappings + permissions |
AWS::Serverless::Api / HttpApi | API Gateway REST / HTTP API |
AWS::Serverless::SimpleTable | DynamoDB table with a single key |
AWS::Serverless::StateMachine | Step Functions state machine |
AWS::Serverless::LayerVersion | Lambda layer |
AWS::Serverless::Application | Nested application from the Serverless Application Repository |
AWS::Serverless::Connector | Generates least-privilege permissions between two resources |
- **
Globals** set shared properties for all functions - Policy templates (
DynamoDBCrudPolicy,S3ReadPolicy,SQSPollerPolicy...) give least-privilege access in one line - **
AutoPublishAliaspublishes a version and moves an alias.DeploymentPreference** uses CodeDeploy to shift traffic gradually (Canary10Percent5Minutes,Linear10PercentEvery1Minute,AllAtOnce) with alarms and automatic rollback and optional pre/post-traffic hook functions
sam deploy do? It uploads artifacts to S3, creates a CloudFormation change set, then executes it. SAM is not a separate engine: you can see everything in CloudFormation.
AWS CDK
The Cloud Development Kit lets you define infrastructure with classes, loops, conditions and functions. cdk synth produces a CloudFormation template, and cdk deploy deploys it.
| Concept | Meaning |
|---|---|
| App | The program entry point. Contains stacks |
| Stack | A unit of deployment = one CloudFormation stack |
| Construct | A reusable cloud component |
**L1 (CfnBucket)** | Direct, 1:1 CloudFormation resources |
**L2 (s3.Bucket)** | Curated constructs with sensible defaults, helper methods (grantRead), and security defaults. Most commonly used |
| L3 (patterns) | Multi-resource solutions, for example ApplicationLoadBalancedFargateService |
| Context and Aspects | Environment data and cross-cutting rules (apply tags or checks to all resources) |
- Bootstrap is required before the first deployment in each account and Region
grant*methods write IAM policies for you. This is a big productivity and safety win- The CDK assertions library (
Template.from_stack) lets you unit test the generated template. Usecdk-nagfor security rule checks - CDK Pipelines builds self-mutating CI/CD pipelines
- Use **
cdk watchand hotswap** for fast development only
SAM vs CDK Quick Guide
| Choose SAM when | Choose CDK when |
|---|---|
| You build a mostly serverless app and want local invoke and API emulation, simple YAML | You want loops, abstractions, reuse and type checks, or you manage VPCs, ECS, EKS, pipelines and more |
| Your team likes declarative templates | Your team prefers writing code in TypeScript or Python |
They combine: CDK can include SAM constructs and use sam local against the synthesized template.
Practice Tasks
- Create a SAM app with
sam init, run it locally and deploy it - Add a canary deployment with an alarm and trigger a rollback by returning an error
- Use
sam sync --watchto update the function code quickly - Create the same stack in CDK, run
cdk diffafter a change - Write a CDK assertion test that checks the table has point-in-time recovery enabled
Conclusion
SAM gives short serverless templates, local testing and gradual deployments, then deploys through CloudFormation. CDK lets you define any AWS infrastructure in a programming language using L1, L2 and L3 constructs, needs bootstrapping, and also synthesizes to CloudFormation. Use change sets or cdk diff before production changes.

