You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
As your infrastructure grows, a single CloudFormation template can become unwieldy — thousands of lines, dozens of resources, and complex interdependencies. CloudFormation provides two powerful mechanisms for breaking large templates into manageable, reusable pieces: nested stacks and cross-stack references. In this lesson we will explore both approaches, understand when to use each, and see practical examples.
A monolithic template — one that defines every resource in a single file — presents several challenges:
Both nested stacks and cross-stack references solve these problems, but in different ways.
A nested stack is a stack that is created as a resource within another stack. The parent stack includes a resource of type AWS::CloudFormation::Stack that points to a child template.
AWS::CloudFormation::Stack resourceAWSTemplateFormatVersion: '2010-09-09'
Description: Parent stack that composes nested stacks
Parameters:
Environment:
Type: String
Default: dev
AllowedValues: [dev, staging, production]
Resources:
NetworkStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: https://my-templates.s3.amazonaws.com/network.yaml
Parameters:
Environment: !Ref Environment
VpcCidr: '10.0.0.0/16'
DatabaseStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: https://my-templates.s3.amazonaws.com/database.yaml
Parameters:
VpcId: !GetAtt NetworkStack.Outputs.VpcId
SubnetIds: !GetAtt NetworkStack.Outputs.PrivateSubnetIds
Environment: !Ref Environment
ApplicationStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: https://my-templates.s3.amazonaws.com/application.yaml
Parameters:
VpcId: !GetAtt NetworkStack.Outputs.VpcId
SubnetIds: !GetAtt NetworkStack.Outputs.PublicSubnetIds
DatabaseEndpoint: !GetAtt DatabaseStack.Outputs.DatabaseEndpoint
Environment: !Ref Environment
Outputs:
ApplicationURL:
Value: !GetAtt ApplicationStack.Outputs.LoadBalancerDNS
AWSTemplateFormatVersion: '2010-09-09'
Description: Network stack — VPC, subnets, and gateways
Parameters:
Environment:
Type: String
VpcCidr:
Type: String
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCidr
Tags:
- Key: Name
Value: !Sub '${Environment}-vpc'
PublicSubnetA:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: !Select [0, !Cidr [!Ref VpcCidr, 4, 8]]
AvailabilityZone: !Select [0, !GetAZs '']
PrivateSubnetA:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: !Select [2, !Cidr [!Ref VpcCidr, 4, 8]]
AvailabilityZone: !Select [0, !GetAZs '']
Outputs:
VpcId:
Value: !Ref VPC
PublicSubnetIds:
Value: !Ref PublicSubnetA
PrivateSubnetIds:
Value: !Ref PrivateSubnetA
!GetAtt ChildStack.Outputs.OutputNameCross-stack references let independent stacks share values through exports and imports. Unlike nested stacks, cross-stack referenced stacks are managed independently.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.