Skip to content

How do I make CloudFront request headers available in my edge compute functions?

0

I'm working with CloudFront edge compute (CloudFront Functions or Lambda@Edge) and want to leverage the built-in CloudFront request headers (like CloudFront-Viewer-Country or CloudFront-Is-Mobile-Viewer) within my functions to customize content and responses. Do I need to specifically enable or configure anything within my CloudFront distribution to make these headers accessible in my edge function?

1 Answer
1
Accepted Answer

You need to explicitly include CloudFront request headers in specific behaviors to access them in your edge compute functions. This is done through either a cache policy or an origin request policy. You might access viewer's device type, location and more through:

  • Cache Policy: Use this if you want the header to be part of the cache key, influencing how CloudFront caches your content. The headers included in your cache policy will also be available to your edge functions.

  • Origin Request Policy: This allows you to use the headers for logic within your functions without affecting caching behavior. The headers will be included in the request to your origin (e.g., your web server or application) and also available to your edge functions.

Keep in mind that headers that you include in the cache key (using a cache policy) are automatically included in origin requests. Use the origin request policy to specify the information that you want to include in origin requests, but not include in the cache key. For more information, please see How origin request policies and cache policies work together in Amazon CloudFront Developer Guide.

AWS
answered a year ago
EXPERT
reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.