Home > Blockchain >  AWS CDK cyclic reference for rest_api-lambda-rds stack
AWS CDK cyclic reference for rest_api-lambda-rds stack

Time:01-27

My setup looks like this:

*Main Stack*
RestApi
VPC (2 subnets, one public, one private_isolated)
DBStack
LambdaStack
*DBStack*
ServerlessCluster (in private_isolated VPC subnet)
*LambdaStack*
LayerVersion
lambda.Function (in private_isolated VPC subnet)

The LambdaStack also (after creating the function) registers a new route through api.root.addResource() and a method through rest_endpoint.addMethod("GET/POST/...", LambdaIntegration(lambaFunction)) as well as a database.connections.allowDefaultPortFrom(lambdaFunction).

When I try to synth this, I get the following error msg:

Error: 'MainStack/lambdastack' depends on 'MainStack' (no description provided, no description provided, no description provided, no description provided, no description provided, no description provided). Adding this dependency (MainStack -> MainStack/lambdastack/lambda_function/Resource.Arn) would create a cyclic reference.

I can "fix" this by removing the rest_endpoint.addMethod() call (which is obviously not an option).

My questions are the following:

  1. How to fix it (obviously)
  2. How would I go about debugging this myself as all the usual resource descriptions are "no description provided" here)

I tried different levels of separation (all in one file, a lot of stacks...) which all made no difference.

CodePudding user response:

LambdaStack can't be deployed before MainStack, because it needs the MainStack VPC. But neither can MainStack be deployed first, because it needs the function reference from LambdaStack. This is the "cyclic reference" in the error.

The easiest fix is to put your entire app into a single stack. Alternatively, refactor your app such that the inter-stack dependencies run in one direction only. The CDK best practices docs address the "How many stacks?" question.

If you do end up keeping multiple stacks, consider making the App the common scope for all the stacks, rather than nesting them. I have not seen Stack constructs (other than NestedStacks) built as parent-child. This may not result in errors, but at a minimum does not seem idiomatic.

CodePudding user response:

Alright, thanks in part to fedonev (moving all stacks one layer up) and some rather lucky finds in the AWS docs, here is what now works well:

  • everything is in one stack for now
  • i had to add an interface endpoint to the private_isolated vpc for the secret manager

Hope that helps someone!

  • Related