fix(apigateway): consolidate lambda permissions when reused for multiple operations #35705
+107,350
−33,927
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue # (if applicable)
Fixes #9327
Fixes #19535
Reason for this change
The maximum Lambda permission policy size could be exceeded for APIs which reused the same Lambda function for multiple operations, as the integration added a new permission for each operation, scoped down to the specific operation.
Description of changes
This change updates both the REST and HTTP API lambda integrations to consolidate permissions when more than 10 permissions would be added for the same handler, creating a permission scoped to the entire API rather than the operation. The behaviour remains the same where individual lambdas are used for operations.
Note that we search for permissions within the route's parent stack for HTTP APIs, or within the API for REST APIs, and so it won't prevent the policy size being exceeded if the same lambda is reused cross-stack.
Describe any new or updated permissions being added
Permission for API Gateway to invoke the lambda is scoped to any resource/method/stage when a lambda is reused for multiple operations.
Description of how you validated changes
Unit tests, Integration tests
Added an integration test for both REST and HTTP (
integ.lambda-permission-consolidation
).There are a lot of integration tests that now have updated snapshots since I've changed the logical ID for lambda permissions to include both the API and Handler IDs so that they can be identified for consolidation.
I wasn't able to get the following 2 integration tests to run:
aws-route53-targets/test/integ.api-gateway-domain-name.ts
- there's a comment in here that mentions it doesn't work due to the reliance on a hardcoded domain nameaws-apigatewayv2-integrations/test/http/integ.lambda-permission-consolidation.ts
- fails since the lambda permission tries to deploy prior to the imported lambda function - I'm not sure if this worked previously - can look into fixing this integration test if requiredChecklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license