Sub topic 12
Previewing updates with change sets
This chapter assumes you can write a template with parameters and resources, and that you have created and deleted a stack from the AWS CLI. It builds on the previous chapter, which explains how a property change causes no interruption, some interruption, or replacement.
Why preview an update
Running aws cloudformation deploy against an existing stack applies the change straight away. If a property change replaces a database or a bucket, you find out after it has happened. A change set is a preview. CloudFormation compares your new template and parameter values with the running stack and lists what it would do. Nothing changes until you execute the change set.
The change set workflow
# 1. Create the change set (nothing is modified yet)
aws cloudformation create-change-set \
--stack-name demo \
--change-set-name preview \
--template-body file://template.yaml \
--parameters ParameterKey=EnvName,ParameterValue=prod
# 2. Read the proposed changes
aws cloudformation describe-change-set \
--stack-name demo \
--change-set-name preview
# 3. Apply them, or discard them
aws cloudformation execute-change-set \
--stack-name demo \
--change-set-name preview
aws cloudformation delete-change-set \
--stack-name demo \
--change-set-name previewCreation is asynchronous. Poll describe-change-set until Status is CREATE_COMPLETE. If the template produces no differences, the status is FAILED and StatusReason says the submitted information contained no changes. That is a normal result, not an error in your template.
Reading the output
Each entry under Changes describes one resource. These fields matter most:
| Field | Meaning |
|---|---|
| `Action` | `Add`, `Modify`, `Remove`, `Import` or `Dynamic` |
| `LogicalResourceId` | The resource's name in your template |
| `Replacement` | `True`, `False` or `Conditional` |
| `Scope` | Whether the change touches `Properties`, `Tags` or other parts |
| `Details` | Which property changed and what triggered it |
Replacement: True means CloudFormation creates a new resource and then deletes the old one. The physical ID changes, and any data stored in the old resource is lost unless a DeletionPolicy or UpdateReplacePolicy protects it. Conditional means the outcome depends on the new value, so check the resource documentation.
Common mistakes
- Reusing a change set name on the same stack. Each name must be unique per stack, so delete the old one first.
- Creating a change set for a stack that does not exist yet. Pass
--change-set-type CREATEfor a new stack. The default isUPDATE. - Forgetting
--capabilities CAPABILITY_IAMwhen the template creates IAM resources. Creation fails without it. - Assuming a clean preview means a successful update. A change set cannot detect quota errors or invalid values that only surface during provisioning.
Exercise
You run describe-change-set and see a resource with Action Modify and Replacement True. It is an S3 bucket that holds production data and has no DeletionPolicy. What should you do before executing?
Show answer
Do not execute yet. Replacement means CloudFormation creates a new bucket and then deletes the old one, so the data would be lost. Add DeletionPolicy: Retain and UpdateReplacePolicy: Retain to the bucket and update the stack first. Then create a new change set, or change the property in a way that avoids replacement, and review again.