
Protecting Stack Resources Using CloudFormation Stack Policy
CloudFormation stack policy lets you mark specific resources as protected against updates and deletion.
Problem Statement
CloudFormation changes your live infrastructure to match your template. Every resource follows the same rule. A production database is treated the same as a temporary test volume.
That is usually fine. But sometimes you want to protect one resource and still let CloudFormation update everything else as normal. Stack policy does that.
How CloudFormation Decides What To Do
Your template describes what you want to exist. When you run
update-stack , CloudFormation compares the template to what is already running, then changes the real resources to match. You never fix resources by hand, so the running stack cannot quietly differ from the template.What happens to a resource depends on what you changed in the template:
Kind of template change | Example | Effect on the resource |
|---|---|---|
Change a property CloudFormation can update in place | Certain AWS::CloudTrail::Trail properties | No interruption. The physical ID stays the same and the resource stays available. |
Change a property the service has to reconfigure | Certain AWS::EC2::Instance properties | Short interruption. The physical ID stays the same but the resource goes down briefly. |
Change a property that can only be set when the resource is created | StackName on AWS::CloudFormation::Stack | Replacement. CloudFormation builds a new resource, deletes the old one, and the physical ID changes. |
Change a property that cannot be updated at all | Encrypted or KmsKeyId on AWS::EC2::Volume | The update fails with UPDATE_FAILED . The resource is left alone. |
Remove the resource from the template | Delete the resource block, or rename it | Deletion. CloudFormation deletes the resource. |
AWS lists the answer for every property, under "Update requires" in the resource and property type reference.
If you delete a resource from the template, or rename it, CloudFormation does not compare properties at all. It sees the old name as gone, and any new name as a brand new resource to build. That name is called the logical ID, and it is what stack policy matches on later.
DeletionPolicy decides what happens to the data when a delete does happen. Set it to Retain or Snapshot and the data survives, whatever the template says. Stack policy is the other half: it decides whether the delete is allowed at all.How Stack Policy Works
A stack policy is a JSON document. It only applies while a stack is being updated. It is not the same as
TerminationProtection , and it is not IAM. AWS docs say it applies to every principal, whatever their IAM permissions are, so it acts as a guardrail on top of the access control you already have.- Default deny once attached. A resource can only be changed if the policy allows it.
- One policy per stack. Use several statements to cover several resources.
- Three action types:
Update:Modify,Update:Replace,Update:Delete.Update:*covers all three.
- Explicit Deny wins. Same evaluation logic as IAM.
- It matches on the resource name from your template.
"Resource": "LogicalResourceId/MyVolume"catches a rename the same way it catches a delete.
- You can override it on purpose. Pass
--stack-policy-during-update-bodyonupdate-stackto let one change through, just for that update. You need thecloudformation:SetStackPolicypermission. This is not the same asset-stack-policy, which replaces the stored policy for good. The stored policy stays as it was.
Walkthrough: Two EBS Volumes
Step 1. Create a stack with two EBS volumes.
# volumes.yamlAWSTemplateFormatVersion: '2010-09-09'Description: Two EBS volumes for stack policy demo
Resources: VolumeOne: Type: AWS::EC2::Volume Properties: Size: 10 AvailabilityZone: ap-southeast-1a VolumeType: gp3 Tags: - Key: Name Value: demo-volume-one
VolumeTwo: Type: AWS::EC2::Volume Properties: Size: 10 AvailabilityZone: ap-southeast-1a VolumeType: gp3 Tags: - Key: Name Value: demo-volume-two
Outputs: VolumeOneId: Value: !Ref VolumeOne VolumeTwoId: Value: !Ref VolumeTwoaws cloudformation create-stack \ --stack-name stack-policy-demo \ --template-body file://volumes.yaml
Step 2 — Apply a stack policy protecting
VolumeOne .// stack-policy.json{ "Statement": [ { "Effect": "Allow", "Action": "Update:*", "Principal": "*", "Resource": "*" }, { "Effect": "Deny", "Action": "Update:*", "Principal": "*", "Resource": "LogicalResourceId/VolumeOne" } ]}aws cloudformation set-stack-policy \ --stack-name stack-policy-demo \ --stack-policy-body file://stack-policy.json
The
Allow lets CloudFormation keep updating everything else. The Deny blocks every kind of update on VolumeOne , because it uses Update:* . Check what is stored with aws cloudformation get-stack-policy --stack-name stack-policy-demo .Step 3 — Confirm the unprotected volume still updates normally.
# volumes.yaml — VolumeTwo's Size changed to 20Resources: VolumeOne: Type: AWS::EC2::Volume Properties: Size: 10 AvailabilityZone: ap-southeast-1a VolumeType: gp3 VolumeTwo: Type: AWS::EC2::Volume Properties: Size: 20 AvailabilityZone: ap-southeast-1a VolumeType: gp3aws cloudformation update-stack \ --stack-name stack-policy-demo \ --template-body file://volumes.yaml
This template does not change
VolumeOne , so the Allow applies and the update finishes with UPDATE_COMPLETE . The policy only protects the resource it names.Step 4 — Attempt a change to the protected volume.
# volumes.yaml — VolumeOne's Size changed to 20Resources: VolumeOne: Type: AWS::EC2::Volume Properties: Size: 20 AvailabilityZone: ap-southeast-1a VolumeType: gp3 VolumeTwo: Type: AWS::EC2::Volume Properties: Size: 20 AvailabilityZone: ap-southeast-1a VolumeType: gp3aws cloudformation update-stack \ --stack-name stack-policy-demo \ --template-body file://volumes.yaml
CloudFormation accepts the call and starts the update. It then hits the Deny on
VolumeOne , stops, and rolls the whole stack back. You get UPDATE_ROLLBACK_COMPLETE , and VolumeOne is unchanged.Size is a property CloudFormation can change in place, so this is the smallest change you could ask for. Update:* still blocks it. The Deny is about the resource, not about how risky the change is.Step 5. Try to delete the protected volume by removing it from the template.
# volumes.yaml — VolumeOne removedResources: VolumeTwo: Type: AWS::EC2::Volume Properties: Size: 20 AvailabilityZone: ap-southeast-1a VolumeType: gp3aws cloudformation update-stack \ --stack-name stack-policy-demo \ --template-body file://volumes.yaml
CloudFormation checks the stored stack policy against the template you just sent, before it does anything else. The policy still names
"Resource": "LogicalResourceId/VolumeOne" , but that name is not in this template. So the call is rejected right away:An error occurred (ValidationError) when calling the UpdateStack operation:Error validating existing stack policy: Unknown logical id 'LogicalResourceId/VolumeOne'in statement {} - stack policies can only be applied to logical ids referenced in the templateCloudFormation never creates an event for
VolumeOne . The volume stays available and unchanged. Renaming instead of deleting gets the same rejection, because CloudFormation treats a rename as a delete plus a create.To really remove
VolumeOne , change the stack policy first so it no longer names it. Then send the template without the resource.// stack-policy-updated.json{ "Statement": [ { "Effect": "Allow", "Action": "Update:*", "Principal": "*", "Resource": "*" } ]}aws cloudformation set-stack-policy \ --stack-name stack-policy-demo \ --stack-policy-body file://stack-policy-updated.json
aws cloudformation update-stack \ --stack-name stack-policy-demo \ --template-body file://volumes.yamlExtra Protection for Important Resources
For resources you really do not want to lose, like production databases and encryption keys, use stack policy together with:
-
DeletionPolicy: RetainorSnapshot, so the data survives no matter what caused the delete
- Termination protection, so nobody can delete the whole stack in one step
- Least privilege IAM on
cloudformation:SetStackPolicyandUpdateStack, because stack policy adds to access control, it does not replace it
- Change sets, so a person reviews the changes before they run

References
- AWS CloudFormation User Guide, Prevent updates to stack resources
- AWS CloudFormation API Reference, UpdateStack (
StackPolicyDuringUpdateBodyparameter)
- AWS CloudFormation Template Reference, AWS::EC2::Volume
- AWS CloudFormation User Guide, Update behaviors of stack resources
This content publication is assisted by Claude Sonnet 5.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article