reference resources between CDK stacks
Hi Team,
I have 2 CDK stacks one in the Front end project and the second in the Backend project.
the first stack needs resources ref from the second stack (and vice versa can be true)
I have a lambda function in the backend stack that needs to call another lambda in the front end stack,
If I deploy the backend CDK stack first, how can I make a reference/call to the second lambda in the front end stack?
If I use event bridge or SNS I still need to have the name of the target lambda which is the lambda created by the CDK front end or I need the SNS ref to be shared between the 2 stacks for pub/sub.
any idea how to solve this :)
Thank you.
CloudFormation supports the concept of exporting outputs from one stack and importing their values into another stack.
In CDK, you can export a resource's output by creating a cfnOutput
with an exportName
property. You can also use Fn.ImportValue
to import an exported value to a stack from a different stack.
Here's a blog post that illustrates this concept in further detail.
Note that neither CDK nor CloudFormation support circular stack dependencies, so you can't have stack A depend on a resource in stack B while also having stack B depend on a resource in stack A. The stack dependency graph must be acyclical.
CDK can handle export/import of the resource variable automatically.
If you make a resource a member variable of the stack instance, and reference that stack member variable from the other stack by passing the variable as a property of stack props parameter, CDK will export the resource reference, and import it from the other stack automatically.
You can find more detailed information from https://docs.aws.amazon.com/cdk/v2/guide/resources.html#resource_stack.
Be aware that this operation only supported in a same region stacks. If you need to deploy the two stacks in two different regions, you need other mechanism.
Relevant questions
Asset Parameters in Cloud Formation template generated by AWS CDK
asked 2 months agoHow to provision CloudFront distribution with ACM certificate in CloudFormation?
asked 3 months agoCdk deploy in pipeline example?
asked 3 months agoreference resources between CDK stacks
asked 2 months agoHow to use CDK without using CDKToolkit
asked 5 months agocdk deploy / destroy seem to ignore profile
Accepted Answerasked 5 months agoDeleting CloudFormation Stacks using a shell script
Accepted Answerasked 2 years agoCloudFormation structuring & splitting of resources between stacks
Accepted Answerasked 2 years agoCDK tag manager vs Cloudformation stack tags
Accepted Answerasked 3 years agoWhy does my stack deletion fail because of an error that occurs when deleting a custom resource?
Accepted Answerasked a year ago