Skip to main content
This guide walks through creating nested stacks in AWS CloudFormation. Nested stacks let you break complex infrastructure into reusable child templates that a parent template references using AWS::CloudFormation::Stack and a TemplateURL. This example demonstrates:
  • Two simple child templates (each creating an S3 bucket)
  • Uploading those templates to S3
  • A parent template that references the children as nested stacks
Prerequisites
  • An AWS account with permissions to create S3 buckets and CloudFormation stacks.
  • Clean up any previous demo resources (existing demo stacks and buckets) to avoid naming or resource conflicts.
Why use nested stacks?
  • Reuse and modularize infrastructure
  • Simplify large templates by splitting them into logical components
  • Improve template maintainability and collaboration
Step 1 — Create an S3 bucket to host templates
  • Create an S3 bucket to store your CloudFormation templates. The bucket name must be globally unique.
  • In this demo the example bucket name is eden-kodekloud-bkt-961.
Screenshot of the AWS S3 "Create bucket" console with the bucket name field filled as "eden-kodekloud-bkt-96" and a button to choose/copy settings from an existing bucket. The lower part of the page shows the "Object Ownership" settings section.
You can accept the default settings for this bucket. It will be used only to host the YAML templates. Optional: Upload via AWS CLI
  • Example upload commands (run from the folder containing your template files):
Step 2 — Create the child templates (two S3 bucket templates) Create two files in your project folder:
  • first-s3-bkt.yaml
  • second-s3-bkt.yaml
Each child template is a minimal CloudFormation template that creates a single S3 bucket. Logical IDs differ between templates to avoid collisions. first-s3-bkt.yaml
second-s3-bkt.yaml
Notes:
  • We deliberately omit explicit BucketName values so CloudFormation generates unique physical names for each bucket.
  • You can extend these templates to include versioning, encryption, lifecycle rules, or other S3 properties.
Template summary Step 3 — Upload the child templates to the S3 bucket
  • Upload first-s3-bkt.yaml and second-s3-bkt.yaml to your eden-kodekloud-bkt-961 bucket (or the unique bucket you created).
  • After upload, open each object in the S3 console and copy the Object URL (the HTTPS URL). Example metadata for first-s3-bkt.yaml:
Copy the HTTPS Object URL for both first-s3-bkt.yaml and second-s3-bkt.yaml. These full URLs are required in the parent template. Step 4 — Create the parent template Create a file called parent.yml (or parent.yaml) that defines two AWS::CloudFormation::Stack resources. Each nested stack’s TemplateURL must be the full HTTPS S3 object URL for the corresponding child template. parent.yml
Important:
  • TemplateURL must include the full HTTPS S3 object URL (including the filename).
  • Templates must be accessible by CloudFormation — typically placing them in an S3 bucket in the same AWS account and region is sufficient.
  • You may also upload the parent template to S3 and point CloudFormation to its S3 URL when creating the stack.
Step 5 — Upload the parent template (optional) and create the parent stack
  • Upload parent.yml to the same S3 bucket if you prefer to reference it from S3 in the CloudFormation console.
  • In the CloudFormation console, choose to create a new stack and specify the Amazon S3 URL of the parent template. CloudFormation will load the parent template and then create the two nested stacks, which create the two S3 buckets.
When specifying the parent template URL in the Create stack dialog, you will use the S3 URL for the parent.yml file:
A screenshot of the AWS CloudFormation "Create stack" page showing an Amazon S3 URL field populated with a parent.yaml S3 link and a "View in Infrastructure Composer" button. The bottom of the page shows "Cancel" and "Next" action buttons.
  • Give the stack a name (for example demo-stack) and proceed.
  • CloudFormation will create the parent stack and then call the two nested stacks (FirstS3Stack and SecondS3Stack) according to the TemplateURL values.
Troubleshooting tips
  • If stack creation fails with a TemplateURL or access error:
    • Verify the S3 object URL is correct and includes the filename.
    • Ensure the objects are in the same AWS Region as the stack (or that CloudFormation can access them).
    • Check S3 bucket/object permissions and block public access settings — CloudFormation needs service access to read the templates.
  • For cross-account or cross-region access, consult AWS docs on permitted access patterns and necessary IAM roles/policies.
Cleanup notes
  • Deleting the parent stack will also delete the nested child stacks and their resources (unless you set DeletionPolicy to retain resources). Remove demo resources when finished to avoid unexpected charges.
Template URLs must be full HTTPS S3 object URLs (including filenames), and the templates should be in a region accessible by CloudFormation (typically the same region as the stack). If CloudFormation cannot access the template object, stack creation will fail.
Additional resources

Watch Video

Practice Lab