Nested Stacks: For Modularity and Reuse
A Nested Stack is a stack that is created as part of another stack. You can think of it as breaking down a large, complex CloudFormation template into smaller, more manageable, and reusable pieces.
What is the Purpose?
The primary purpose of nested stacks is to promote code reuse and simplify template management. As your infrastructure grows, you'll often find yourself redeclaring the same common resources (like a standard VPC, a logging configuration, or a load balancer setup) in multiple templates.
Instead of copying and pasting this code, you can create a dedicated template for that common component and then simply reference it from other "parent" stacks.
How It Works
-
You create a main or "parent" stack.
-
Within the parent template, you use the
AWS::CloudFormation::Stack
resource type. -
The
TemplateURL
property of this resource points to the location of your child template in an Amazon S3 bucket. -
When you launch the parent stack, CloudFormation creates the child stack as one of its resources.
-
You can pass outputs from one nested stack to another, allowing you to link resources together.
When to Use Nested Stacks
-
When you have a common architectural pattern or set of resources that you want to reuse across multiple templates within the same AWS account and region.
-
When you want to break down a single, monolithic CloudFormation template into smaller, logical components for easier management and updating.
-
When you want to give different teams ownership over different parts of a larger architecture.
Key takeaway: Nested Stacks are about composition and modularity within a single deployment.
StackSets: For Multi-Account, Multi-Region Deployments
A StackSet is a management feature that allows you to create, update, or delete CloudFormation stacks across multiple AWS accounts and/or multiple AWS Regions with a single operation.
What is the Purpose?
The primary purpose of StackSets is centralized governance and deployment at scale. It allows a central administrator to roll out a standard set of AWS resources or configurations across an entire organization.
How It Works
-
From a single administrator account, you create a "StackSet" by defining a CloudFormation template.
-
You then specify the target deployment locations:
-
Accounts: You can provide a list of specific AWS account numbers or target an entire Organizational Unit (OU) from AWS Organizations.
-
Regions: You can select one or more AWS Regions where you want the stack to be deployed.
-
-
CloudFormation then creates an independent stack instance in each target account and region combination.
-
Updating the StackSet allows you to push changes to all of the managed stack instances simultaneously.
-
Prerequisite: StackSets require specific IAM roles to be set up in the administrator and target accounts (
AWSCloudFormationStackSetAdministrationRole
andAWSCloudFormationStackSetExecutionRole
) to grant the necessary permissions for cross-account actions.
When to Use StackSets
-
When you need to deploy a baseline set of resources to all new and existing accounts in your AWS Organization (e.g., standard IAM roles, VPCs, logging configurations).
-
When you need to deploy an application or infrastructure to multiple geographic regions for high availability or disaster recovery.
-
When you need to enforce security standards by deploying consistent AWS Config rules or AWS WAF rules across all your accounts.
Key takeaway: StackSets are about distribution and standardization across your entire AWS landscape.
Summary: Key Differences
| Feature | Nested Stacks | StackSets |
| :--- | :--- | :--- |
| Purpose | Code reuse and modularity | Centralized deployment and governance |
| Scope | Single AWS Account, Single Region | Multiple AWS Accounts, Multiple Regions |
| Implementation | AWS::CloudFormation::Stack
resource | A management capability in the console/API |
| Use Case | Building a complex stack from smaller, reusable parts. | Rolling out a standard stack to many places. |
In short, use Nested Stacks to organize a single, complex deployment. Use StackSets to manage multiple, independent deployments across your organization.