简体   繁体   中英

AWS-CDK: Passing cross-stack references props between multi region (cross-region) stacks in AWS- CDK

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 :

  • 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 :

  • 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?

Thanks in advance

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM