Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 791
John Avatar asked May 03 '20 18:05

John


People also ask

How do you reference a resource in CDK?

To satisfy these requirements, you can refer to a resource in one of two ways: By passing a resource defined in your CDK app, either in the same stack or in a different one. By passing a proxy object referencing a resource defined in your AWS account, created from a unique identifier of the resource (such as an ARN)

How do you reference output from another CloudFormation stack?

To create a cross-stack reference, use the export field to flag the value of a resource output for export. Then, use the Fn::ImportValue intrinsic function to import the value in any stack within the same AWS Region and account. AWS CloudFormation identifies exported values by the names specified in the template.

What is Deploymentstack in CDK?

The unit of deployment in the AWS CDK is called a stack. All AWS resources defined within the scope of a stack, either directly or indirectly, are provisioned as a single unit. Because AWS CDK stacks are implemented through AWS CloudFormation stacks, they have the same limitations as in AWS CloudFormation.


1 Answers

CDK's official documentation has a complete example for sharing a S3 bucket between stacks. I copied it below for quicker reference.

/**
 * Stack that defines the bucket
 */
class Producer extends cdk.Stack {
  public readonly myBucket: s3.Bucket;

  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const bucket = new s3.Bucket(this, 'MyBucket', {
      removalPolicy: cdk.RemovalPolicy.DESTROY,
    });
    this.myBucket = bucket;
  }
}

interface ConsumerProps extends cdk.StackProps {
  userBucket: s3.IBucket;
}

/**
 * Stack that consumes the bucket
 */
class Consumer extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props: ConsumerProps) {
    super(scope, id, props);

    const user = new iam.User(this, 'MyUser');
    props.userBucket.grantReadWrite(user);
  }
}

const producer = new Producer(app, 'ProducerStack');
new Consumer(app, 'ConsumerStack', { userBucket: producer.myBucket });
like image 97
Chuck Avatar answered Sep 28 '22 23:09

Chuck