Sub topic 13
Previewing updates with change sets
This chapter assumes you can deploy a stack from the CLI and read a template with Parameters, Conditions and Mappings. It also assumes you know that a property change can update a resource in place, interrupt it, or replace it. Here you will learn to see which of these will happen before you commit to an update.
What a change set is
A change set is a saved comparison between a stack's current state and a proposed template or set of parameter values. CloudFormation computes the difference and stores it without touching any resource. You read it, then either execute it or delete it.
Running update-stack directly applies the change immediately. A change set adds a review step, and you can keep several candidate change sets for the same stack.
Creating and reading one
Using the environment-aware bucket from earlier, this command previews switching from dev to prod:
aws cloudformation create-change-set \
--stack-name demo \
--change-set-name preview \
--template-body file://template.yaml \
--parameters ParameterKey=EnvName,ParameterValue=prod
aws cloudformation wait change-set-create-complete \
--stack-name demo \
--change-set-name preview
aws cloudformation describe-change-set \
--stack-name demo \
--change-set-name preview \
--query "Changes[].ResourceChange.{Action:Action,Id:LogicalResourceId,Replace:Replacement,Details:Details[].Target.Name}"The Changes list holds one ResourceChange per affected resource. These are the fields to check:
| Field | Meaning |
|---|---|
| `Action` | `Add`, `Modify`, `Remove`, `Import` or `Dynamic`. |
| `LogicalResourceId` | The template name of the resource. |
| `Replacement` | `True`, `False` or `Conditional`. `True` means the resource is recreated with a new physical ID. |
| `Scope` | What changed: `Properties`, `Metadata`, `Tags`, `CreationPolicy`, `UpdatePolicy` or `DeletionPolicy`. |
| `Details` | Which property changed, and what caused it. |
For the dev-to-prod switch you should see Modify on AppBucket with Replacement: False. The versioning status, lifecycle expiration and Name tag all change in place. If you had also edited an immutable property such as BucketName, Replacement would be True. The old bucket would then be deleted after the new one exists, unless UpdateReplacePolicy says otherwise.
Conditional replacement and dynamic evaluation
Conditional means whether the resource is replaced depends on the new value. For example, changing an EC2 instance's InstanceType can be a no-interruption change or a stop and start, depending on the type.
Each Details entry also has an Evaluation of Static or Dynamic. A static change is fully known from the template. A dynamic change depends on a value that exists only at execution time, such as a Ref to a resource that will be replaced or a value returned by a custom resource. Treat dynamic entries as a warning that the preview is incomplete.
Executing, discarding and creating new stacks
Execute the change set to apply exactly what you reviewed:
aws cloudformation execute-change-set \
--stack-name demo \
--change-set-name preview
aws cloudformation delete-change-set \
--stack-name demo \
--change-set-name previewRun only one of those two commands for a given change set. After you execute a change set, the stack goes through UPDATE_IN_PROGRESS like any other update. Other change sets for that stack stay in the list but are stale, because they were computed against the old state.
You can also preview a first deployment. Pass --change-set-type CREATE to create-change-set. The stack then enters REVIEW_IN_PROGRESS and contains no resources until you execute the change set. For importing existing resources, the type is IMPORT.
The deploy command can stop at the review step too:
aws cloudformation deploy \
--template-file template.yaml \
--stack-name demo \
--parameter-overrides EnvName=prod \
--no-execute-changesetCommon errors
- Status
FAILEDwith "didn't contain changes". The template and parameters match the stack. Delete the empty change set and carry on. - Missing capabilities. Templates that create IAM resources need
--capabilities CAPABILITY_IAMorCAPABILITY_NAMED_IAM. Add the flag when you create the change set, not only when you execute it. - Parameters silently reset. With
--template-body, any parameter you omit and don't markUsePreviousValue=truereverts to the template default. Check theParameterssection of the described change set. - Executing a change set against a stack in the wrong state. Execution fails if the stack is mid-operation or in a state such as
UPDATE_ROLLBACK_FAILED.
Exercise
Exercise
You create a change set and describe it. One entry shows Action Modify, Replacement True, for a resource named DataBucket with no UpdateReplacePolicy set. What will happen if you execute it, and what would you do first?
Show answer
CloudFormation creates a new bucket with a new physical ID, then deletes the old one during cleanup, so the data in the old bucket is lost. Before executing, add UpdateReplacePolicy: Retain (and usually DeletionPolicy: Retain) and deploy that protection first, or change the template to avoid editing the immutable property. Then create a new change set to confirm Replacement is no longer a risk.