Course contentsShow
← Cloud and Infrastructure
Lesson 882 of 2,98916. AWS CloudFormationFree lesson

Cross-Stack References

Export outputs from one stack and import them in another using cross-stack references for modular architecture.

Cross-Stack References

What you'll learn: How to export outputs from one CloudFormation stack and import them in another to build modular, interconnected infrastructure.

The Core Idea

Cross-stack references let you break your infrastructure into separate, manageable stacks while maintaining connections between them. Instead of cramming everything into one giant template, you can create a VPC in one stack, export its ID, and import that ID in another stack that creates EC2 instances—all without hardcoding values.

Think of it like building with LEGO sets: one box contains the foundation pieces, another contains the walls, and another the roof. Each set is self-contained but designed to connect seamlessly with the others.

How It Works

You've already learned Fn::ImportValue in lesson 873—now you'll see it in action with cross-stack architecture:

Stack A (Network Stack) exports a value:

Outputs:
  VPCId:
    Value: !Ref MyVPC
    Export:
      Name: NetworkStack-VPC-ID

Stack B (Application Stack) imports it:

Resources:
  WebServer:
    Type: AWS::EC2::Instance
    Properties:
      SubnetId: !ImportValue NetworkStack-Subnet-ID
      # ... other properties

Why This Matters

Cross-stack references enable modular architecture. You can update your network stack independently of your application stack, reuse foundational infrastructure across multiple application stacks, and organize resources by lifecycle (persistent databases vs ephemeral test environments).

The key constraint: you cannot delete a stack if another stack imports its outputs. This dependency creates safety—you won't accidentally delete infrastructure others rely on.

Key Takeaway: Cross-stack references use Export in Outputs and Fn::ImportValue to share resource identifiers between stacks, enabling modular, maintainable infrastructure design without tight coupling.