AWS CDK: how do I reference cross-stack resources in same app?

前端 未结 2 585
悲哀的现实
悲哀的现实 2020-12-21 12:08

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 t

2条回答
  •  遥遥无期
    2020-12-21 12:32

    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());
    

提交回复
热议问题