Skip to main content
In this lesson we’ll explain immutable infrastructure patterns using AWS Launch Templates, Auto Scaling Groups (ASGs), and CloudFormation. You’ll learn how to bake a golden AMI (for example with EC2 Image Builder), deploy it with a versioned Launch Template, and safely replace instances using CloudFormation update policies or Auto Scaling instance refresh. We cover:
  • Launch Templates and minimal user-data for immutable AMIs
  • CloudFormation patterns: CreationPolicy, UpdatePolicy (AutoScalingRollingUpdate), and cfn-signal
  • ASG-native instance refresh vs CloudFormation-driven replacements
  • Recommended deployment strategies and exam tips

Immutable vs Mutable: quick comparison

EC2 Image Builder or other bake pipelines create the golden AMI you will reference in a Launch Template / ASG so every instance launched from that AMI has identical configuration.

Launch Templates

  • Use Launch Templates to describe instance configuration (the modern replacement for launch configurations).
  • Launch Templates can contain ImageId (AMI), InstanceType, IAM instance profile, security groups, monitoring, metadata options, and minimal user-data.
  • Versioning a Launch Template is how you tie a specific golden AMI to a deployment.
Screenshot of the AWS EC2 console showing a Launch Template details page for "devops-020-380-lt" (ID lt-06a29c7cda6f00cce). The template's version and instance details are visible, including an instance type of t3.micro.
Example minimal user-data for an immutable AMI
  • Keep user-data minimal: start services and signal instance health.
  • The application and configuration should be baked into the AMI.

CloudFormation template structure (high level)

Below are the key CloudFormation sections used in this demo to demonstrate immutable deployments via Launch Template versioning, Auto Scaling rolling updates, and instance signaling. Parameters (partial):
Launch Template (partial important bits):
Note: VersionDescription and ImageId are how you tie a specific golden AMI to a launch template version. Launch template versions persist; you can reference specific versions or use LatestVersionNumber. Auto Scaling Group (key bits)
  • The ASG references the Launch Template by LaunchTemplateId and Version (for example LatestVersionNumber).
  • UpdatePolicy with AutoScalingRollingUpdate controls how CloudFormation performs instance replacements during stack updates.
  • CreationPolicy with ResourceSignal instructs CloudFormation to wait for instances to signal healthy via cfn-signal.
Application Target Group / Listener (partial)

CloudFormation stack creation and signals

When creating/updating the stack, CloudFormation provisions resources. The CreationPolicy plus cfn-signal from user-data informs CloudFormation when instances are ready. If the stack shows CREATE_IN_PROGRESS, check the CloudFormation Events tab for resource signaling and health information.
A screenshot of the AWS CloudFormation console showing the Stacks list on the left and the details pane for the selected stack "devops-020-380-cfn-stack" on the right, currently marked CREATE_IN_PROGRESS. The page shows stack action buttons and tabs (Stack info, Events, Resources, Outputs) with overview and status fields.

Instance refresh and Auto Scaling native replacement

  • Auto Scaling’s built-in Instance Refresh lets you replace ASG instances (for example after updating the launch template version).
  • Instance refresh is independent of CloudFormation; it can be started from the ASG console, CLI, or SDKs.
  • If you prefer CloudFormation-driven replacements, update the Launch Template or ASG in a stack change set; CloudFormation will respect UpdatePolicy and CreationPolicy during updates.
Example: starting an instance refresh from the ASG console (shows success banner):
A screenshot of the AWS EC2 Auto Scaling Groups console on the "Instance refresh" tab, showing a green banner "Instance refresh started successfully" and details of an active instance refresh (ID, minimum healthy percentage 100%, rolling strategy). The page also shows control buttons like "Cancel instance refresh" and "Start rollback."
The ASG console also exposes simple controls for instance refresh on the group’s “Instance refresh” tab:
A screenshot of the AWS EC2 Auto Scaling groups console showing two auto scaling groups (devops-020-380-cfn-stack-asg and devops-020-380-asg), with the latter selected and set to 2 instances. The lower pane shows the "Instance refresh" tab with no active refresh and a "Start instance refresh" button.

Deployment strategies and exam tips

Use Launch Templates (not launch configurations). Combine CloudFormation UpdatePolicy (AutoScalingRollingUpdate), CreationPolicy with cfn-signal, and ASG Instance Refresh for smooth immutable workflows. For large-scale blue/green workflows, prefer AWS CodeDeploy (with CloudFormation integration) rather than manual ALB target switching in exam scenarios.

Key takeaways

  • Immutable infrastructure replaces instances with new AMIs instead of mutating running instances — this reduces configuration drift.
  • Bake application and configuration into a golden AMI (EC2 Image Builder is commonly used).
  • Use Launch Templates and their versioning to point deployments to a specific AMI version.
  • Use CloudFormation UpdatePolicy (AutoScalingRollingUpdate) together with CreationPolicy and cfn-signal to coordinate safe replacements during stack updates.
  • Use Auto Scaling Instance Refresh for ASG-native replacements, or trigger replacements via CloudFormation updates/change sets.
  • Consider AWS CodeDeploy for managed blue/green deployments; manual blue/green is possible (two ASGs + ALB target switching) but CodeDeploy is the recommended approach.
That’s it for this section on immutable infrastructure with CloudFormation.

Watch Video

Practice Lab