Skip to main content
Welcome. This lesson explains how to modularize AWS CloudFormation templates so teams can work independently while keeping resources consistent and reusable across accounts and Regions. The goal is to move away from one large monolithic template and instead split infrastructure into smaller, focused templates—networking, compute, IAM, storage, etc.—that teams can develop, version, and deploy independently. To illustrate, imagine a car factory: the chassis (frame) is standard, engines have a few options, and interiors vary widely. Different teams design each component, but the parts must fit together during assembly. Modular CloudFormation promotes the same separation of concerns: independent teams own different stacks while referencing shared resources as needed. Consider a typical modular layout: networking, compute, and security. Instead of a single massive template for everything, break it into smaller, reusable templates so changes are scoped and deployments are faster.
A diagram titled "Modularization in AWS CloudFormation" showing an AWS Cloud layout with a Virtual Private Cloud spanning two Availability Zones. It illustrates NAT Gateways, Amazon EC2 Auto Scaling groups and instances, with side labels for Network and Compute.
If you modularize, you might create:
  • a networking template (VPC, subnets, route tables),
  • a compute template (Auto Scaling groups, Launch Templates), and
  • an IAM template (roles, policies).
Each template becomes easier to maintain and faster to deploy. When a shared template (such as IAM or networking) is updated, all consumers can pick up the change without duplicating definitions.
Slide titled "Modularization in AWS CloudFormation" showing a large pink CloudFormation/template icon on the left with an arrow pointing to three smaller template icons on the right, each paired with a small cloud/stack badge.
Key benefits of modularization include reusability, maintainability, faster deployments, improved scalability, and better team collaboration—each team can depend on well-defined templates rather than editing a monolith.
Slide titled "Why Modularize CloudFormation Templates?" showing five colored icons labeled Reusability, Maintainability, Faster deployments, Scalability, and Team collaboration. Each benefit is represented by a circular icon and brief caption.
Modularization techniques There are three common approaches to organize modular CloudFormation:
  • Nested Stacks — include one stack inside another to compose hierarchical templates.
  • StackSets — deploy a template consistently across multiple AWS accounts and Regions.
  • Cross-Stack References — export outputs from one stack and import them into other stacks to avoid duplication.
Slide titled "CloudFormation – Modularization Techniques" showing three numbered items: Nested Stacks, Stack Sets, and Cross-Stack References, each with a short description of how they organize or share CloudFormation resources. The items are shown as colored arrows with brief explanatory text.
Comparison: When to use which technique Nested stacks
  • A nested stack is a CloudFormation stack declared inside another stack. Use nested stacks to separate concerns into child stacks for networking, security, shared services, etc.
  • Use nested stacks when you need tight coupling or hierarchical organization—e.g., an IAM child stack that provides roles referenced by sibling components.
  • Benefits: reusability, simplified management, and logical hierarchy. Changes to a nested stack are scoped and less likely to break unrelated resources.
A slide titled "Nested Stacks – Benefits" showing a parent stack at the top connected to two child stacks below, with three benefit bullets on the right: Reusability, Simplifies management, and Enables hierarchical organization.
StackSets
  • StackSets let you deploy a single CloudFormation template (a StackSet) to multiple target accounts and Regions, creating stack instances in each target.
  • A stack instance represents the template in a target account/Region. If creation fails in a target, the instance may be recorded without a fully created stack.
  • Use StackSets for multi-account, multi-Region governance—e.g., replicate a standardized networking stack across all accounts and Regions.
  • Note: StackSets require appropriate administrator permissions/roles and trust in target accounts. You can choose between the service-managed or self-managed permissions models.
A diagram titled "Stack Sets" showing an administrator account creating a Stack Set that is deployed as stacks into multiple target accounts across two regions, with a labeled "Regional Resource" connection.
When you update or delete a StackSet, the operation applies to stack instances and then to the actual stacks in target accounts/Regions. Think of a StackSet as the controller that creates and manages stack instances across targets.
A slide titled "Stack Instance" showing a Stack Set branching to Create, Update and Delete operations that produce multiple Stack instances. The Create/Update boxes show rows of stacks while the Delete box shows stacks marked with red Xs to indicate removal.
Cross-stack references
  • Use exports and imports to share resources across separate (non-nested) stacks. For example, a networking stack can export VPC IDs and subnet IDs; database or application stacks import those outputs rather than recreate or hard-code them.
  • Benefits: reduces duplication, ensures consistency, and improves scalability. When an exported resource changes (for example, a VPC ID), importing stacks see the new value when they are created or explicitly updated — changes do not automatically propagate to existing stacks.
  • Note: CloudFormation exports/imports are limited to stacks within the same AWS account and Region. To share values across accounts or Regions, use StackSets or other cross-account mechanisms.
A slide titled "Cross-Stack References – Benefits" showing a VPC Stack exporting an output to a central "Output" box which is then imported by a Database Stack. The bottom lists three benefits: reduces duplication, ensures consistency, and improves scalability.
Quick examples: exporting and importing values Example: export a VPC ID from a network stack (YAML):
Example: import the exported VPC ID into an application stack (YAML):
Best practices and constraints
  • Use clear, consistent, and descriptive export names. Choose a naming convention (for example, project-env-resource) and apply it consistently so teams can find and import the correct outputs.
  • Track exports centrally (documentation, inventory, or a registry) so teams know which outputs are available and where they originate.
  • Avoid circular dependencies. Do not make Stack A depend on Stack B while Stack B depends on Stack A; such cycles block deployments and deletions.
  • Keep individual templates manageable—aim for a reasonable resource count (for many teams, keeping templates under ~500 resources helps avoid long processing times and complexity).
  • Store templates and artifacts in source control (Git) or centralized artifact storage (Amazon S3) for versioning and consistent access.
Export names must be unique within the same AWS account and Region. If you change an export name, remove all imports that reference it first—CloudFormation does not allow renaming an exported value while it is still imported by other stacks.
Operational tips
  • Use nested stacks when resources are tightly coupled and you want a clear parent/child lifecycle.
  • Use cross-stack references when consumers are loosely coupled but need shared values (IDs, ARNs).
  • Use StackSets for multi-account, cross-Region replication of agreed patterns—this supports governance at scale.
  • Keep a registry or README that documents available exports, their naming conventions, owners, and update procedures.
Stack deletions will fail if another stack is still importing an exported value. Before deleting or renaming exported outputs, remove or update all importing stacks. Avoid long chains of dependencies that become difficult to delete or update.
Limitations and considerations
  • Export names must be unique within the same AWS account and Region—track them carefully.
  • You cannot update an export name while it is being imported; you must remove imports first.
  • Deleting a stack that is referenced by others will fail until references are removed.
A presentation slide titled "Best Practices" with a numbered list of five items. The tips cover export and stack handling: use clear/consistent export names, manage exports centrally, avoid circular dependencies, use nested stacks for tight coupling, and use StackSets for cross-region/multi-account scenarios.
A presentation slide titled "Limitations and Considerations" that lists three points about exports and stack deletions, each paired with a green check icon. The points state that export names must be globally unique within a region; export names can't be updated without deleting references; and stack deletions fail if another stack is still importing a value.
Summary
  • Modularization via nested stacks, StackSets, and cross-stack references helps teams build consistent, reusable, and maintainable infrastructure.
  • Choose nested stacks for tight coupling and hierarchy, StackSets for multi-account/Region replication, and cross-stack references to share outputs without duplication.
  • Track exports, avoid circular dependencies, and plan deletion/rename workflows before changing exported values.
Further reading and references This article included diagrams and practical examples showing how to create and update StackSets, and how to export/import values between stacks for reusable, modular infrastructure.

Watch Video