Menu
Amazon CloudFront
Developer Guide (API Version 2016-09-29)

Use Cases for Lambda@Edge

You can use Lambda@Edge to access, process, and change HTTP header values. The following sections describe common use cases.

A/B Testing

You want to test two different versions of your home page with users, but you don’t want to create redirects or change the URL. Both versions of the page, “A” and “B,” exist in the CloudFront cache. Using Lambda@Edge, you can write a function that sets cookies at the edge location, randomly assigns the user to version A or B, and then returns the right version of the page back to the user. This makes the response to the user faster, and reduces load on the origin.

Customer User-Agent Detection and Grouping

You want to send back a different object to the user accessing your site based on the user-agent header. You can have multiple versions of your object (for example, an image in different resolutions) at the edge, and then you can select the right version of the image based on your own custom pattern match on the user-agent string. You might want to consider additional criteria. For example, you could examine a cookie to decide which version of the image to deliver. You also could consider the type of referrer. For example, you could always deliver the lowest resolution images to bots. You can customize this logic at the edge based on your business needs.

Other Examples

You can rewrite URLs to support a legacy version of your application code to make the URLs more user friendly, to change the URL to point to a custom error page, or to add or remove headers. For example, you could add a Content-Disposition header based on whether a user is using a mobile device, or you could insert HSTS headers when certain cookie values are confirmed, without the need for each request to go back to their backend origin infrastructure. Node.js also allows you to loop through headers to count or find a specific pattern.