Nested Stacks

Composing a Template from Nested Stacks

A nested stack is an AWS 24 ::CloudFormation 24 ::Stack resource whose TemplateURL points at another template in S3 24 . The parent passes Parameters down and reads the child's outputs as !GetAtt Child.Outputs.Name; creating, updating or deleting the parent does the same to every child, as one unit. Here a root stack nests the table and stores the child's output in Parameter Store, with no export involved:

infra/nested/table.yaml and root.yaml: a child stack and its parentYAML
# table.yaml
Parameters: {Stage: {Type: String}}
Resources:
  BooksTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: !Sub booknest-${Stage}-nested-books
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions: [{AttributeName: id, AttributeType: N}]
      KeySchema: [{AttributeName: id, KeyType: HASH}]
Outputs: {TableName: {Value: !Ref BooksTable}}
# root.yaml
Parameters: {Stage: {Type: String, AllowedValues: [dev, prod], Default: dev}}
Resources:
  Table:
    Type: AWS::CloudFormation::Stack
    Properties: {TemplateURL: table.yaml, Parameters: {Stage: !Ref Stage}}
  TableNameParam:
    Type: AWS::SSM::Parameter
    Properties:
      Name: !Sub /booknest/${Stage}/nested/table
      Type: String
      Value: !GetAtt Table.Outputs.TableName

aws cloudformation package uploads each local TemplateURL file to a bucket and writes a copy of the parent that points at the upload (here at the CLI's LocalStack 63,725 endpoint; on AWS, an https://s3... URL):

Packaging the child into S3 and deploying the root stackShell
cd nested
aws s3 mb s3://booknest-templates
aws cloudformation package --template-file root.yaml --s3-bucket booknest-templates \
  --output-template-file root.packaged.yaml >/dev/null 2>&1 && echo "packaged"
aws cloudformation deploy --stack-name booknest-nested --template-file root.packaged.yaml
aws cloudformation list-stacks --output text \
  --query "StackSummaries[?starts_with(StackName,'booknest-nested')].[StackName,StackStatus]"
aws ssm get-parameter --name /booknest/dev/nested/table --query Parameter.Value --output text
Output
make_bucket: booknest-templates
packaged
Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - booknest-nested
booknest-nested   CREATE_COMPLETE
booknest-nested-Table-0fa8b662   CREATE_COMPLETE
booknest-dev-nested-books

The child got a generated name, booknest-nested-Table-...; on AWS it also carries ParentId and RootId, which LocalStack leaves empty. Nest when pieces always deploy together and one team owns them; use separate stacks with exports when owners or release cycles differ. cfn-lint 2,641 warns W3002 on a local TemplateURL, which only works through package.