Sub topic 17
Sharing values between stacks
This chapter assumes you can write a template with Parameters, Resources and Outputs, and that you can create and update stacks from the AWS CLI. It covers how one stack exports a value and how other stacks import it. It also covers the dependency this creates and the errors it causes.
Why share values
Large systems rarely fit in one stack. A network stack creates a VPC and subnets. An application stack needs those subnet IDs. A storage stack creates a bucket that several services read. Separate stacks let each part change on its own schedule and be owned by a different team. They also need a way to pass values to each other.
CloudFormation offers a few ways to do this:
- Copy the value into a parameter by hand or from a script.
- Look the value up at deploy time, for example with an SSM parameter type.
- Export the value from one stack and import it into another.
- Pass values from a parent template to a nested stack.
This chapter covers exports and imports. Nested stacks are a separate chapter.
Exporting a value
An export is an output with an Export block. The export name is the key other stacks use to find the value.
Outputs:
BucketArn:
Description: ARN of the application bucket
Value: !GetAtt AppBucket.Arn
Export:
Name: !Sub "${AWS::StackName}-BucketArn"If this output sits in a stack named demo, it exports a value named demo-BucketArn. Building the name from AWS::StackName is a common convention. It makes names unique when you deploy the same template several times under different stack names.
Export rules to remember:
- Export names must be unique within one account and Region. Creating a second export with an existing name fails.
- Exports are scoped to a Region. A stack in
us-east-1cannot import fromeu-west-1. - The value is resolved when the exporting stack is created or updated. The importing stack reads the current value.
- Exports are plain text. Do not export secrets.
Importing a value
Another stack reads the export with Fn::ImportValue. The short form is !ImportValue.
AWSTemplateFormatVersion: "2010-09-09"
Description: Consumer stack that imports the bucket ARN exported by the stack named demo
Resources:
ReaderPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: s3:GetObject
Resource: !Join
- ""
- - !ImportValue demo-BucketArn
- "/*"Several details matter here:
ImportValuetakes the export name, not the stack name or the output logical ID.- The export name can be built with
Fn::SuborFn::Join, but it cannot depend on a resource attribute of the importing stack. It can use parameters and pseudo parameters, so you can pass the exporting stack's name as a parameter. - You cannot use
ReforFn::GetAttto read another stack's output. OnlyImportValuereaches across stacks. ImportValueis not available inside a nested stack's parameters in the same way as in a top-level stack, so check the documentation before combining the two.
Parameterizing the export name
Hard-coding demo-BucketArn ties the consumer to one stack. A parameter removes that coupling.
Parameters:
NetworkStackName:
Type: String
Default: demo
Resources:
ReaderPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: s3:GetObject
Resource: !Sub
- "${Arn}/*"
- Arn:
Fn::ImportValue: !Sub "${NetworkStackName}-BucketArn"Here Fn::Sub with a variable map receives the imported value as Arn. The long form of ImportValue is required because !ImportValue !Sub cannot be nested as short-form tags in older YAML parsers.
The dependency an import creates
Once a stack imports a value, CloudFormation records the dependency. While any stack imports an export:
- The exporting stack cannot be deleted.
- The exported output cannot be removed or renamed.
- The exported value cannot change. An update that would change it fails.
This protects consumers from a value disappearing under them. It also means the exporting stack is harder to change than it looks. If you need to replace the bucket, you must first update every consumer to stop importing the export.
Deleting is blocked, not queued.You can see who depends on an export from the CLI:
# List all exports in the current Region
aws cloudformation list-exports
# List stacks that import a specific export
aws cloudformation list-imports --export-name demo-BucketArnlist-imports returns an error if nothing imports the export. Treat that error as "no importers" rather than as a failure when you script it.
Trying to delete the exporting stack while a consumer exists produces a message like this in the stack events:
Export demo-BucketArn cannot be deleted as it is in use by consumer-stackThe stack returns to a stable state, usually DELETE_FAILED for a delete. Delete or update the consumer first, then delete the exporter.
Order of operations
The order you create and delete stacks follows the import direction.
Creating the consumer before the exporter fails with a validation error that the export does not exist. A common scripting mistake is to run both deployments in parallel.
Choosing between exports and other options
| Approach | Coupling | Good for | Drawback |
|---|---|---|---|
| Export and ImportValue | Tight, enforced by CloudFormation | Stable values such as VPC IDs | Blocks changes to the exported value |
| Parameter filled by a script | Loose | Values that change often | You must supply the value on each deploy |
| SSM Parameter Store type parameter | Loose, resolved at deploy time | Values shared with non-CloudFormation tools | No dependency tracking |
| Nested stack | Parent owns child | One deployable unit split into pieces | Stacks are updated together |
Exports suit values that rarely change and that you want protected. If a value changes often, such as an AMI ID, exports will keep blocking updates. Use a parameter or Parameter Store instead.
Common errors
- No export named X found. The exporter does not exist yet in this Region, or the name is misspelled. Check with
aws cloudformation list-exports. - Export with name X is already exported by stack Y. Two stacks use the same export name. Include
AWS::StackNamein the name. - Cannot update export as it is in use by another stack. You changed the exported value while a consumer imports it. Update the consumers first, or give the new value a new export name.
- Wrong Region. Both stacks must be in the same account and Region.
Exercise
Stack net exports net-SubnetId. Stack app imports it. You want to replace the subnet in net with a new one, which changes the exported value. What fails if you update net directly, and what is the safe sequence?
Show answer
The update to net fails because an export that another stack imports cannot change. Safe sequence: add a new export with a new name from net (for example net-SubnetIdV2), update app to import the new name, then remove the old export from net once list-imports shows no importers.
Exercise
You run aws cloudformation delete-stack --stack-name demo and it fails. list-imports --export-name demo-BucketArn shows consumer-stack. What do you do?
Show answer
Delete consumer-stack, or update it so it no longer imports demo-BucketArn. Then delete demo.