DEV Community

AWS Lambda@Edge


[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.

Sample Codes - GitRepo

Thanks for reading the Article.

Top comments (2)

hectorfernandezdev profile image
Hector Fernandez CloudparaTodo

Great summary I used for auth 💪

lasanthasilva profile image
Lasantha Sanjeewa Silva

Thanks You!!