I need to execute some Lambda function for collecting statistics before any method from API gateway is called. Is it possible to achieve this in AWS? I am using proxy integration in Api Gateway.
2 Answers
This can be done in the CDN level if you have one. If you are using cloudfront then the steps could be,
- Create a regional endpoint.
- Set up own cloudfron distribution - https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-cloudfront-distribution/
- Handle the request with Lambda@Edge, add a trigger for Origin Request.https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-cloudfront-trigger-events.html
3 Comments
Mikołaj Waśniewski
Thanks for your idea. Unfortunately your solution is possible to implement only in "US East (N. Virginia)" region -> docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/… I can't change the region to US East (N. Virginia) so I need to look for other solutions.
avisek hazra
may I know what kind of statistics are you looking to measure?
Mikołaj Waśniewski
I want to count API requests which contain certain data, i.e. API requests which have "personId" parameter set in body. I want to store these statistics in DynamoDB table.
Few Ideas,
- If the requests may be whitelisted and blacklisted based on the parameter, lambda custom authorizor can be used to plug in the logic.
- if the statistics don't influence the business logic, then cloudwatch logs can be parsed (log insights or cloudwatch rules) and the statistics can be gathered.
- Aggregation pattern can be used for the statistics- either Gateway or Lambda level , but this will add additional latency for each requests.
1 Comment
Mikołaj Waśniewski
Unfortunately CloudWatch is not an option for me. I need to extract tenantId field from the request. This is needed for my statistics.