Sub topic 18
Composing templates with nested stacks
This chapter assumes you can write a template with Parameters and Outputs, use Ref, Fn::GetAtt and Fn::Sub, and create and update stacks from the AWS CLI. It covers how nested stacks work, how values move between parent and child, and the errors people hit most often.
What a nested stack is
A nested stack is a stack created by another stack. The parent template declares a resource of type AWS::CloudFormation::Stack. CloudFormation loads the child template from the TemplateURL, creates a separate stack from it, and records the parent-child link. The parent is the root when it has no parent of its own.
Use nested stacks to split a large template into pieces you can reuse. A network layer, a database layer and an application layer can each be a child template. Nested stacks also help with template size limits. A template passed in the request body is limited to 51,200 bytes, and one loaded from S3 can be up to 1 MB. A parent that only declares child stacks stays small.
Declaring a child stack
The child template must be in S3, because TemplateURL must point to an S3 location. The parent from the Topic Spine looks like this:
Resources:
NetworkStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: https://s3.amazonaws.com/amzn-s3-demo-bucket/network.yaml
Parameters:
EnvName: !Ref EnvNameThe Parameters property of the resource maps to the child's Parameters section. The parent passes EnvName into the child. The child must declare a parameter with that exact name, or the create fails.
Passing values between parent and child
Values flow in two directions through two different mechanisms.
- Parent to child: the
Parametersproperty on theAWS::CloudFormation::Stackresource. - Child to parent: the child's
Outputs. The parent reads them withFn::GetAttusing the formOutputs.OutputName.
Suppose network.yaml declares an output named VpcId. The parent can pass that value to a second child:
Resources:
NetworkStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: https://s3.amazonaws.com/amzn-s3-demo-bucket/network.yaml
Parameters:
EnvName: !Ref EnvName
AppStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: https://s3.amazonaws.com/amzn-s3-demo-bucket/app.yaml
Parameters:
EnvName: !Ref EnvName
VpcId: !GetAtt NetworkStack.Outputs.VpcIdThe GetAtt creates an implicit dependency. CloudFormation finishes NetworkStack before it starts AppStack. Without a reference between two child stacks, CloudFormation may create them in parallel. Use DependsOn if one child needs another without exchanging a value.
The parent's Parameters section must also declare EnvName, since the parent uses !Ref EnvName.
How updates propagate
Nested stacks matter most during updates. To update a child, you update the root stack, not the child directly. The workflow is:
- Upload the changed child template to S3.
- Run an update on the parent, for example with
aws cloudformation deploy. - CloudFormation compares the parent template. If only the child's contents changed and the
TemplateURLstring is identical, the parent sees no change and skips the child.
That third point is a common gotcha. CloudFormation does not read the child template to see if it differs. It acts when the parent's resource properties change. To force an update, give each upload a new S3 key, such as a version or hash in the file name, or use aws cloudformation package. This command uploads local child templates and rewrites the TemplateURL for you.
aws cloudformation package \
--template-file parent.yaml \
--s3-bucket amzn-s3-demo-bucket \
--output-template-file packaged.yaml
aws cloudformation deploy \
--template-file packaged.yaml \
--stack-name demo-root \
--capabilities CAPABILITY_IAMWith package, the parent can set TemplateURL: ./network.yaml locally. The command replaces it with the S3 URL.
Change sets on the root stack show nested changes. The change set lists the child stack as a resource, and with nested change sets enabled (--include-nested-stacks on create-change-set) it also lists the child's own resource changes.
Capabilities and failures
If any child template creates IAM resources, you must acknowledge the capability on the root operation. Pass CAPABILITY_IAM or CAPABILITY_NAMED_IAM. If a child uses transforms such as AWS::Serverless, also pass CAPABILITY_AUTO_EXPAND.
A failure in a child fails the child stack. CloudFormation then marks the child resource in the parent as failed, and the parent rolls back by default. Read the failure in the child's own events. The parent's events show only that the nested stack resource failed. Find the child stack by its physical ID, which is a stack ARN, and run describe-stack-events against it.
aws cloudformation describe-stack-resources \
--stack-name demo-root \
--logical-resource-id NetworkStack \
--query "StackResources[0].PhysicalResourceId"Deletion
Deleting the root stack deletes the children first or last according to dependency order. Do not delete a child directly. That breaks the parent's state, and the next parent update can fail. A child resource with DeletionPolicy: Retain is still retained when the stack is deleted.
Nested stacks versus exports
Cross-stack references with exports link independent stacks, while nested stacks form one unit.
| Aspect | Nested stacks | Exports and imports |
|---|---|---|
| Lifecycle | Managed together through the root | Each stack is managed on its own |
| Value passing | Parameters and `Outputs.Name` | `Export` and `Fn::ImportValue` |
| Deletion | Root delete removes all children | Exporting stack cannot be deleted while imported |
| Reuse | Same child in many parents | Shared values across many stacks |
| Best for | One application split into parts | Long-lived shared layers such as networking |
Design cautions
- Nesting depth has a limit, so keep hierarchies shallow, usually one level.
- Keep child interfaces small: few parameters, few outputs.
- Child templates should not assume which parent calls them.
- A child cannot read a sibling's outputs directly. Route the value through the parent.
Exercise
A parent stack has NetworkStack and AppStack, both nested. You upload a modified network.yaml to the same S3 key and run an update on the parent. Nothing changes in the network. Why, and how do you fix it?
Show answer
The parent's TemplateURL property did not change, so CloudFormation sees no difference in the parent and does not update the child. Upload the modified template under a new S3 key, or use aws cloudformation package, so that TemplateURL changes and the update reaches the child.
Exercise
How does AppStack receive the VpcId created inside NetworkStack, and what ordering does this create?
Show answer
The child NetworkStack declares an output VpcId. The parent passes !GetAtt NetworkStack.Outputs.VpcId as a parameter to AppStack. This creates an implicit dependency, so NetworkStack finishes before AppStack starts.