Sub topic 14
Protecting data with DeletionPolicy and UpdateReplacePolicy
This chapter assumes you can write a template with Resources and Parameters, and that you have created and deleted a stack from the AWS CLI. It covers the two resource attributes that decide what happens to a resource when CloudFormation would normally remove it.
Why stacks can destroy data
CloudFormation removes a physical resource in two situations:
- You delete the stack. CloudFormation deletes every resource in it.
- An update replaces a resource. CloudFormation creates a new one, then deletes the old one.
For a stateless resource this is fine. For a database or a bucket full of objects, it means data loss. Two attributes, written beside Type and not inside Properties, control this behavior.
DeletionPolicy
DeletionPolicy applies when the resource is removed from the stack, either because the stack is deleted or because you remove the resource from the template.
| Value | Effect |
|---|---|
| `Delete` | Deletes the resource. This is the default for most types. |
| `Retain` | Leaves the resource in your account and removes it from the stack. |
| `Snapshot` | Takes a final snapshot, then deletes the resource. Only some types support it, such as `AWS::RDS::DBInstance`, `AWS::EC2::Volume` and `AWS::ElastiCache::CacheCluster`. |
The default for AWS::RDS::DBCluster is Snapshot. Check the documentation for the type you use, because defaults differ.
UpdateReplacePolicy
UpdateReplacePolicy applies when a property change forces replacement. It controls what happens to the old resource after the new one exists. It accepts Delete, Retain and Snapshot.
The two attributes cover different events. DeletionPolicy does not protect you during a replacement, so set both on resources that hold data.
Resources:
DataBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
UpdateReplacePolicy: RetainWhat Retain leaves behind
A retained resource keeps running and keeps costing money, but CloudFormation no longer tracks it. If you delete the stack and create it again from the same template, CloudFormation tries to create a second bucket. With a fixed bucket name, that fails because the name is taken. Either let CloudFormation generate names, or import the retained resource into the new stack, which a later chapter covers.
Common errors
- Retain on a non-empty bucket you want gone. Deleting an S3 bucket that contains objects fails regardless of policy. Retain avoids the failure by skipping the delete, so the bucket stays.
- Setting only DeletionPolicy. A property change that forces replacement still deletes the old resource. Preview the update with a change set and look for
Replacement: True. - Placing the attribute under Properties. Validation rejects it. It belongs at the same level as
Type. - Expecting rollback to restore data. A retained or snapshotted resource is the only recovery path. Rollback does not bring deleted data back.
Exercise
A stack holds an RDS instance with DeletionPolicy: Snapshot and no UpdateReplacePolicy. You change a property that forces replacement. What happens to the old instance and its data?
Show answer
DeletionPolicy does not apply to replacement, so the update uses UpdateReplacePolicy, which here defaults to Delete. The old instance is deleted with no snapshot. Add UpdateReplacePolicy: Snapshot (or Retain) to keep the data.