-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cyclic reference when API and Lambda are in different stacks (Python code) #3000
Comments
Need to check for this condition and give a helpful error message |
This issue isn't isolated to Python - it occurs even in a Typescript CDK app. This has to do with the way the API Gateway and Lambda constructs interplay. When a Lambda function from one stack is referenced into an API Gateway integration on another stack, the latter stack takes a dependency on the former stack (as expected) by referencing the function handler's ARN or its ARN is referenced. At the same time, the function handler construct is now updated with permissions to allow the API Gateway method to invoke the function, which depends on knowing the 'execute-api' ARN of the API Gateway 'Method'. In effect, the former stack now depends on the ARN generated by the latter stack. This causes the cyclic dependency. |
1. Token resolution of Deployment construct must not resolve the entire stack, specifically during the prepare phase. stack.resolve() works only after the CDK app has been fully prepared. During the 'prepare' phase, token resolution should instead resolve the token partially and within the local context. 2. Scope the lambda.CfnPermission construct closer to the consumer of the permission rather than being closer to the lambda function. For instance, when a lambda function is being consumed by an APIGateway RestApi Method as a cross-stack reference, placing the lambda.CfnPermission construct closer to the RestApi Method reduces the possibility of cyclic dependencies. fixes #3705, #3000
β¦n and/or creates cyclic references 1. Token resolution of Deployment construct must not resolve the entire stack, specifically during the prepare phase. stack.resolve() works only after the CDK app has been fully prepared. During the 'prepare' phase, token resolution should instead resolve the token partially and within the local context. 2. Scope the lambda.CfnPermission construct closer to the consumer of the permission rather than being closer to the lambda function. For instance, when a lambda function is being consumed by an APIGateway RestApi Method as a cross-stack reference, placing the lambda.CfnPermission construct closer to the RestApi Method reduces the possibility of cyclic dependencies. fixes #3705, #3000
β¦n and/or creates cyclic references 1. Token resolution of Deployment construct must not resolve the entire stack, specifically during the prepare phase. stack.resolve() works only after the CDK app has been fully prepared. During the 'prepare' phase, token resolution should instead resolve the token partially and within the local context. 2. Scope the lambda.CfnPermission construct closer to the consumer of the permission rather than being closer to the lambda function. For instance, when a lambda function is being consumed by an APIGateway RestApi Method as a cross-stack reference, placing the lambda.CfnPermission construct closer to the RestApi Method reduces the possibility of cyclic dependencies. fixes #3705, #3000
β¦ence (#4010) 1. Token resolution of Deployment construct must not resolve the entire stack, specifically during the prepare phase. stack.resolve() works only after the CDK app has been fully prepared. During the 'prepare' phase, token resolution should instead resolve the token partially and within the local context. 2. Scope the lambda.CfnPermission construct closer to the consumer of the permission rather than being closer to the lambda function. For instance, when a lambda function is being consumed by an APIGateway RestApi Method as a cross-stack reference, placing the lambda.CfnPermission construct closer to the RestApi Method reduces the possibility of cyclic dependencies. fixes #3705, #3000
This error seems to be occurring between Lambda and Cloudwatch Event rules as well. We have a main stack in which we have two child stacks. One of which is used to create Lambda Functions. In another stack, we create Cloudwatch event rules with the Lambda functions as Targets. And we see the same error. Can someone explain whether the fix above is only between API GW, Lambda? |
@advaj - Yes, the above fix is only between APIGateway and Lambda. Please report this a separate issue with enough code to reproduce this. Thanks. |
I am importing aws_apigateway to use SpecRestApi and I am getting the cyclic reference error. aws_apigatewayv2 does not reference SpecRestApi. How do I resolve the issue issue when trying to add_permission to the lambda? I am using the latest aws-cdk-lib==2.88.0. |
I'm submitting a ...
What is the current behavior?
Python code to reproduce:
Results in:
What is the expected behavior (or behavior of feature suggested)?
Maybe I'm doing something wrong? I would expect that code to work.
What is the motivation / use case for changing the behavior or adding this feature?
Once the APIs are stable the code changes much more often, so it would make sense to separate them in different stacks.
Please tell us about your environment:
Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. associated pull-request, stackoverflow, gitter, etc)
I'm creating the actual API outside of the stack's
__init__
method in order to be able to set the LambdaRestApi handler first. I was a bit surprised that it seemed to work and I'm not sure it's the right approach, but I don't think that's what's causing the bug.The text was updated successfully, but these errors were encountered: