ste26054
ste26054

Reputation: 469

LambdaEdge viewer cannot access CloudFront HTTP header CloudFront-Viewer-Country

I am trying to access the CloudFront HTTP Header CloudFront-Viewer-Country from a LambdaEdge Viewer function.

However, despite adding said header as a cache key in the CloudFront Cache Policy, and seeing this header appear in my Origin logs, I am unable to view this header from the lambda function. Logging the request only shows custom headers, nothing else.

exports.handler = async (event, context) => {
  const request = event.Records[0].cf.request;
  const headers = request.headers;

  console.log(`Given headers: ${JSON.stringify(headers)}`); // Nothing related to CloudFront-Viewer-Country
};

In the CloudFront HTTP headers documentation, edge functions are clearly mentioned (and I believe it applies to CloudFront functions as well as LambdaEdge functions) :

With these headers, your origin or your edge function can receive information about the viewer without the need for you to write your own code to determine this information.

Is this possible at all?

Upvotes: 3

Views: 1278

Answers (1)

ste26054
ste26054

Reputation: 469

I must have missed it, but in the Restriction on edge functions documentation:

CloudFront adds the headers after the viewer request event, which means they are not available to Lambda@Edge in a viewer request function.

Which means I am unable to extract the viewer country from a viewer request...

Upvotes: 0

Related Questions