> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Nested Stacks in CloudFormation

> Guide demonstrating how to build AWS CloudFormation nested stacks by hosting child templates in S3 and creating a parent template referencing them to provision reusable S3 buckets

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.](https://mintcdn.com/kodekloud-c4ac6d9a/5BuH6-_lHDMy3nuh/images/AWS-Certified-DevOps-Engineer-Professional/Domain-2-Configuration-Management-and-IaC/Demo-Nested-Stacks-in-CloudFormation/s3-create-bucket-eden-bkt96-ownership.jpg?fit=max\&auto=format\&n=5BuH6-_lHDMy3nuh\&q=85\&s=48a463f0ab78f8f774b01e2fc3a0af3e)

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):

```bash theme={null}
aws s3 cp first-s3-bkt.yaml s3://eden-kodekloud-bkt-961/first-s3-bkt.yaml
aws s3 cp second-s3-bkt.yaml s3://eden-kodekloud-bkt-961/second-s3-bkt.yaml
```

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

```yaml theme={null}
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  FirstBucket:
    Type: AWS::S3::Bucket
    Properties:
      Tags:
        - Key: Author
          Value: Arno
```

second-s3-bkt.yaml

```yaml theme={null}
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  SecondBucket:
    Type: AWS::S3::Bucket
    Properties:
      Tags:
        - Key: Author
          Value: Arno
```

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

| File | Purpose | Key resource |
| - | - | - |
| `first-s3-bkt.yaml` | Child template that provisions the first S3 bucket | `FirstBucket` (`AWS::S3::Bucket`) |
| `second-s3-bkt.yaml` | Child template that provisions the second S3 bucket | `SecondBucket` (`AWS::S3::Bucket`) |

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`:

```text theme={null}
Amazon Resource Name (ARN)
arn:aws:s3:::eden-kodekloud-bkt-961/first-s3-bkt.yaml

Object URL
https://eden-kodekloud-bkt-961.s3.us-east-2.amazonaws.com/first-s3-bkt.yaml

AWS Region
US East (Ohio) us-east-2

Key
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

```yaml theme={null}
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  FirstS3Stack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://eden-kodekloud-bkt-961.s3.us-east-2.amazonaws.com/first-s3-bkt.yaml

  SecondS3Stack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://eden-kodekloud-bkt-961.s3.us-east-2.amazonaws.com/second-s3-bkt.yaml
```

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.](https://mintcdn.com/kodekloud-c4ac6d9a/5BuH6-_lHDMy3nuh/images/AWS-Certified-DevOps-Engineer-Professional/Domain-2-Configuration-Management-and-IaC/Demo-Nested-Stacks-in-CloudFormation/cloudformation-create-stack-s3-parent-yaml.jpg?fit=max\&auto=format\&n=5BuH6-_lHDMy3nuh\&q=85\&s=92f1929745c6c1313f75ae4f27af716e)

* 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

* [AWS CloudFormation User Guide](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)
* [Amazon S3 Documentation](https://docs.aws.amazon.com/s3/index.html)
* [Working with Nested Stacks](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-nested-stacks.html)

> **[Watch Video](https://learn.kodekloud.com/user/courses/aws-certified-devops-engineer-professional/module/42510d3c-bacc-4a37-adb8-2cfea86f1a36/lesson/2348a349-8d4e-4b4a-b841-0bfee107fb8f)**

> **[Practice Lab](https://learn.kodekloud.com/user/courses/aws-certified-devops-engineer-professional/module/42510d3c-bacc-4a37-adb8-2cfea86f1a36/lesson/f7eed052-343b-4a04-b993-e8897174da0f)**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.