Sub topic 13
Sharing values across stacks
This chapter assumes you can already define a stack with a bucket and a function, and that you know tokens resolve at deploy time. It covers how one stack uses a resource that another stack owns.
How CDK shares values
You don't write export code. You pass a construct, or one of its properties, from one stack into another. During synthesis the CDK sees that a resource in the consumer stack refers to a resource in the producer stack. It then does three things:
- Adds an
Exportto the producer stack's template outputs. - Replaces the reference in the consumer stack with
Fn::ImportValue. - Records a dependency, so the producer deploys first.
Example
The producer stack exposes the bucket as a public property. The consumer receives it through its props, typed as the interface s3.IBucket.
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as lambda from 'aws-cdk-lib/aws-lambda';
export class StorageStack extends cdk.Stack {
public readonly bucket: s3.Bucket;
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
this.bucket = new s3.Bucket(this, 'UploadsBucket', { versioned: true });
}
}
interface ProcessingStackProps extends cdk.StackProps {
readonly bucket: s3.IBucket;
}
export class ProcessingStack extends cdk.Stack {
constructor(scope: Construct, id: string, props: ProcessingStackProps) {
super(scope, id, props);
const fn = new lambda.Function(this, 'Processor', {
runtime: lambda.Runtime.NODEJS_20_X,
handler: 'index.handler',
code: lambda.Code.fromAsset('lambda'),
environment: { BUCKET_NAME: props.bucket.bucketName },
});
props.bucket.grantRead(fn);
}
}
const app = new cdk.App();
const env = {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION,
};
const storage = new StorageStack(app, 'StorageStack', { env });
new ProcessingStack(app, 'ProcessingStack', { env, bucket: storage.bucket });Running cdk synth produces an export in StorageStack and an Fn::ImportValue in ProcessingStack. The grant is added to the function's role, which lives in the consumer stack.
Common errors
- Deadly embrace. CloudFormation refuses to change or remove an export while another stack imports it. To drop the reference, first deploy the consumer without it, then update the producer.
- Cyclic reference. If stack A uses a value from B and B uses one from A, synthesis fails. Move the shared resources into a third stack, or merge the two stacks.
- Different environments. An export only works inside one account and region. For cross-region references, set
crossRegionReferences: trueon both stacks. You can also publish the value to an SSM parameter and read it in the other stack. - Resource names that change. An export holds a value that CloudFormation generates. If the producer resource is replaced, the export value changes too.
When to avoid cross-stack references
If two resources always change together, keep them in one stack. For a resource you don't manage in the app, use a lookup such as s3.Bucket.fromBucketName. This creates no export and no dependency.
Exercise
You remove the bucket prop from ProcessingStack and deploy both stacks in one cdk deploy --all. The deploy fails because the export is still in use. Why, and what is the safe order?
Show answer
CloudFormation can't delete an export while a deployed stack imports it. Because the consumer no longer references the bucket, the CDK also drops the export from the producer. But the producer may be updated first, while the consumer still imports the value. The safe order is to deploy the consumer without the reference first, then deploy the producer so it drops the export. Alternatively, keep the export alive temporarily with stack.exportValue(bucket.bucketName) in the producer.