Sub topic 19
Stack policies and termination protection
This chapter covers two guardrails that stop accidental changes to a stack. A stack policy limits which resources an update can modify. Termination protection blocks stack deletion. It assumes you can create and update stacks from the CLI, that you know what a logical ID is, and that you understand the update behaviors (no interruption, some interruption, replacement) and change sets from earlier chapters.
Two guardrails, two scopes
| Guardrail | What it blocks | What it does not block |
|---|---|---|
| Stack policy | Update actions (modify, replace, delete) on chosen resources during a stack update | Stack creation, stack deletion, changes made outside CloudFormation |
| Termination protection | The `DeleteStack` call on the whole stack | Updates that remove or replace resources inside the stack |
The two are independent. A production stack often uses both.
Stack policies
A stack policy is a JSON document attached to a stack. CloudFormation checks it every time an update tries to change a resource. If the policy denies the action, the update is rejected.
Three details shape how you write one:
- Once a stack has a policy, every resource is protected by default. Updates are denied unless a statement allows them. Your policy almost always starts with an allow-all statement.
- The
Principalmust be"*". A stack policy cannot grant or deny actions per IAM user. Use IAM permissions for that. - A
Denyoverrides anAllow, whatever the order of the statements.
The actions are Update:Modify, Update:Replace, Update:Delete, and the wildcard Update:*. The Resource element names a resource as LogicalResourceId/<id>. You can also use a Condition on ResourceType to cover every resource of a type.
This policy allows all updates, then forbids replacing or deleting the database:
{
"Statement": [
{
"Effect": "Allow",
"Action": "Update:*",
"Principal": "*",
"Resource": "*"
},
{
"Effect": "Deny",
"Action": ["Update:Replace", "Update:Delete"],
"Principal": "*",
"Resource": "LogicalResourceId/ProdDatabase"
}
]
}Notice that the policy still lets ProdDatabase be modified in place. That fits the update behaviors you learned earlier. A property that needs no interruption or some interruption is a modify, and a property that forces replacement is a replace.
To protect a whole class of resources, use a condition:
{
"Statement": [
{
"Effect": "Allow",
"Action": "Update:*",
"Principal": "*",
"Resource": "*"
},
{
"Effect": "Deny",
"Action": "Update:*",
"Principal": "*",
"Resource": "*",
"Condition": {
"StringEquals": {
"ResourceType": ["AWS::RDS::DBInstance", "AWS::S3::Bucket"]
}
}
}
]
}Applying and reading a policy
Save the JSON as policy.json, then attach it:
aws cloudformation set-stack-policy \
--stack-name demo \
--stack-policy-body file://policy.json
aws cloudformation get-stack-policy --stack-name demoYou can also pass --stack-policy-body when you run create-stack, so the stack is protected from its first update.
Overriding a policy for one update
Sometimes you need to make a blocked change on purpose. Use a temporary override with update-stack:
aws cloudformation update-stack \
--stack-name demo \
--template-body file://template.yaml \
--stack-policy-during-update-body file://override.jsonThe override applies only to that update. Afterward, the original policy is back in force. An override file is usually a short allow-all policy like the first statement above. Because set-stack-policy and the override flag need their own IAM permissions, you can let most users deploy and reserve policy changes for a smaller group.
Termination protection
Termination protection is a single flag on a stack. While it is on, delete-stack fails. Turn it on at creation or later:
aws cloudformation create-stack \
--stack-name prod-data \
--template-body file://template.yaml \
--enable-termination-protection
aws cloudformation update-termination-protection \
--stack-name demo \
--enable-termination-protectionCheck the current state with describe-stacks and read the EnableTerminationProtection field. An attempt to delete a protected stack returns an error saying the stack cannot be deleted while termination protection is enabled. To delete it on purpose, disable the flag first:
aws cloudformation update-termination-protection \
--stack-name demo \
--no-enable-termination-protection
aws cloudformation delete-stack --stack-name demoThat two-step process is the point. It forces a deliberate action before a deletion.
Edge cases
- Nested stacks inherit the setting of their parent. You change it on the parent, not on the nested stack.
- Protection covers the stack, not its contents. An update that removes a resource from the template still deletes that resource, so use a stack policy or
DeletionPolicyfor that case. - A stack that other stacks import from already cannot be deleted while those imports exist. Termination protection adds a second barrier.
Exercise
A stack has a stack policy with one statement: Allow Update:* on all resources. An engineer edits the template so that the logical ID AppBucket no longer appears, then runs an update. Separately, the stack has termination protection enabled. Does either guardrail stop the bucket from being deleted by the update, and how would you protect the bucket?
Show answer
Neither stops it. The stack policy allows Update:* on everything, so the Update:Delete is allowed. Termination protection only blocks DeleteStack on the whole stack, not removal of one resource during an update. To protect the bucket, add a Deny statement for Update:Delete on LogicalResourceId/AppBucket to the stack policy, and set DeletionPolicy: Retain on the bucket so the data survives if it is removed anyway.