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

John picture John · May 3, 2020 · Viewed 7.8k times · Source

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

How do pass the ARN into StackB?

Answer

Abhinaya picture Abhinaya · May 3, 2020

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