📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials AWS Solutions Architect CloudFormation — Infrastructure as Code

CloudFormation — Infrastructure as Code

4 min read
Understand CloudFormation templates, stacks, StackSets, change sets, and drift detection. Know when to use CDK for complex infrastructure.

CloudFormation — Build Infrastructure with Code

CloudFormation lets you define your entire AWS infrastructure as YAML or JSON code. Instead of clicking through the console, you write a template and CloudFormation builds everything automatically — the same way, every time.

Teacher Note: Think of CloudFormation like IKEA instructions. Instead of buying furniture piece by piece and assembling randomly, you follow a complete instruction set (template) that builds the whole room identically every time. Build your dev environment on Monday, build an identical production environment on Friday — zero mistakes.

Why Infrastructure as Code?

  • Repeatability: deploy identical environments for dev, staging, and production
  • Version control: track every change in Git — see who changed what and roll back
  • Automation: deploy 50 resources in the correct order with one command
  • Documentation: the template IS the documentation of your infrastructure
  • Cost control: DELETE the whole stack when done — no orphaned resources accumulating charges

CloudFormation Key Concepts

ConceptDescriptionExample
TemplateYAML or JSON file defining resourcesVPC, EC2, RDS, ALB in one file
StackA deployment of a templatemy-webapp-production stack
StackSetsDeploy same stack across multiple accounts/regionsSecurity baseline for entire organization
Change SetsPreview changes before applyingShow me what will change before I update production
Drift DetectionFind manually changed resourcesWho modified the security group without using code?

AWS CDK — Code Instead of YAML

CDK (Cloud Development Kit) lets you write infrastructure in Python, TypeScript, or Java that compiles to CloudFormation. Much more powerful than raw YAML:

# CDK Python Example — Create an entire web tier
web_tier = ecs_patterns.ApplicationLoadBalancedFargateService(
    self, "WebService",
    cluster=cluster,
    memory_limit_mib=512,
    cpu=256,
    task_image_options=ecs_patterns.ApplicationLoadBalancedTaskImageOptions(
        image=ecs.ContainerImage.from_registry("nginx")
    )
)
# This ONE construct creates: ECS Service + ALB + Target Group
# + Security Groups + IAM Roles + CloudWatch Logs
# That would be 200+ lines of CloudFormation YAML!
Exam Tip: Change Sets are a critical exam topic. ALWAYS create a Change Set before updating a production CloudFormation stack. Change Sets show you exactly what will be created, modified, or DELETED before you make the change — preventing accidental resource deletions.