aws-cdk: [APIGateway] LambdaIntegration: Add option to create a single trigger/permission with wildcards only instead of one for each ApiGateway Resource
For the Lambda ApiGateway integration, add an option to prefer a single wildcard trigger/integrationPermission instead of multiple triggers/integrationPermissions for each URL/endpoint/resource defined in the ApiGateway.
Currently the created triggers in the AWS console looks like that:
arn:aws:execute-api:us-east-1:123:api_id/prod/GET/v1/parent_res_1/*
arn:aws:execute-api:us-east-1:123:api_id/prod/POST/v1/parent_res_1/*
arn:aws:execute-api:us-east-1:123:api_id/prod/GET/v1/parent_res_2/*
...
The requested feature would allow to have something like that instead:
arn:aws:execute-api:us-east-1:123:api_id/*
Use Case
In case of APIs with a larger amount of urls/endpoints/resources, it is likely to get a “The final policy size (XXX) is bigger than the limit (20480)” error.
In our case, we run into that for an API with around 15 resources and worked around temporarily by setting LambdaIntegrationOptions.allowTestInvoke to false. This cut the number of triggers/IntegrationPermissions in half and the policy didn’t hit the limit anymore.
However, we would prefer leaving allowTestInvoke to true.
Moreover as the API grows over time, we will likely run into the same issue again later: the faster the API grows, the sooner. Implementing something like described in https://github.com/aws/aws-cdk/issues/5774#issuecomment-609583801 (also see below) currently seems to be something like a last resort for us.
Implication of the current state of CDK in this respect for us is that the CDK ApiGateway -> LambdaIntegration cannot be easily used for APIs with a considerable amount of endpoints because the CDK stack will break sooner or later when adding more resources to the APIGateway.
Proposed Solution
- Add
booleanoptionsingleWildcardTriggerorsingleWildcardIntegrationPermissiontoaws_cdk.aws_apigateway.LambdaIntegrationOptions. - Per default, it is
falseand everything works like as it does currently. - In case of
true, only a single trigger with wildcards is generated (see above). - With the existing
allowTestInvokeoption, there is already an option which works globally an all tiggers/integrationPermissions as well. So something very similar is already available.
Other
There is a similar (duplicate?) issue which as been closed already https://github.com/aws/aws-cdk/issues/5774 (closed https://github.com/aws/aws-cdk/issues/5774#issuecomment-594902199 by AWS). The discussions in the end (after closing by AWS) are about workarounds (subclassing CDK) for something which seems to be missing as a feature, thus I created a new issue. Feel free to reopen the original one and add this as a duplicate.
Please also check https://github.com/aws/aws-cdk/issues/5774#issuecomment-609583801 which has been added after closing the issue. This comment describes the problem exactly the same as we see it.
This is a 🚀 Feature Request
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 24
- Comments: 33 (5 by maintainers)
Expecting for this to be fixed also!
Author of the comment that is referenced in this ticket. It has been a few versions since that was posted and no longer works. I took some inspiration from @nija-at comment above. Rather extending the class as I did in the old method than using Aspects that apply it over all the whole API GW methods. So my alternative updated version that seems to work:
I’m new to using CDK since I thought it would be a good idea to switch my AWS project from using Serverless Framework to
cdkI immediately hit this issue which meant I couldn’t deploy my HttpApi due to the per-route permissions making the policy too large to deploy.
After lots of head scratching, digging through
aws-cdkcode and borrowing ideas from the snippets above, here’s my contribution to the growing collection of (hacky) workarounds:ApiGatewayV2 + HttpApi + Python Workaround
In terms of the comment above where @enroly-mike was told that this is not a CDK problem it seems fair to note here that this issue didn’t exist with Serverless Framework which was creating a wildcard rule instead.
From my perspective as a first-time user of CDK this issue is a pretty big red flag for me currently. The issue is now over two years old and it seems pretty clear that this is a serious problem in situations where you have multiple routes that share a single function. (You can’t deploy your stack without implementing a workaround)
In my case I’m using Rust to implement a native lambda with
Runtime.PROVIDED_AL2which handles multiple, related routes. I wouldn’t have imagined this would be considered unusual, so it was surprising to hit a hurdle like this.Versions:
cdk = 2.52.0 (build 096d2e0) aws-cdk-lib = 2.52.0 apigatewayv2-alpha = 2.52.0a0
Unfortunately, we’ve not gotten around to tackling this issue.
However, I have a workaround below -
The
PermissionAspectmodel walks the construct tree starting from theRestApiand removes all permissions associated with the methods. Then, a compressed set of permissions with wildcards can be added.Hey, we’ve actually just recently merged a PR which aims to tackle all sorts of policy size limit issues. Look forward to the next v2 release and let me know if this is still an issue 🙂
https://github.com/aws/aws-cdk/pull/19114
Here’s a solution that’s similar to the ones of @rehanvdm but the lambda integration are also responsable to register the permission of the
lambda:InvokeFunctionNote sure if its the case for anyone else but the above code didn’t work with cdk 2.37.1 so I had to resort to the following
I was told by AWS Premium support that this wasn’t a CDK problem, it was a lambda problem, I’m somewhat unconvinced by that statement. Also forgive me for my TS escape, please feel free to recommend a more type safe way (or collapse the permissions properly)
Thanks for letting us know @robert-hanuschke, we’re aware that our fix which went out before didn’t fix as many of these issues as we would have liked. We’re still working on figuring out how to best minimize the templates
========================================================================== Example from my code :
If you’re looking for Python solution, here is my snippet translated from @rehanvdm