Sub topic 15
Protecting data with DeletionPolicy and UpdateReplacePolicy
This chapter assumes you can write a template with a Resources section and that you know the stack lifecycle: create, update, delete. By default, CloudFormation deletes any resource it removes from a stack. For a stateless resource that is the right behavior. For a database, volume, or bucket full of data, it can destroy the only copy. Two resource attributes control this: DeletionPolicy and UpdateReplacePolicy.
Two attributes for two events
Both attributes sit beside Type and Properties, not inside Properties. They cover different events.
| Attribute | Triggers when | Acts on |
|---|---|---|
| `DeletionPolicy` | The stack is deleted, or the resource is removed from the template | The resource being removed |
| `UpdateReplacePolicy` | An update replaces the resource with a new physical resource | The old resource, after the replacement succeeds |
A replacement happens when you change a property whose update behavior is "replacement", such as a fixed name. CloudFormation creates the new resource first, then removes the old one. Without UpdateReplacePolicy, the old resource and its data are deleted at that point, even though you never deleted the stack.
Policy values
Deleteis the default for most resource types. CloudFormation deletes the resource.Retainremoves the resource from the stack but leaves it in your account. It keeps running and keeps billing, and CloudFormation no longer manages it.Snapshottakes a final snapshot, then deletes the resource. It works only on types that support snapshots, such asAWS::EC2::Volume,AWS::RDS::DBInstance,AWS::RDS::DBCluster,AWS::ElastiCache::CacheCluster,AWS::Redshift::Cluster,AWS::Neptune::DBClusterandAWS::DocDB::DBCluster. S3 buckets do not support it.RetainExceptOnCreateis available only forDeletionPolicy. It retains the resource on deletion but deletes it if the stack's initial creation fails and rolls back.
Some types have a different default. An AWS::RDS::DBCluster, and a AWS::RDS::DBInstance without a DBClusterIdentifier, default to Snapshot. Check the documentation for each critical resource type.
Example template
AWSTemplateFormatVersion: "2010-09-09"
Description: Data resources protected from deletion and replacement
Resources:
DataBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
BucketName: !Sub "${AWS::AccountId}-${AWS::Region}-app-data"
DataVolume:
Type: AWS::EC2::Volume
DeletionPolicy: Snapshot
UpdateReplacePolicy: Snapshot
Properties:
AvailabilityZone: !Select [0, !GetAZs ""]
Size: 20
VolumeType: gp3Changing BucketName replaces the bucket. Because of UpdateReplacePolicy: Retain, the old bucket stays in your account with its objects. Changing Size on the volume does not replace it, because an EBS volume can be resized in place. The Snapshot policy would apply only if you changed a property that forces replacement, such as AvailabilityZone, or when the stack is deleted.
How the decision flows
For deletion the policy is DeletionPolicy. For replacement it is UpdateReplacePolicy. The flow is otherwise the same.
Applying the policy in the right order
CloudFormation reads the policy from the template version last deployed to the stack. Adding DeletionPolicy: Retain and deleting the stack in the same step does not protect you. Deploy the policy first, then delete.
# 1. Deploy the template that contains the policies
aws cloudformation deploy \
--template-file template.yaml \
--stack-name data-demo
# 2. Only now is it safe to delete the stack
aws cloudformation delete-stack --stack-name data-demo
aws cloudformation wait stack-delete-complete --stack-name data-demo
# 3. Confirm the snapshot exists and the bucket remains
aws ec2 describe-snapshots --owner-ids self \
--query "Snapshots[].[SnapshotId,Description]"The stack ends in DELETE_COMPLETE, but the retained bucket is still there. To manage it again, import it into a stack, which a later chapter covers.
Edge cases
- Removing a resource from the template triggers
DeletionPolicyduring the next update, even though the stack survives. - Name collisions. A retained bucket with a fixed name blocks recreation of the same stack, because bucket names are globally unique. Use generated names, or delete the orphan by hand first.
- Non-empty buckets. With the default
Delete, a bucket that still holds objects fails to delete. The resource entersDELETE_FAILEDand the stack deletion stops.Retainavoids the error but leaves the bucket. - Cost. Retained databases keep running and snapshots keep billing until you remove them.
- Rollback. If an update replaces a resource and then fails, CloudFormation rolls back and removes the new resource.
UpdateReplacePolicygoverns how that new resource is treated, so check which one applies before relying on rollback to protect data.
Exercise
A template defines an RDS-backed data store with DeletionPolicy: Snapshot and no UpdateReplacePolicy. You change a property that forces replacement and the update succeeds. What happens to the old database, and what would you add to the template?
Show answer
With no UpdateReplacePolicy, the old database is deleted after the new one is created, using the default Delete behavior, so its data is lost. DeletionPolicy applies only to stack deletion or removal from the template, not to replacement. Add UpdateReplacePolicy: Snapshot (or Retain) to the resource, and deploy that change before the replacing change.