Lambda@Edge is a CloudFront feature that runs code at AWS edge locations to customize requests and responses with low latency. Functions (Node.js or Python) execute on viewer/origin request and response events, so you can rewrite URLs, add or strip headers/cookies, authorize users, or route to different origins – without managing servers.
Data traffic on the modern Internet is growing rapidly. To keep up with this growth, website and application owners have turned to CDNs (Content Delivery Networks) like Amazon CloudFront to cache web content on geographically dispersed servers located at edge locations around the world. End users can expect faster load times and less load on the origin server. But what if you need to bring the code and its computing closer to your user? This is where Lambda@Edge can help.
In this blog post, we’ll introduce Lambda@Edge functions, use cases, benefits, how to deploy it, and its pricing.
You can also watch our video about Lambda@Edge.
Lambda@Edge allows you to run code across Amazon CloudFront edge locations globally without provisioning or managing servers, responding to end-users at the lowest network latency.

You just upload your Node.js or Python code to AWS Lambda and configure your function to be triggered in response to CloudFront requests/events. The code is then ready to execute across AWS edge locations globally when a content request is received and scales with the volume of requests globally.

Your functions will automatically trigger in response to the following Amazon CloudFront events:


The following AWS Lambda features are not supported by Lambda@Edge:
Quotas that differ by event type:

You can automatically scale and run your code in AWS locations around the world without deploying or managing origin servers in multiple locations, or setting up load balancing or DNS (Domain Name System) routing services.
You can add new functionality without making any changes to existing applications running on the origin server. Finally, by using Lambda@Edge and CloudFront, you have less origin infrastructure to manage than with a traditional CDN.
Lambda@Edge runs your code in AWS edge locations around the world, close to your users, so you can deliver full-featured custom content with high performance and low latency.
With Lambda@Edge, you can customize the content delivered through CloudFront CDN, and you can select the computing resources provided, thus customizing execution time, based on your application performance needs.
Lambda@Edge is charged based on the following two factors:
Take a look at the AWS Lambda pricing model, some things you need to keep in mind when using it and how to optimize the cost of Lambda functions.
Lambda@Edge is optimized for latency-sensitive use cases where your end viewers are distributed globally. All the information you need to make a decision should be available at the CloudFront edge, within the function and the request.
The use cases of Lambda@Edge can be divided into:
One of the biggest benefits of using Lambda@Edge is to improve cache hit rates, either by increasing the likelihood that content will be cached when it is returned from the origin, or by increasing the availability of content that is already in a cache condition. You can add or modify cache control headers on responses.
With Lambda@Edge, you can dynamically generate content based on request or response attributes.
Lambda@Edge can also be used to handle custom authentication and authorization.
In some cases, the origin requires additional request and response logic. You can run your Lambda@Edge function in CloudFront instead of code running on the origin server for a more seamless solution. For example, you can implement logic to do the following:
The first thing you will notice is that you will not find this service in the AWS Management Console. This is because it is a little different from a lot of other services like Amazon S3, Amazon EC2 etc. So let’s look at how the Lambda@Edge function is created in the AWS Management console.
Before going through this simple manual, you will need an Amazon CloudFront distribution.
1. In the AWS Lambda console, create a Lambda function in the US East (N. Virginia) Region. When creating your function, select AWS policy template: Basic Lambda@Edge permissions as your execution role.

2. Upload or add your code in Node.JS or Python. Save and deploy this function. 3. Add a trigger to this function.

4. Choose CloudFront as a trigger and click on deploy.

5. Choose the CloudFront distribution and cache behavior. Then specify the CloudFront event that causes the function to execute.

6. When you create a trigger, Lambda replicates the function to AWS edge locations. 7. If you need a more accurate description, try visiting the official AWS docs. And if you need to delete your test function, visit deleting Lambda@Edge functions and replicas.
Because Lambda@Edge can be used for many use cases that have been described in the previous section, it’s very difficult to add examples of the code that will be needed for a specific use case. AWS has some in their official documentation here: Lambda@Edge example functions.
In our video about Lambda@Edge, we are using the Node.JS code below to redirect traffic from non-www Route 53 hosted domain to www domain. If you want to use it, just change the my-test-2022.cf domain to yours.
exports.handler = async (event) => {
// (1)
const request = event.Records[0].cf.request;
// (2)
if (request.headers.host[0].value === 'my-test-2022.cf') {
// (3)
return {
status: 301',
statusDescription: Redirecting to www domain,
headers: {
location: [{
key: 'Location',
value: https://www.my-test-2022.cf${request.uri}
}]
}
};
}
return request;
};
The difference is that AWS Lambda is a regional service, and if you use it for Internet-related apps, you usually use it with Amazon API Gateway. And if you need to use this kind of application globally, you can deploy it yourself to multiple regions around the world or you can leverage Amazon CloudFront CDN.
Using Lambda@Edge and Amazon CloudFront allows you to execute logic automatically across multiple AWS edge locations based on where your end viewers are located.
This is the main difference between Lambda and Lambda@Edge. Everything else is mostly similar.
By using CloudFront and Lambda@Edge, you can start building high-performing distributed serverless web applications for your use cases. In this blog post, we shared what Lambda@Edge is, its benefits, use cases, and how to deploy it in the AWS Management Console.
Adam Novotny is an AWS Solutions Architect at Stormit with 5+ years of experience designing and optimizing AWS cloud architectures.
He supports customers across the full cloud lifecycle — from pre-sales consulting and solution design to AWS funding programs such as AWS Activate, Proof of Concept (PoC), and the Migration Acceleration Program (MAP).
Adam holds the AWS Certified Solutions Architect – Professional and AWS Certified CloudOps Engineer – Associate certifications.