📖 Scenario: You are working on an AWS CloudFormation project where you want to share information between two stacks. For example, one stack creates a VPC and outputs its ID. Another stack needs to use that VPC ID to create resources inside it.
🎯 Goal: Build two CloudFormation stacks where the first stack outputs a value, and the second stack imports that output using cross-stack references.
📋 What You'll Learn
Create a CloudFormation stack named NetworkStack that defines a VPC resource with a specific CIDR block.
Add an output in NetworkStack that exports the VPC ID with the export name MyVPCID.
Create a second CloudFormation stack named ComputeStack that imports the VPC ID using the export name MyVPCID.
Use the imported VPC ID to define a subnet resource inside ComputeStack.
💡 Why This Matters
🌍 Real World
Sharing resource IDs or configuration values between different CloudFormation stacks is common in real AWS projects to keep infrastructure modular and reusable.
💼 Career
Understanding cross-stack references is essential for cloud engineers and architects to design scalable and maintainable AWS infrastructure.
Progress0 / 4 steps
1
Create the NetworkStack with a VPC resource
Create a CloudFormation template for NetworkStack that defines a VPC resource named MyVPC with the CIDR block 10.0.0.0/16.
AWS
Hint
Use the AWS::EC2::VPC resource type and set CidrBlock to 10.0.0.0/16.
2
Add an output exporting the VPC ID
Add an Outputs section to NetworkStack that exports the VPC ID of MyVPC with the export name MyVPCID. Use the intrinsic function !Ref MyVPC for the output value.
AWS
Hint
Use Outputs with a name like VPCID. Set Value to !Ref MyVPC and add an Export with Name: MyVPCID.
3
Create ComputeStack importing the VPC ID
Create a CloudFormation template for ComputeStack that imports the VPC ID using the intrinsic function !ImportValue MyVPCID and assigns it to a parameter named ImportedVPCID.
AWS
Hint
Use a Parameters section with ImportedVPCID of type String. Set Default to !ImportValue MyVPCID.
4
Define a subnet in ComputeStack using the imported VPC ID
Add a subnet resource named MySubnet in ComputeStack that uses the imported VPC ID from the parameter ImportedVPCID as its VpcId. Set the subnet's CIDR block to 10.0.1.0/24.
AWS
Hint
Define a AWS::EC2::Subnet resource named MySubnet. Use !Ref ImportedVPCID for VpcId and set CidrBlock to 10.0.1.0/24.
Practice
(1/5)
1. What is the main purpose of using Outputs in AWS CloudFormation stacks?
easy
A. To share important values like resource IDs between different stacks
B. To delete resources automatically after stack deletion
C. To increase the stack creation speed
D. To encrypt stack resources
Solution
Step 1: Understand Outputs role
Outputs provide a way to expose key information from one stack.
Step 2: Identify sharing purpose
This information can be used by other stacks to reference resources like IDs or names.
Final Answer:
To share important values like resource IDs between different stacks -> Option A
Quick Check:
Outputs share values across stacks = A [OK]
Hint: Outputs share values between stacks, not speed or encryption [OK]
Common Mistakes:
Thinking Outputs delete resources
Believing Outputs speed up stack creation
Confusing Outputs with encryption
2. Which syntax correctly exports an output value named MyBucketName for cross-stack use in CloudFormation?
Hint: Export must be an object with Name key, not a string [OK]
Common Mistakes:
Using ExportName instead of Export with Name
Setting Export as a string instead of object
Incorrect Value reference syntax
3. Given two stacks, StackA exports an output named VpcId. StackB imports it using Fn::ImportValue. What will happen if StackA's export name changes but StackB is not updated?
medium
A. StackB will ignore the import and deploy without errors
B. StackB will automatically update to the new export name
C. StackB will use the old value cached locally
D. StackB deployment will fail because the import value no longer exists
Solution
Step 1: Understand ImportValue dependency
StackB depends on StackA's export name to import the value correctly.
Step 2: Effect of changing export name
If StackA changes the export name, StackB's import reference breaks and causes deployment failure.
Final Answer:
StackB deployment will fail because the import value no longer exists -> Option D
5. You want to share a VPC ID from StackA to StackB using outputs and imports. StackA exports the VPC ID as VpcIdExport. StackB imports it with Fn::ImportValue: VpcIdExport. Later, you need to update StackA to create a new VPC and export its ID with the same name. What is the best practice to avoid deployment failures in StackB?
hard
A. Change the export name in StackA and update StackB to import the new name
B. Delete StackB before updating StackA, then redeploy StackB after update
C. Keep the export name the same and update StackB without changes
D. Remove the export from StackA and hardcode the VPC ID in StackB
Solution
Step 1: Recognize export name immutability
Export names cannot be changed or deleted without affecting dependent stacks.
Step 2: Plan safe update
To update the VPC and export a new ID, use a new export name and update StackB to import that new name.
Step 3: Avoid breaking dependencies
This avoids deployment failures and keeps stacks consistent.
Final Answer:
Change the export name in StackA and update StackB to import the new name -> Option A
Quick Check:
Update export name and import to avoid conflicts = B [OK]
Hint: Change export name and update imports to avoid breaking stacks [OK]
Common Mistakes:
Deleting dependent stacks before update
Keeping export name same causing conflicts
Hardcoding values instead of using imports
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.