Sub topic 11
How property changes affect a resource
This chapter assumes you can write a basic template with parameters and resources, and that you have created and updated a stack from the AWS CLI. Here you will learn how CloudFormation applies a changed property to an existing resource, and how to predict the outcome before you update.
The three update behaviors
When you update a stack, CloudFormation compares the new template with the old one. For each resource whose properties changed, it applies one of three behaviors. The behavior depends on the property, not on the resource as a whole.
| Behavior | What happens | Physical ID |
|---|---|---|
| Update with no interruption | The resource is changed in place and keeps running. | Unchanged |
| Update with some interruption | The resource is changed in place but is briefly unavailable, for example during a reboot. | Unchanged |
| Replacement | CloudFormation creates a new resource, then deletes the old one. | New |
The documentation for each resource type lists the behavior in an Update requires note on every property. Check it before you change a property on anything that holds data or serves traffic.
Why replacement is the risky one
A replacement creates a second resource, so anything that points at the old one must be updated. CloudFormation handles references made with Ref or Fn::GetAtt inside the same stack. It cannot fix references that live outside the stack, such as a DNS record you manage by hand.
Replacement also deletes the old resource, and its data goes with it. A replaced database or bucket does not copy its contents to the new one. The UpdateReplacePolicy attribute, covered in a later chapter, controls what happens to the old resource.
Names add a second trap. If you set a fixed name such as BucketName, the new bucket cannot be created while the old one still has that name. The update fails. Leaving the name out lets CloudFormation generate a unique one, which is why this is usually the better choice.
Predicting a change
Take the bucket from the earlier chapters. Changing VersioningConfiguration is an in-place update. Changing BucketName requires replacement.
Resources:
AppBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: my-app-data-example
VersioningConfiguration:
Status: EnabledIf you edit BucketName in this template and update the stack, CloudFormation must replace the bucket. If you edit only Status, it updates the existing bucket.
Seeing the behavior in a change set
You do not have to memorize the tables. A change set reports what will happen. In describe-change-set, each resource change has a Replacement field with the value True, False or Conditional. A Conditional value means the outcome depends on the new value or on a resource's state.
aws cloudformation describe-change-set \
--stack-name demo \
--change-set-name preview \
--query "Changes[].ResourceChange.[LogicalResourceId,Action,Replacement]"An Action of Modify with Replacement set to True means the old resource will be deleted. Treat that line as a prompt to stop and review before you run execute-change-set.
Exercise
You change the BucketName property of an S3 bucket that has a fixed name, and the update fails. Why, and what is a safer way to name the bucket?
Show answer
Changing BucketName requires replacement. CloudFormation creates the new bucket before deleting the old one, and two buckets cannot share a name, so creation fails. Omit BucketName so CloudFormation generates a unique name, or include a changing part such as the stack name in it.