Skip to content

Files

Latest commit

9135d6d · Mar 15, 2025

History

History

0007-cdk-cross-stack-references

kiwi-blog-0007-cdk-cross-stack-references

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

Important files

Usage

Run:

npm install
npm run cdk:deploy:all

Transparently-generated outputs

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.

SSM parameters

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.

Destroy

Then, you can destroy the deployed infrastructure with:

npm run cdk:destroy:all

Useful commands

CDK commands

# 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

JS commands

# Run tests
npm run test

# Run lint
npm run lint