Is anybody using CloudFront Functions? I want to d...
# help
Is anybody using CloudFront Functions? I want to display some content on my React app based on user-country… I know CF has some headers returned with country-code, but I would like to have that embedded into the HTML/JS to make decisions based on that. Anybody used this approach? Or maybe my lambda ideally can have that info.
If you want the result of that cached in CloudFront, you probably want to look at Lambda @Edge, as you can run function for origin requests and then have the manipulated result stored in the cache. If CloudFront is caching based on user’s country, then each unique transformation would be cached. But if it something that needs to be uniquely generated on every single request, CloudFront Functions could work well. One of the biggest differences is the runtime and limitations of CloudFront functions. It can only run for viewer request/response, must complete in under 100ms and it only supports vanilla Javascript in the runtime.
In either case though, the event that comes into the function will have the CloudFront headers available for inspection, including user location data.
Maybe I can build a simple CF Function which returns the user-cuntry.
Copy code
function country() {
return headers['cloudfront-viewer-country'];
And my frontend can simply do a GET there.
Yep, it’s easier to understand when you see the event coming through. You can start by just logging and printing the whole event to see the structure and whats available
Yes makes sense.
We use it to serve a language-specific version of our Angular site based on user’s browser language header or the URL path.. it then changes the request path sent to s3
Interesting, I will need to try how to use it.