5.4 CloudFormation and Infrastructure as Code

Key Takeaways

  • CloudFormation provisions AWS resources declaratively using JSON or YAML templates — it is the primary IaC tool in AWS.
  • Stacks are the unit of deployment; StackSets deploy stacks across multiple accounts and Regions simultaneously.
  • Change Sets preview changes before applying them, reducing the risk of unintended modifications to production resources.
  • Drift detection identifies resources that have been manually changed outside of CloudFormation.
  • AWS CDK (Cloud Development Kit) lets you define infrastructure using programming languages (TypeScript, Python, Java) and synthesizes CloudFormation templates.
Last updated: March 2026

CloudFormation and Infrastructure as Code

Quick Answer: CloudFormation = declarative IaC using YAML/JSON templates. Stacks = unit of deployment. StackSets = deploy across accounts/Regions. Change Sets = preview changes before applying. CDK = write IaC in programming languages, compiled to CloudFormation. SAM = simplified CloudFormation for serverless.

CloudFormation Overview

AWS CloudFormation lets you model and set up your AWS resources so you can spend less time managing resources and more time focusing on applications.

Template Anatomy

SectionRequiredDescription
AWSTemplateFormatVersionNoTemplate version (only valid value: "2010-09-09")
DescriptionNoTemplate description
ParametersNoInput values at stack creation time
MappingsNoStatic key-value lookups (e.g., AMI IDs by Region)
ConditionsNoControl resource creation based on parameters
ResourcesYesAWS resources to create (the only required section)
OutputsNoValues to export or display after creation

Key Concepts

ConceptDescription
StackA collection of AWS resources managed as a single unit
StackSetDeploy stacks to multiple accounts and Regions
Change SetPreview proposed changes before execution
Drift detectionDetect manual changes to managed resources
Nested stacksReusable template components (stack references another stack)
Cross-stack referencesExport values from one stack, import in another
RollbackAutomatic rollback on creation/update failure

CloudFormation vs. Terraform vs. CDK

FeatureCloudFormationTerraformCDK
LanguageYAML/JSONHCLTypeScript, Python, Java, C#, Go
ProviderAWS onlyMulti-cloudCompiles to CloudFormation
StateManaged by AWSExternal state fileCloudFormation (via cfn)
DriftBuilt-in detectionRefresh commandCloudFormation drift
Best forAWS-native IaCMulti-cloudDevelopers who prefer programming languages

AWS SAM (Serverless Application Model)

SAM is an extension of CloudFormation for serverless applications:

  • Simplified syntax for Lambda, API Gateway, DynamoDB, Step Functions
  • Local testing and debugging via SAM CLI
  • Compiles to standard CloudFormation

On the Exam: "Deploy identical infrastructure across 15 AWS accounts in 3 Regions" → CloudFormation StackSets. "Preview infrastructure changes before applying" → Change Sets. "Detect manual changes to managed resources" → Drift Detection.

Test Your Knowledge

A company needs to deploy the same CloudFormation stack to 20 AWS accounts across 4 Regions. What should they use?

A
B
C
D
Test Your Knowledge

Before updating a production CloudFormation stack, a DevOps engineer wants to see exactly what changes will be made. What should they use?

A
B
C
D