简体   繁体   English

AWS-CDK:在 AWS-CDK 中的多区域(跨区域)堆栈之间传递跨堆栈引用道具

[英]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.具体来说,它需要一个 ARN 值。 The default region is us-east-1 .默认区域是us-east-1 That is where the parent stack will get deployed.这就是父堆栈将被部署的地方。

To solve this I attempted the following为了解决这个问题,我尝试了以下

1- First Attempt : 1-第一次尝试

  • Created a cfnOutput in the parent and in the child I capture the value with cdk.Fn.ImportValue()在父级和子级中创建了一个cfnOutput我使用cdk.Fn.ImportValue()捕获值
  • RESULT : Got an error as cfnOutput can not be used between stacks on different regions as explained in CloudFormation User Guide结果:出现错误,因为 cfnOutput 无法在不同区域的堆栈之间使用,如 CloudFormation 用户指南中所述

2- Second Attempt : 2-第二次尝试

  • Created an interface in the parent stack that inherit from StackProps, set a public property and put the ARN value there在从 StackProps 继承的父堆栈中创建一个接口,设置一个公共属性并将 ARN 值放在那里

from the lib/mystack file来自 lib/mystack 文件

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:将值与包含区域的 env 键一起作为道具传递到第二个堆栈,如下所示:

from the bin/myapp file来自 bin/myapp 文件

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?如何解决在 CDK 中使用不同区域的堆栈之间传递跨堆栈引用的问题?

Thanks in advance提前致谢

Use a Parameter Store value with a CustomResource .将 Parameter Store 值与CustomResource一起使用。

This answer has a full Typescript CDK example of cross-region refs. 这个答案有一个完整的 Typescript CDK 跨区域参考示例。

(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). (我最初将此作为评论发布,因为我认为这个问题可能是重复的。但经过反思,我发现链接的问题标签只提到 CloudFormation,而不是 CDK。似乎社区从保持这个问题的活力中获得了最大的好处)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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