Nested Stacks
What you'll learn: How to break down large CloudFormation templates into reusable, manageable pieces using nested stacks.
The Core Idea
As your infrastructure grows, single CloudFormation templates become unwieldy—hundreds or thousands of lines of YAML/JSON managing everything from networking to databases to application servers. Nested stacks let you organize complex infrastructure by creating a hierarchy: a parent stack references child stack templates, each handling a specific component.
Think of it like organizing a company: instead of one manager overseeing every employee directly, you have department heads (child stacks) who manage their teams, all reporting to a CEO (parent stack).
How It Works
You use the AWS::CloudFormation::Stack resource type in your parent template to reference child templates stored in S3. Each child stack is a complete, independent CloudFormation template that the parent stack launches and manages.
Parent template structure:
Resources:
NetworkStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: https://s3.amazonaws.com/my-bucket/network.yaml
Parameters:
VPCCidr: 10.0.0.0/16
The parent passes parameters down to children and can retrieve outputs using Fn::GetAtt:
Outputs:
VPCId:
Value: !GetAtt NetworkStack.Outputs.VPCId
Why Use Nested Stacks?
- Reusability: Standard network or database configurations become templates you reference across projects
- Modularity: Separate concerns (networking, compute, storage) into logical units
- Limits: Bypass the template size limit by splitting across multiple files
- Updates: Modify individual components without touching everything
Key Takeaway: Nested stacks organize complex CloudFormation infrastructure into hierarchical, reusable components—each child stack is a full template launched and managed by a parent stack resource.