Sub topic 16
Composing templates with nested stacks
This chapter assumes you can write a template with Parameters, Resources and Outputs, and that you know how stacks are created and updated. It covers nested stacks and then two protections against accidental changes: stack policies and termination protection.
Why nest stacks
A single template tends to grow until it is hard to review and close to the template size limit. A nested stack splits it up. The parent template declares a child stack as a resource of type AWS::CloudFormation::Stack. CloudFormation creates the child as part of the parent's operation. You manage only the parent, and updates, rollbacks and deletes flow down to the children.
Nesting suits pieces that belong together, such as a network layer reused by several applications. Cross-stack references (exports and imports) suit stacks with separate lifecycles. A nested stack shares its parent's lifecycle.
Declaring a nested stack
Resources:
NetworkStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: https://s3.amazonaws.com/amzn-s3-demo-bucket/network.yaml
Parameters:
EnvName: !Ref EnvNameThree points matter here:
TemplateURLmust point to an S3 object. A nested template cannot be passed inline.Parameterspasses values into the child. The child must declare a parameter with each name you pass.- The parent reads child outputs with
!GetAtt NetworkStack.Outputs.OutputName. The child must declare that output.
AppSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: App access
VpcId: !GetAtt NetworkStack.Outputs.VpcIdBecause the parent references NetworkStack, CloudFormation creates the child first. This is the same implicit dependency you saw with Ref.
Updating nested stacks
When you update the parent, CloudFormation checks each child and applies only the changes it finds. If you edit a file in S3 but leave TemplateURL unchanged, the parent template itself has not changed. The usual remedy is to deploy with aws cloudformation package, which uploads local child templates and rewrites the URLs for you, or to use versioned S3 keys.
Stack policies
A stack policy is a JSON document that controls which resources an update may change. When you set one, all resources are protected by default, so you must explicitly allow actions. A policy applies to updates only. It does not stop a stack deletion.
This policy allows all updates except to the database:
{
"Statement": [
{
"Effect": "Allow",
"Action": "Update:*",
"Principal": "*",
"Resource": "*"
},
{
"Effect": "Deny",
"Action": "Update:*",
"Principal": "*",
"Resource": "LogicalResourceId/ProductionDatabase"
}
]
}Apply it with the CLI:
aws cloudformation set-stack-policy \
--stack-name demo \
--stack-policy-body file://policy.jsonAn explicit Deny overrides an Allow. To make a one-off change to a protected resource, pass --stack-policy-during-update-body on the update with a temporary policy that allows it. The stored policy stays unchanged.
Termination protection
Termination protection blocks delete-stack until you turn it off. It is a separate setting from the stack policy.
aws cloudformation update-termination-protection \
--stack-name demo \
--enable-termination-protectionA nested stack cannot be deleted on its own, and its protection setting follows the parent. Enable protection on the parent.
| Protection | Blocks | Does not block |
|---|---|---|
| Stack policy | Updates to specific resources | Stack deletion |
| Termination protection | Deleting the stack | Updates |
| DeletionPolicy | Deleting a resource when the stack goes | Updates that do not delete |
Exercise
A parent template passes EnvName to a child through Parameters, but the stack fails to create. The child template has no EnvName parameter. Why does it fail, and how do you fix it?
Show answer
The parent passes a parameter that the child does not declare, so CloudFormation rejects the child. Add a Parameters entry named EnvName to the child template, or remove the value from the parent's Parameters property.
Exercise
You set a stack policy that denies Update:* on ProductionDatabase. Does it prevent someone from running delete-stack? What would you enable to prevent that?
Show answer
No. A stack policy applies only to updates. Enable termination protection on the stack to block delete-stack.