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

前端 未结 2 581
悲哀的现实
悲哀的现实 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:30

    Option 1:

    pass the data from Stack A to Stack B using the constructor :

    You can extend cdk.stack and create a new class that will contain stackA.

    In that stack, expose the relevant data you want by using public XXX: string\number (etc) ( See line 2 in the example).

    Later, just pass this data into StackB constructor ( you can pass it using props as well).

    Working code snippet:

    Stack A:

        export class StackA extends cdk.Stack {
            public YourKey: KEY_TYPE;
        
            constructor(scope: cdk.Construct, id: string, props: cdk.StackProps ) {
                super(scope, id, props);
        
                Code goes here...
        
                // Output the key 
                new cdk.CfnOutput(this, 'KEY', { value: this.YourKey });
        
            }
        }
    

    Stack B:

    export class StackB extends cdk.Stack {
        constructor(scope: cdk.Construct, id: string,importedKey: KEY_TYPE, props: cdk.props) {
            super(scope, id, props)
    
            Code goes here...
            
            console.log(importedKey)
    
        }
    }
    

    bin ts:

    const importedKey = new StackA(app, 'id',props).YourKey;
    new StackB(app, 'id',importedKey,props);
    

    Option 2:

    Sometimes it's just better to save this kind of stuff in the parameter store and read it from there.

    More info here.

    0 讨论(0)
  • 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());
    
    0 讨论(0)
提交回复
热议问题