# Lambda\@Edge

* Lambda\@Edge is **an extension of AWS Lambda**, a compute service that **lets you execute functions that customize the content that CloudFront delivers**.<br>
* You can **author Node.js or Python functions in one Region**, US-East-1 (N. Virginia), and then **execute them in AWS locations globally that are closer to the viewer,** without provisioning or managing servers.<br>
* Lambda\@Edge **scales automatically, from a few requests per day to thousands per second**.<br>
* Processing requests at AWS locations closer to the viewer instead of on origin servers **significantly reduces latency and improves the user experience**.<br>
* When you associate a **CloudFront distribution with a Lambda\@Edge function**, **CloudFront intercepts requests and responses at CloudFront edge location**s.<br>
* You can **execute Lambda functions when the following CloudFront events occur:**<br>
  * **When CloudFront receives a request from a viewer** (viewer request)<br>
  * **Before CloudFront forwards a request to the origin** (origin request)<br>
  * **When CloudFront receives a response from the origin** (origin response)<br>
  * **Before CloudFront returns the response to the viewer** (viewer response)

![](https://4079160698-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MOmHH1M7R1RV5MGm4F7%2F-MP1-EQ9z14_xPmgrX2p%2F-MP1JaTB2WHem8Ms0_OB%2Fimage.png?alt=media\&token=623e88f7-badf-45e4-8145-e108381bdb03)

## Use Cases

* A Lambda **function can inspect cookies and rewrite URLs so that users see different versions of a site for A/B testing**.<br>
* CloudFront can **return different objects to viewers based on the device they're using** by checking the `User-Agent` header, which includes information about the devices.
  * For example, **CloudFront can return different images based on the screen size of their device**.<br>
* For example, on a retail website that sells clothing, if you **use cookies to indicate which colour a user chose for a jacket, a Lambda function can change the request so that CloudFront returns the image of a jacket in the selected colour**.
