Copyright (c) 2026 MindMesh Academy. All rights reserved. This content is proprietary and may not be reproduced or distributed without permission.

4.1.3. CloudFormation: Templates, Stacks, and StackSets

šŸ’” First Principle: CloudFormation treats infrastructure as a state machine. You declare the desired end state in a template, and CloudFormation figures out how to get there — creating, updating, or deleting resources in the right order, handling dependencies automatically. This is fundamentally different from scripting (where you tell it how to do it) — CloudFormation lets you describe what you want.

Without CloudFormation, provisioning a three-tier application means: create VPC, create subnets, create security groups, create RDS, create EC2, configure ELB — in the right order, with the right parameters. Make a mistake and you have to manually undo it. With CloudFormation, you declare all of that in one template, and CloudFormation handles creation, ordering, rollback on failure, and deletion.

Template Anatomy:
SectionRequired?Purpose
AWSTemplateFormatVersionNoAlways 2010-09-09
DescriptionNoHuman-readable description
ParametersNoInput values at deploy time
MappingsNoStatic lookup tables (e.g., AMI IDs by region)
ConditionsNoConditional resource creation
Resourcesāœ… YesThe actual AWS resources to create
OutputsNoValues to export or display after creation
TransformNoMacros, SAM transforms
Stack Operations:
OperationWhat Happens
CreateProvisions all resources; rolls back on failure
UpdateChanges are calculated via change set; existing resources updated, added, or deleted
DeleteAll resources in the stack are deleted (unless DeletionPolicy is set)
Detect DriftCompares actual resource configuration to template definition

Change Sets: Before executing an update, create a change set to preview exactly which resources will be added, modified, or deleted. This prevents surprise deletions — particularly critical for databases and stateful resources.

DeletionPolicy: Controls what happens to a resource when its stack is deleted or the resource is removed from the template:

ValueEffect
Delete (default)Resource is deleted
RetainResource is kept (orphaned from stack)
SnapshotA final snapshot is taken before deletion (RDS, EBS, ElastiCache)

CloudFormation Helper Scripts run inside EC2 instances to integrate instance initialization with stack creation:

ScriptPurpose
cfn-initReads AWS::CloudFormation::Init metadata and applies it (installs packages, writes files, runs commands)
cfn-signalSignals the stack that initialization succeeded or failed (used with CreationPolicy/WaitCondition)
cfn-hupDaemon that detects metadata changes and re-runs cfn-init on stack updates
cfn-get-metadataRetrieves resource metadata

CreationPolicy and WaitCondition: These tell CloudFormation to wait for a signal before marking a resource as successfully created. Without them, CloudFormation marks an EC2 instance as created the moment the API call succeeds — before the instance has actually finished its initialization script.

Resources:
  WebServer:
    Type: AWS::EC2::Instance
    CreationPolicy:
      ResourceSignal:
        Timeout: PT15M  # Wait up to 15 minutes for signal
        Count: 1
    Metadata:
      AWS::CloudFormation::Init:
        config:
          packages:
            yum:
              httpd: []
          commands:
            start_apache:
              command: "systemctl start httpd"

Drift Detection: Identifies configuration changes made outside CloudFormation (console changes, CLI changes, AWS Config remediations). Running drift detection shows you which resources have drifted and what changed. It does not automatically remediate drift — you must decide whether to update the template or revert the resource.

Nested Stacks and Cross-Stack References:
PatternUse Case
Nested StacksReuse common templates (VPC, security groups) across multiple parent stacks
Cross-Stack ReferencesExport output from Stack A (e.g., VPC ID) and import it in Stack B
StackSetsDeploy the same template to multiple accounts and/or regions simultaneously

StackSets are critical for multi-account deployments. From the management account (or a delegated admin account), you define a StackSet and target it at OUs or account lists. CloudFormation deploys the stack in every targeted account and region automatically.

āš ļø Exam Trap: CloudFormation cannot update a resource that requires replacement (e.g., changing an RDS instance's DB engine) without deleting and recreating it. Changing certain properties triggers replacement, which means downtime for stateful resources. Always check the "Update requires" field in the CloudFormation documentation before updating production stacks, and use DeletionPolicy: Snapshot to protect data.

Reflection Question: A CloudFormation stack creates an EC2 instance. The stack creation completes successfully in the console, but the application on the instance isn't working because the bootstrap script failed silently. What CloudFormation mechanism should have been used to catch this, and how does it work?

Alvin Varughese
Written byAlvin Varughese
Founder•15 professional certifications