AWS Lambda@Edge
Tags: aws, lambda, cloud, serverless
Published: 2023-05-27 12:35:33

![Day8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17p8hsgf0j10f1ju6lhk.png)

[8/30] #30DaysOfAWS Today, AWS Lambda@Edge & Hands-On.

**AWS Lambda@Edge** is a service provided by Amazon Web Services (AWS) that allows you to run custom code in response to events generated by Amazon CloudFront, AWS's content delivery network (CDN). CloudFront is responsible for delivering content such as web pages, images, videos, and other files to end users with low latency and high transfer speeds.

Lambda@Edge extends the capabilities of CloudFront by allowing you to execute serverless functions at edge locations, which are distributed globally close to end users. These functions are written in AWS Lambda, no need for provisioning or managing servers.

By leveraging Lambda@Edge, you can customize the content delivered by CloudFront based on various events. For example, you can modify HTTP responses, generate dynamic responses, perform authentication and authorization, redirect requests, and implement A/B testing. The code you write for Lambda@Edge functions can manipulate incoming requests and outgoing responses, making it possible to modify or enhance the behavior of your CloudFront distribution at the edge locations.

Lambda@Edge functions are written in supported programming languages such as Node.js, Python, Java, and C#. They are associated with specific CloudFront events, such as viewer request, origin request, viewer response, or origin response. These events allow you to control different stages of the content delivery process and customize the behavior accordingly.

Using Lambda@Edge, you can achieve lower latency and improved performance by processing requests closer to the end user. It also reduces the load on your origin servers, as some processing tasks can be offloaded to the edge locations.

Overall, Lambda@Edge is a powerful service that enables you to add custom logic and enhance the capabilities of CloudFront CDN by running serverless functions at the edge, providing greater control and flexibility in delivering your content to end users.

## Differences between Lambda@Edge & CloudFront

**CloudFront** and **AWS Lambda@Edge** are both services offered by Amazon Web Services (AWS) that are commonly used together for content delivery and serverless computing. While they have some overlapping functionalities, they serve different purposes and play distinct roles in the AWS ecosystem. Here's an overview of the differences between CloudFront and Lambda@Edge:

- **CloudFront**:

   - _Content Delivery Network (CDN)_: CloudFront is a globally distributed CDN that helps deliver content to end-users with low latency and high transfer speeds. It caches content in edge locations located around the world, reducing the load on the origin server and improving the overall performance.

   - _Caching and Content Delivery_: CloudFront caches static and dynamic content, such as images, videos, HTML pages, and API responses, at its edge locations. It serves cached content directly from the edge locations, reducing the round trip time to the origin server.

   - _Scalability and Availability_: CloudFront automatically scales to handle high traffic loads and provides high availability. It distributes traffic across multiple edge locations, enabling faster content delivery to users globally.

   - _Content Manipulation_: CloudFront supports various content manipulation capabilities, such as content compression, HTTP header customization, redirect rules, and SSL/TLS termination.

- **Lambda@Edge**:

   - _Serverless Computing_: Lambda@Edge allows you to run custom JavaScript or Node.js code in response to CloudFront events at the edge locations. It leverages the AWS Lambda service to execute code close to the end-users, reducing latency and improving responsiveness.

   - _Event-Driven Execution_: Lambda@Edge functions are triggered by CloudFront events, such as viewer requests, origin requests, viewer responses, and origin responses. This allows you to modify the behavior of your CloudFront distribution at the edge.

   - _Content Personalization_: Lambda@Edge enables dynamic content personalization by modifying requests or responses based on specific criteria. For example, you can customize the content based on the user's location, device type, or user-specific data.

   - _Custom Logic_: With Lambda@Edge, you can implement custom logic at the edge locations, such as A/B testing, authentication, authorization, request/response transformation, bot detection, and security enhancements.

In summary, CloudFront is primarily a content delivery service that caches and serves content from edge locations, while Lambda@Edge enables serverless computing at the edge, allowing you to customize and modify CloudFront behavior based on specific events. Lambda@Edge provides the ability to execute custom code closer to end-users, enhancing the performance and personalization capabilities of CloudFront distributions.

**Step-by-step tutorial** for how to create AWS Lambda edge. You can download it using this [URL](https://bit.ly/3OQEKwy).

**Sample Codes** - [GitRepo](https://github.com/sanju2/lambda-edge-demo)

Thanks for reading the Article.