I need some help with the CloudFront-Viewer-Country header. I'm trying to use it to customize responses for users from specific countries, but the documentation is really confusing. Here's my situation: I'm not seeing the header during the viewer request phase, despite adding it to both the cache and origin policies. However, I do see it at the origin request phase, but that's too late for me to change the cache key. My goal is to create just two caches: one for users from country A and another for everyone else, without creating separate caches for each country. What am I missing? What's the best approach to achieve this?
2 Answers
It sounds like you're a bit mixed up. In CloudFront, you can't actually access the CloudFront-Viewer-Country header during the viewer request event. Instead, you should really focus on using it during the origin request phase. You can customize your response based on the country at that stage, but just remember that you won’t be able to affect the caching behavior based on that header directly in the viewer request.
I ran into this same issue before! What worked for me was using the Origin-Response Lambda@Edge function to modify how the response is tailored by country. Just make sure to include the country header as an additional key in your cache policy; that way, the country-specific responses will get cached correctly without fragmenting them too much across multiple countries.

I see your point, but if we can't access it during the viewer request, how can I ensure that specific API responses tailored for country A are properly cached for just those users?