skip to Main Content

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:

  1. Cloudfront distribution that points to Http API with a lambda function as a route. The cloudfront distribution has an origin request policy as follows:

enter image description here

When I test this, I have no cloudfront headers appearing in the request (when I print the headers in the lambda function).

  1. 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


  1. Chosen as BEST ANSWER

    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.


  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search