Skip to main content
In this lesson you’ll learn how to modularize CloudFormation templates using nested stacks. A nested stack is a CloudFormation stack created as a resource inside another CloudFormation stack. The parent stack declares AWS::CloudFormation::Stack resources whose TemplateURL properties point at child templates stored in Amazon S3. This pattern helps you split large templates into smaller, reusable units and manage them together. What you’ll build in this walkthrough
  • One parent stack that creates two nested (child) stacks.
  • Each child stack provisions a single S3 bucket.
  • Child templates are stored in an S3 bucket and referenced by the parent template.
Why use nested stacks
  • Reuse common infrastructure components across multiple stacks.
  • Keep templates readable and maintainable by breaking them into logical units.
  • Apply independent updates to child templates while keeping a single parent orchestration point.
Prerequisites and cleanup
  • If you have an existing demo stack or related S3 buckets, delete them to get a clean environment.
  • After deleting the stack, verify the nested resources (for example, the buckets) are removed.
  • Create an S3 bucket to host your CloudFormation templates. Bucket names must be globally unique. In this lesson we use eden-kodekloud-bkt-961 as an example.
  • Keep templates in the same AWS region where you will create the parent stack.
A screenshot of the Amazon S3 console open to the bucket "eden-kodekloud-bkt-961" with the Objects tab selected. Visible controls include Upload, Copy S3 URI/URL, Download, Delete, Actions, and Create folder.
Select the parent template source in CloudFormation
  • When creating the parent stack, choose the “Amazon S3 URL” option for the template source so CloudFormation can retrieve the parent template from your bucket.
A screenshot of the AWS CloudFormation "Create stack" console showing the Template source options, with "Amazon S3 URL" selected and an input field for the S3 template URL. The browser toolbar and AWS top navigation are visible at the top.
Create the child templates (child stacks)
  • In your project folder (for example in Visual Studio Code) create two files:
    • first-s3-bkt.yaml
    • second-s3-bkt.yaml
A dark-themed Visual Studio Code window showing the Explorer sidebar with YAML files (first-s3-bkt.yaml, stack.yaml and a renaming entry for second-s3-bkt) and an open, mostly empty editor tab for first-s3-bkt.yaml.
Example content for the child templates
  • first-s3-bkt.yaml
  • second-s3-bkt.yaml
Notes about child templates
  • Logical IDs (here FirstBucket and SecondBucket) must be valid CloudFormation logical IDs (alphanumeric, no spaces or special characters).
  • Child templates can be simple (as shown) or contain parameters, outputs, and many resource types in real-world usage.
Upload the child templates to S3 and get their object URLs
  • Upload first-s3-bkt.yaml and second-s3-bkt.yaml into your template bucket.
  • Open an object’s details and copy its Amazon S3 object URL (the HTTPS object URL). You will use this full URL in the parent template as TemplateURL.
A screenshot of the AWS S3 console showing the object details page for "first-s3-bkt.yaml" in the bucket "eden-kodekloud-bkt-961." The page shows the object overview (owner, region, S3 URI) and buttons to copy the S3 URI, download, open, or perform object actions.
Create the parent template
  • Create parent.yaml in your project. The parent template declares AWS::CloudFormation::Stack resources pointing to the child template object URLs you uploaded.
Example parent.yaml (replace these example URLs with the exact S3 object URLs you copied):
Upload parent.yaml to the same S3 bucket (so CloudFormation can retrieve it), then create a new CloudFormation stack that uses the parent template’s S3 URL:
  • In the CloudFormation console, choose Create stack -> With new resources (standard).
  • Select Amazon S3 URL as the template source and paste the parent.yaml object URL.
A screenshot of the AWS CloudFormation "Create stack" console showing the Template source options, with "Amazon S3 URL" selected and a field to enter the S3 template URL. The browser header and AWS navigation bar are also visible.
What happens when you create the parent stack
  • CloudFormation reads parent.yaml and provisions two nested stacks as resources: FirstS3Stack and SecondS3Stack.
  • Each nested stack is created independently using the child template referenced by its TemplateURL and will create the S3 bucket described in that template.
  • By default, deleting the parent stack deletes the nested stacks and their resources, unless you specify a DeletionPolicy: Retain or similar retention setting on the nested resources.
Quick reference: files and purpose Callouts and important reminders
Make sure the S3 object URLs you use in TemplateURL are the HTTPS object URLs (for example https://bucket-name.s3.<region>.amazonaws.com/first-s3-bkt.yaml). The child templates must be accessible to CloudFormation and must be in the same region as the parent stack. If your S3 bucket blocks public access, ensure CloudFormation can retrieve the objects (for example by granting s3:GetObject to the CloudFormation service principal or using an appropriate bucket policy/role). If templates are stored in a different account or region, ensure the appropriate permissions and cross-account/cross-region considerations are addressed.
Logical resource names (CloudFormation logical IDs) cannot contain spaces. Use names like FirstBucket or SecondBucket. Also ensure your S3 bucket names for storing templates are unique and that the object URLs are correct before creating the parent stack.
Summary
  • Nested stacks let you modularize and reuse CloudFormation templates while managing them from a single parent stack.
  • Store child templates in S3 and reference them from a parent template using the AWS::CloudFormation::Stack resource and its TemplateURL property.
  • When the parent stack is created, CloudFormation automatically creates the nested stacks and their resources; when the parent is deleted, nested stacks are deleted by default (unless retention policies are applied).
Further reading

Watch Video

Practice Lab