What You'll Learn
You'll learn how to use stack policies in AWS CloudFormation to protect critical resources from accidental updates or deletions during stack operations.
Why Stack Policies Matter
Imagine you have a production database or a critical networking component in your CloudFormation stack. Without protection, someone could accidentally update or delete it while modifying other resources. Stack policies act like a security guard—they explicitly control which resources can be modified and under what conditions.
By default, CloudFormation allows updates to all resources in a stack. A stack policy is a JSON document that defines update permissions for specific resources, helping you prevent costly mistakes.
How Stack Policies Work
A stack policy uses Allow and Deny statements (similar to IAM policies) to control update actions:
- Effect: Either
AlloworDeny - Principal: Always
"*"(applies to all users) - Action:
Update:Modify,Update:Replace, orUpdate:Delete - Resource: Which stack resources the policy applies to
Once you apply a stack policy to a stack, all resources are protected by default (implicitly denied). You must explicitly allow updates to resources you want to remain modifiable.
Common Use Cases
- Protect databases from accidental replacement or deletion
- Lock down production networking (VPCs, subnets, security groups)
- Prevent deletion of stateful resources during updates
- Require manual overrides for sensitive changes
Stack Policy Limitations
- Stack policies only prevent updates, not initial resource creation
- They don't prevent stack deletion (use termination protection for that)
- You can temporarily override policies during updates when necessary
Key Takeaway: Stack policies are your safety net against unintended changes—apply them to protect critical resources in production stacks, ensuring updates require careful, deliberate action.