AWS has recently introduced Lambda@Edge which gives you the possibility to run Lambda code in Edge locations. What does it mean for CloudFront users and what can you achieve with it?
AWS Lambda is a serverless solution that allows you to run code in the Cloud without actually provisioning and managing any servers. Using Lambda, your code is executed only when needed and scales to your needs. You also only pay for the compute time you have used.
Lambda@Edge extends its capability by allowing you to run Lambda code in AWS Edge locations. This gives you the opportunity to deliver a response to your end users at the lowest latency and customise content for a better user experience. From this, your code is triggered by various CloudFront events.
Events that can trigger Lambda@Edge:
- After CloudFront receives a request from a viewer (viewer request)
- Before CloudFront forwards the request to the origin (origin request)
- After CloudFront receives the response from the origin (origin response)
- Before CloudFront forwards the response to the viewer (viewer response)
With Lambda@Edge you can:
- Inspect cookies and rewrite URLs to perform A/B testing.
- Send specific objects to your users based on the User-Agent header.
- Implement access control by looking for specific headers before passing requests to the origin.
- Add, drop, or modify headers to direct users to different cached objects.
- Generate new HTTP responses.
- Cleanly support legacy URLs.
- Modify or condense headers or URLs to improve cache utilisation.
- Make HTTP requests to other Internet resources and use the results to customise responses
You can now create custom content for your users more easily (and cheaper) than ever before. You can also, for example, identify a user’s device type and distribute the best content to them. It can also be used to identify and validate visitors at the edge and you can detect search engine bots and filter traffic from origin servers with a capture page.
Read more on Lambda@Edge optimisation on my post here.
Here are just a few Lambda@Edge use cases:
Highly Personalised Websites
Remote Network Calls
Response Generation At Viewer Request
Access Control at the Edge
To start using Lambda@Edge you need to associate a function with the CloudFront distribution. The function will be automatically replicated and ready to run at every AWS Edge location.