AWS Certified CloudOps Engineer – Associate
CloudFormation DeletionPolicy vs stack policy vs protection
The CloudFormation documentation gives three separate controls. DeletionPolicy decides what happens to one resource when it leaves the stack: Delete, Retain or Snapshot. A stack policy blocks update actions on chosen resources during stack updates. Termination protection makes any attempt to delete the whole stack fail. None of them controls calls made outside CloudFormation.
What does DeletionPolicy do?
With no DeletionPolicy, CloudFormation deletes a resource when its stack is deleted. The exception is AWS::RDS::DBCluster, and AWS::RDS::DBInstance resources without a DBClusterIdentifier, where the default is Snapshot.
Retain keeps the resource and its contents and can be used on any resource type. Snapshot takes a snapshot before deletion on resources that support it, such as AWS::EC2::Volume, AWS::RDS::DBInstance and AWS::Redshift::Cluster. RetainExceptOnCreate behaves like Retain except when the operation that created the resource is rolled back.
The attribute also applies when a stack update removes the resource from the template. With Retain, the physical resource stays and only leaves CloudFormation's scope. It does not apply to replacement: when an update replaces a resource, the old physical resource is deleted.
Retained resources and snapshots keep incurring charges until you delete them. For a Secrets Manager secret, CloudFormation's default deletion uses the ForceDeleteWithoutRecovery flag.
What does a stack policy do?
A stack policy is a JSON document that says which update actions are allowed on which resources. A new stack has none, so every update is allowed. Once a policy is set, every resource is protected by default and any update not explicitly allowed is denied.
The actions are Update:Modify, Update:Replace, Update:Delete and Update:*. A Deny always overrides an Allow. Principal must be *, so one policy applies to every user; a stack has one policy only.
A stack policy applies only during stack updates. AWS describes it as a fail-safe against accidental updates, not an access control. To control who can act on a resource, use IAM.
What does termination protection do?
With termination protection enabled, a request to delete the stack fails and the stack, including its status, stays unchanged. It is disabled by default and can be turned on at creation or later.
The setting passes to nested stacks, and you change it on the root stack only. A stack update that removes a nested stack still deletes that nested stack.
Where the exam catches people
- Removing a resource from the template to hand it to another team without
Retain: the update deletes the resource. - Believing
DeletionPolicy: Retainsurvives replacement: when an update replaces a resource, the old physical resource is deleted. - Believing a stack policy stops a console or API delete of the resource itself: it applies only during stack updates. Use an IAM deny.
- Believing termination protection protects individual resources: it only makes attempts to delete the stack fail.
- Assuming an RDS DB instance is deleted outright with its stack: its default policy is
Snapshotunless it belongs to a cluster.