What's changed: In-scope service coverage (axis B): added CodeArtifact/CloudShell (s1) and Amplify (s3) definitions, roles, and selection criteria.
4.2IaC: CloudFormation and SAM
Understand declarative infrastructure with CloudFormation (stacks, templates), the serverless shorthand AWS SAM, and the benefits of repeatable deployments.
Rather than building infrastructure by hand, Infrastructure as Code (IaC) codifies it as templates. On AWS, CloudFormation is the core, and SAM helps for serverless.
4.2.1CloudFormation and SAM
- CloudFormation: deploys a group of resources (stack) via declarative YAML/JSON templates.
- AWS SAM: serverless shorthand for Lambda/API Gateway/DynamoDB; transforms into CloudFormation under the hood.
- IaC enables repeatability, version control, and review, reducing environment drift.
A CloudFormation template has several sections: Parameters take inputs (e.g., environment name), Resources declare resources, Outputs expose stack outputs (e.g., endpoints), and Mappings/Conditions absorb environment differences. Before applying changes, a change set shows "what will be created/updated/deleted," preventing accidents. When manual changes make the live state diverge from the template, that’s drift, found via drift detection. SAM expresses serverless with short syntax like AWS::Serverless::Function and expands into ordinary CloudFormation at deploy time. With the SAM CLI you can run locally via sam local and ship via sam deploy, which uploads artifacts to S3 then updates the stack. A related tool, CDK, generates templates from general-purpose programming languages.
| Item | CloudFormation | SAM |
|---|---|---|
| Scope | Any AWS resource | Serverless-focused |
| Syntax | Verbose YAML/JSON | Concise shorthand |
| Execution | Deploy a stack | Transforms to CloudFormation |
Master the template building blocks too. Intrinsic functions reference values: Ref for a logical ID, Fn::GetAtt for an attribute, Fn::Sub to embed variables in a string, Fn::Join to concatenate. Region and account ID come from pseudo parameters (e.g., AWS::Region). Organize large setups with nested stacks (split templates) or cross-stack references (Export/ImportValue). To avoid deleting critical resources, protect them with a DeletionPolicy (e.g., Retain) or a stack policy. On a failed deploy, CloudFormation auto-rolls back by default, returning the stack to its prior stable state. SAM offers Globals for shared settings and policy templates to grant common permissions easily, keeping serverless templates even shorter.
Scenario: deploy Lambda + API Gateway + DynamoDB together. Template them with SAM shorthand for minimal lines. Before production, review the diff (resources added/removed) via a change set, then run sam deploy from the CI/CD pipeline for repeatable deployments. Monitor drift from manual changes via detection.
Q. CloudFormation vs. SAM? CloudFormation for general use; SAM to write serverless concisely (transforms to CloudFormation). Q. Apply changes safely? Preview diffs with a change set. Q. What is drift? Divergence between template and live state, found via detection.
Common on DVA: declaratively codify infra = CloudFormation (stacks), define serverless concisely = SAM (transforms to CloudFormation), preview diffs before applying = change set, template-vs-live divergence = drift. The SAM CLI supports local testing and deployment.
4.2.2Section summary
- CloudFormation = deploy stacks via declarative templates; preview diffs with a change set
- SAM = serverless shorthand (transforms into CloudFormation); catch divergence with drift detection
Sign in to track progress — Log in.
Quick check
(just a quick review)Q1. Which AWS service defines infrastructure as declarative templates deployed as a stack of resources?
Q2. Which concisely defines serverless apps (Lambda, API Gateway) and transforms into CloudFormation?
Q3. Which is the best benefit of managing infrastructure with IaC (templates)?
Keep track of your progress
The full study guide is free to read. Sign up free to practice with the question bank, track what you have read, review your mistakes, and highlight passages.

