Sub topic 12
How property changes affect a resource
This chapter assumes you can write a basic template with Resources and Parameters. It also assumes you have created and updated a stack from the AWS CLI. When you update a stack, CloudFormation compares the new template and parameter values with the current ones. Each changed property then has one of three effects on its resource. Knowing which effect applies lets you predict an update before you run it.
The three update behaviors
Every property of every resource type has an update behavior. The "Update requires" line in the resource's documentation states it.
| Behavior | What happens | Physical ID | Typical impact |
|---|---|---|---|
| No interruption | The resource is modified in place and keeps running. | Unchanged | None |
| Some interruption | The resource is modified in place but is briefly unavailable or restarts. | Unchanged | Short outage, such as an EC2 instance stop and start |
| Replacement | CloudFormation creates a new resource and deletes the old one. | New ID | Data on the old resource is lost unless you protect it |
The behavior belongs to the property, not to the resource. One resource can have properties in all three categories.
- On
AWS::EC2::Instance, changingTagsneeds no interruption. - Changing
InstanceTypeneeds some interruption, because the instance is stopped, resized and started. This applies to EBS-backed instances. - Changing
ImageIdneeds replacement.
A single update can change several properties at once. The most disruptive behavior among them decides what happens to the resource. If you change a tag and an AMI in the same update, the instance is replaced.
How replacement works
Replacement is the case that causes real damage, so it is worth understanding in detail.
- CloudFormation creates the new resource with the new property values.
- It updates every resource that references the old one, such as through
ReforFn::GetAtt, so that they point to the new one. - The stack reaches
UPDATE_COMPLETE_CLEANUP_IN_PROGRESS. - CloudFormation deletes the old resource.
The old resource is deleted last. This ordering means that if the update fails before cleanup, CloudFormation can roll back to the old resource, which still exists.
Creating the new resource first has a consequence for resources with fixed names. If you set a custom name, such as BucketName, and a property change forces replacement, the new resource cannot use the same name while the old one exists. The update fails with a name conflict. For this reason, letting CloudFormation generate names is the safer default for resources that might be replaced.
Replacement also changes the physical ID. Anything outside the stack that stored the old ID, such as a DNS record, a script or a hard-coded ARN, keeps pointing at the deleted resource.
Reading the update behavior in a template
The next template shows properties from each category. The comments state the behavior for an update to the value shown.
AWSTemplateFormatVersion: "2010-09-09"
Description: Update behavior examples
Parameters:
ImageId:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64
InstanceType:
Type: String
Default: t3.micro
Resources:
AppInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref ImageId # Replacement
InstanceType: !Ref InstanceType # Some interruption (EBS-backed)
Tags: # No interruption
- Key: Name
Value: app
DataBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
BucketName: !Sub "app-data-${AWS::AccountId}-${AWS::Region}" # Replacement
VersioningConfiguration: # No interruption
Status: EnabledA caution about this template: ImageId here resolves from an SSM parameter. When AWS publishes a new image, the parameter's value changes, and the next stack update replaces the instance even though you edited nothing. Pin the AMI if you want updates to be predictable.
Properties that hide behavior
Some cases are easy to miss.
- Conditional behavior. The effect of a change can depend on other settings. For example, whether an RDS instance change needs a restart can depend on the engine and on other properties. Read the notes in the documentation beside the property.
- Dependent resources. Replacing one resource can force changes in others. If a security group is replaced, instances that reference it by
Refmust be updated to use the new group ID. Those instances are modified, and depending on the property, that can interrupt them. - Name properties. Setting a name property such as
BucketNameorTableNameturns later changes to that name into replacements. Setting it also blocks replacement, as described earlier, because of name conflicts. - Immutable by design. Some properties never support in-place changes. For example,
AWS::S3::Bucketreplaces on aBucketNamechange.
Checking the effect before you update
Do not rely on memory for this. A change set reports the effect for each resource. The Replacement field on a resource change is True, False or Conditional. Conditional means the outcome depends on a value that is not known until the update runs.
aws cloudformation create-change-set \
--stack-name demo \
--change-set-name preview \
--template-body file://template.yaml \
--parameters ParameterKey=InstanceType,ParameterValue=t3.small
aws cloudformation describe-change-set \
--stack-name demo \
--change-set-name preview \
--query "Changes[].ResourceChange.{Id:LogicalResourceId,Action:Action,Replacement:Replacement}"A result with "Replacement": "True" for a database or bucket is the signal to stop and decide how to protect the data. The next chapter covers change sets in full.
Decision flow
Reducing the risk of replacement
- Preview every update with a change set and check the
Replacementcolumn. - Set
UpdateReplacePolicyon resources that hold data, such as buckets, databases and volumes. - Avoid custom physical names on resources that may be replaced.
- Pin AMI IDs and other values that change outside your control, or accept that they trigger replacement.
- Apply a stack policy to block updates to critical resources. The stack policies chapter covers this.
Exercise
A stack contains an EC2 instance. One update changes both the Tags value (no interruption) and the ImageId (replacement). What happens to the instance, and in what order does CloudFormation create and delete resources?
Show answer
The instance is replaced, because the most disruptive behavior among the changed properties decides the outcome. CloudFormation creates the new instance first, updates resources that reference the old one, and deletes the old instance during cleanup.
Exercise
An update to an S3 bucket with a fixed BucketName fails with a name conflict after you change a property that requires replacement. Why, and how do you avoid it in future?
Show answer
CloudFormation creates the replacement before deleting the original, and bucket names are globally unique, so the new bucket cannot take the name while the old one exists. Avoid it by letting CloudFormation generate the name, or by not changing properties that force replacement on a bucket with a fixed name.