AWS CDK:如何在同一应用程序中引用跨栈资源? [英] AWS CDK: how do I reference cross-stack resources in same app?
问题描述
我有一个具有两个堆栈的应用程序,它们都位于同一地区/帐户内。这些堆栈中的一个需要另一个堆栈中存在的lambda的ARN。我该如何引用呢?
I have an App that has two stacks, both within the same region/account. One of those stacks requires the ARN of a lambda that exists in the other stack. How do I reference this?
// within stackA constructor
public StackA(Construct scope, String id, StackProps props) {
SingletonFunction myLambda = SingletonFunction.Builder.create(this, "myLambda")
// some code here
.build()
CfnOutput myLambdaArn = CfnOutput.Builder.create(this, "myLambdaArn")
.exportName("myLambdaArn")
.description("ARN of the lambda that I want to use in StackB")
.value(myLambda.getFunctionArn())
.build();
}
App app = new App();
Stack stackA = new StackA(app, "stackA", someAProps);
Stack stackB = new StackB(app, "stackB", someBProps);
stackB.dependsOn(stackA);
如何将ARN传递到StackB?
How do pass the ARN into StackB?
推荐答案
您可以访问其他堆栈中的资源,只要它们位于同一帐户和AWS区域中即可。以下示例定义了堆栈stack1,该堆栈定义了Amazon S3存储桶。然后,它定义第二个堆栈stack2,它将堆栈1中的存储桶作为构造函数属性。
You can access resources in a different stack, as long as they are in the same account and AWS Region. The following example defines the stack stack1, which defines an Amazon S3 bucket. Then it defines a second stack, stack2, which takes the bucket from stack1 as a constructor property.
// Helper method to build an environment
static Environment makeEnv(String account, String region) {
return Environment.builder().account(account).region(region)
.build();
}
App app = new App();
Environment prod = makeEnv("123456789012", "us-east-1");
StackThatProvidesABucket stack1 = new StackThatProvidesABucket(app, "Stack1",
StackProps.builder().env(prod).build());
// stack2 will take an argument "bucket"
StackThatExpectsABucket stack2 = new StackThatExpectsABucket(app, "Stack,",
StackProps.builder().env(prod).build(), stack1.getBucket());
这篇关于AWS CDK:如何在同一应用程序中引用跨栈资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!