15

I have to deploy one stack, let's call it the parent stack in one region Them a second stack(child) needs to be deployed, in another region. The region of the second stack(child stack) can not include the region where the parent was deployed. The second stack can be deployed in multiple regions.

However, the second stack needs props from the first stack. Specifically, it needs an ARN value. The default region is us-east-1. That is where the parent stack will get deployed.

To solve this I attempted the following

1- First Attempt : Using cfnOutput

  • Created a cfnOutput in the parent and in the child I capture the value with cdk.Fn.ImportValue()
  • RESULT: Got an error as cfnOutput can not be used between stacks on different regions as explained in CloudFormation User Guide

2- Second Attempt: Using StackProps

  • Created an interface in the parent stack that inherit from StackProps, set a public property and put the ARN value there

from the lib/mystack file

export interface myStackProps extends cdk.StackProps {
 principalKeyArn: string
}
  • Passed the value to the second stack as props along with the env key containing the region as under:

from the bin/myapp file

const app = new cdk.App();
const regions = ["us-east-2"]

const primaryMRKey = new KmsMultiregionPrincipalKey(app, 'KmsMultiregionKeyStack')

for (let region of regions){
 const envToDeploy = {region: region, account: "123456789123"}
 new  KmsReplicaKey(app, 'KmsReplicaKey-' + region, {env: envToDeploy, principalKeyArn: primaryMRKey.principalKeyArn  } )

}

RESULT: Cross stack references are only supported for stacks deployed to the same environment or between nested stacks and their parent stack

Question:

  • How to resolve the issue of passing cross-stack references between stacks that are using different regions in CDK?

[Edited] one solution to this problem is using SSM as explained below.

Thanks in advance

2
  • 2
    Use a Parameter Store value with a CustomResource. This answer has a full Typescript CDK example of cross-region refs.
    – fedonev
    Commented Nov 25, 2021 at 16:39
  • 1
    The above link is a correct approach and I did implemented the solution using that link. Just for clarity, I will post a response shortly. Thanks to @fedonev Commented Nov 27, 2021 at 3:52

2 Answers 2

13

CDK now has an override to allow cross-region references between stacks in the same app.

Set the parameter crossRegionReferences to true in both stacks.

For example stack 1 with a global CloudFront WAF:

new WafStack(app, 'waf', {
  env: { region: 'us-east-1'},
  crossRegionReferences: true // <-- Enable cross region references
})

Do the same for the other stacks.

There is an example in this issue: https://github.com/aws/aws-cdk/issues/22820

3
  • Thanks. This is what I needed. Also make sure you have to put env with region in all your stacks.
    – Shivam
    Commented May 14, 2023 at 11:32
  • I can confirm this worked for me. Here's a good illustration how it would look like in code: twitter.com/pahudnet/status/1587522689740046336 Commented Feb 7 at 15:46
  • According to the documentation, "crossRegionReferences" is currently experimental. See: github.com/aws/aws-cdk/tree/… I used "crossRegionReferences" in my project, and it works. However, since it is still experimental, I don’t want to depend on it for production. Instead, I will use Parameter Store and Custom Resource methods. Be careful when using it in your projects.
    – Lucas
    Commented Jul 11 at 19:46
10

Use a Parameter Store value with a CustomResource.

This answer has a full Typescript CDK example of cross-region refs.

(I originally posted this as a comment because I thought the question was perhaps a duplicate. But on reflection, I see that the linked question and tags only mention CloudFormation, not the CDK. Seems the community gets the most benefit from keeping this question alive).

1
  • 1
    Yes to clarify what is done in the above link: In the parent Stack, you build an SSM resource and write to it the ARN. Inside the child Stack, you create an SSM reader and read the value. No props are passed . An auxiliary class is created as a helper to read the SSM from the child Commented Nov 27, 2021 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.