Get started with Nested Stacks in AWS CloudFormation

Nested stacks in AWS CloudFormation are a feature that allows you to create a hierarchy or composition of CloudFormation templates. It enables you to break down a complex infrastructure deployment into smaller, more manageable units called stacks. These stacks can be created and managed independently while being interconnected through a parent-child relationship.

At a high level, a nested stack is a CloudFormation stack that is created as a resource within another CloudFormation template, referred to as the parent stack. The parent stack contains a resource type called AWS::CloudFormation::Stack, which defines the nested stack. The nested stack, in turn, can contain its own set of resources, parameters, outputs, and so on, defined in a separate CloudFormation template.

By utilizing nested stacks, you can modularize your infrastructure deployments and reuse templates across multiple projects or environments. This approach promotes code reuse, simplifies management, and allows for better organization and separation of concerns.

Here's a step-by-step process of working with nested stacks in AWS CloudFormation:

  1. Create a separate CloudFormation template for the nested stack. This template represents a self-contained unit of infrastructure that can be deployed independently.

  2. In the parent stack's CloudFormation template, define a resource of type AWS::CloudFormation::Stack. Specify the template URL or inline definition for the nested stack, along with any input parameters required.

  3. During stack creation/update of the parent stack, CloudFormation provisions the nested stack as a resource. It deploys the nested stack using the specified template and passes the required input parameters.

  4. The nested stack is created, updated, or deleted as per the instructions defined within its template. It can have its own set of resources, parameters, outputs, and other CloudFormation constructs.

  5. The parent stack can reference outputs from the nested stack to retrieve information or pass it to other resources within the parent stack or other nested stacks.

Some key benefits of using nested stacks are:

  1. Modularity and reusability: By breaking down infrastructure into smaller, modular templates, you can reuse them across different projects or environments. This promotes consistency and reduces duplication of effort.

  2. Separation of concerns: Nested stacks allow you to manage different aspects of infrastructure separately. Each stack can focus on a specific set of resources or configuration, making it easier to understand and maintain.

  3. Parallel deployment: Since each nested stack operates independently, CloudFormation can deploy them in parallel, reducing the overall deployment time.

  4. Granular updates: When a change occurs in a nested stack, CloudFormation can update only that particular stack, avoiding the need to update the entire parent stack. This can help minimize disruption and improve update speed.

In summary, nested stacks in AWS CloudFormation provide a powerful mechanism for structuring and managing complex infrastructure deployments by breaking them down into smaller, reusable units. They offer improved modularity, reusability, and manageability, enabling you to efficiently create and maintain AWS resources and applications.

Hands-on Lab overview

In this hands-on lab we create parent and nested stacks. The nested stack deploys EC2 instance and private S3 bucket.

  1. Upload the nested stack template (nested-stack.yaml) to an S3 bucket or any accessible location and note down the template URL.

         Type: AWS::EC2::Instance
           InstanceType: t2.micro
           ImageId: ami-06b09bfacae1453cb  # Replace with your desired AMI ID
         Type: AWS::S3::Bucket
           BucketName: my-bucket-<account-id>
           AccessControl: Private
         Value: !Ref MyResource1
         Value: !Ref MyResource2

  2. Update the TemplateURL property in the parent stack template (parent-stack.yaml) with the URL of the nested stack template.

         Type: AWS::CloudFormation::Stack
           TemplateURL: <TemplateURL>
         Value: !GetAtt MyNestedStack.Outputs.NestedStackOutput1
         Value: !GetAtt MyNestedStack.Outputs.NestedStackOutput2

  3. Deploy the parent stack using the AWS CloudFormation service, either through the AWS Management Console:

During the stack creation process, the parent stack will create the nested stack and pass the required input parameters. Once the stack creation is complete, you can view the outputs of the nested stack, such as the references to the EC2 instance (MyResource1) and S3 bucket (MyResource2), by examining the parent stack's outputs.

  1. Optionally: To deploy the stack via AWS CLI use:

     aws cloudformation create-stack --stack-name your-stack-name --template-body file://parent-stack.yaml


  1. Working with nested stacks

  2. AWS CloudFormation StackSets and Nested Stacks TutorialsDojo

  3. Nested stacks Workshop