I am trying to create an API (either REST or HTTP API) on AWS that points to some lambda function that can then access the Cloudfront HTTP headers described here:
Adding the CloudFront HTTP headers
I have been struggling to get these headers added all day. I have tried the following:
- Cloudfront distribution that points to Http API with a lambda function as a route. The cloudfront distribution has an origin request policy as follows:
When I test this, I have no cloudfront headers appearing in the request (when I print the headers in the lambda function).
- When I try to do the same setup with a REST API, I am able to see the "CloudFront-Viewer-Country" and "CloudFront-Viewer-ASN" but don’t see any other cloudfront headers, even with the above origin request policy.
2
Answers
I was able to get it to work without API Gateway by following this tutorial:
Using Amazon CloudFront with AWS Lambda as origin to accelerate your web applications
Then applying the above origin request policy. Still not sure how to get it to work with API Gateway.
I faced the similar issue but the point to note here is you need to tell cloudfront which header you want to pass through.
I believe it is related to security reasons.
So allow the specific header in cloudfront as custom and then that header will be available in lambda.
Example –
https://kuchbhilearning.blogspot.com/2022/11/add-custom-header-in-cloudfrontpass.html