This example shows multiple ways of using values generated by a stack, such as resources' ARNs, in another stack.
You can find the relative blog post at: https://blog.infra.kiwi/aws-cdk-cross-stack-references-54e52e2f8053
- CDK entrypoint: bin/app.ts
- Example stacks:
- Source stack with transparently-generated outputs: lib/source-stack-with-outputs.ts
- Source stack with semi-hardcoded names: lib/source-stack-with-manual-names.ts
- Source stack with SSM parameters: lib/source-stack-with-parameters.ts
- The destinations stack, which supports importing the previous stacks' outputs: lib/destination-stack.ts
Run:
npm install
npm run cdk:deploy:all
You can then see the transparently-generated output of the SourceStackWithOutputs
stack using the following command:
aws cloudformation list-exports --query "Exports[? contains(ExportingStackId,'kiwi-blog-0007-cdk-cross-stack-references-SourceStackWithOutputs')]"
The command should return an output similar to:
[
{
"ExportingStackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/kiwi-blog-0007-cdk-cross-stack-references-SourceStackWithOutputs/53807860-045e-11ef-b963-0e0e787241bd",
"Name": "kiwi-blog-0007-cdk-cross-stack-references-SourceStackWithOutputs:ExportsOutputRefMyTopic868694349D03D60F",
"Value": "arn:aws:sns:us-east-1:123456789012:kiwi-blog-0007-cdk-cross-stack-references-SourceStackWithOutputs-MyTopic86869434-W4oZpBMxbSxU"
}
]
Where Value
contains the ARN of our SNS topic.
Verify the execution by fetching the value of the generated SSM command:
aws ssm get-parameter --name kiwi-blog-0007-cdk-cross-stack-references-SourceStackWithParameters-topic-arn --query 'Parameter.Value' --output text
The command should return an output similar to:
arn:aws:sns:us-east-1:123456789012:kiwi-blog-0007-cdk-cross-stack-references-SourceStackWithParameters-TopicBFC7AF6E-26y1MW6eMial
Where this value is the ARN of our SNS topic.
Then, you can destroy the deployed infrastructure with:
npm run cdk:destroy:all
# Performs a CDK diff against the current deployed environment
npm run cdk:diff
# Runs the CDK deployment
npm run cdk:deploy:all
# Destroys all the CDK deployment resources
npm run cdk:destroy:all
# Shows the synthesized CloudFormation templates
npm run cdk:synth -- kiwi-blog-0007-cdk-cross-stack-references-SourceStackWithOutputs
npm run cdk:synth -- kiwi-blog-0007-cdk-cross-stack-references-DestinationStackFromOutputs
npm run cdk:synth -- kiwi-blog-0007-cdk-cross-stack-references-SourceStackWithManualNames
npm run cdk:synth -- kiwi-blog-0007-cdk-cross-stack-references-DestinationStackFromManualNames
npm run cdk:synth -- kiwi-blog-0007-cdk-cross-stack-references-SourceStackWithParameters
npm run cdk:synth -- kiwi-blog-0007-cdk-cross-stack-references-DestinationStackFromParameters
# Run tests
npm run test
# Run lint
npm run lint