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:
- How to fix it (obviously)
- 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.
2
Answers
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:
Hope that helps someone!
LambdaStack
can’t be deployed beforeMainStack
, because it needs theMainStack
VPC. But neither canMainStack
be deployed first, because it needs the function reference fromLambdaStack
. 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 seenStack
constructs (other than NestedStacks) built as parent-child. This may not result in errors, but at a minimum does not seem idiomatic.