Skip to content
Gallery
7. DNS, Caching and Performance Optimization
Share
Explore
Amazon Cloudfront

icon picker
Customize at the edge with functions

Last edited 52 days ago by Kirtan Chavda

Lambda@Edge

Lambda@Edge is an extension of AWS Lambda that offers powerful and flexible computing for complex functions and full application logic closer to your viewers, and is highly secure. Lambda@Edge functions run in a Node.js or Python runtime environment. You publish them to a single AWS Region, but when you associate the function with a CloudFront distribution, Lambda@Edge automatically replicates your code around the world.
You can author Node.js or Python functions in the Lambda console in one AWS Region, US East (N. Virginia).

Functionality

Allows running Node.js and Python Lambda functions to customize content delivery in CloudFront.
Executes functions in AWS locations closer to viewers for improved performance.

Execution Points

Functions can be executed at various points in CloudFront's request-response cycle:
Viewer Request: After CloudFront receives a request from a viewer.
Origin Request: Before CloudFront forwards the request to the origin.
Origin Response: After CloudFront receives the response from the origin.
Viewer Response: Before CloudFront forwards the response to the viewer.
image.png

Capabilities

Lambda@Edge functions can:
Inspect cookies and rewrite URLs for A/B testing.
Serve specific objects based on the User-Agent header.
Implement access control by checking specific headers before forwarding requests to the origin.
Manipulate headers to direct users to different cached objects.
Generate new HTTP responses dynamically.
Support legacy URLs seamlessly.
Modify or condense headers or URLs to improve cache utilisation.
Make HTTP requests to external resources and customize responses based on the results.

Exam Tip

Lambda@Edge can be used to load different resources based on the User-Agent HTTP header, enabling personalised content delivery based on user characteristics.

Cloudfront Functions

With CloudFront Functions in Amazon CloudFront, you can write lightweight functions in JavaScript for high-scale, latency-sensitive CDN customizations. Your functions can manipulate the requests and responses that flow through CloudFront, perform basic authentication and authorization, generate HTTP responses at the edge, and more. CloudFront Functions is approximately 1/6th the cost of Lambda@Edge and is extremely low latency as the functions are run on the host in the edge location, instead of the running on a Lambda function elsewhere.
The CloudFront Functions runtime environment offers submillisecond startup times, scales immediately to handle millions of requests per second, and is highly secure. CloudFront Functions is a native feature of CloudFront, which means you can build, test, and deploy your code entirely within CloudFront.
When you associate a CloudFront function with a CloudFront distribution, CloudFront intercepts requests and responses at CloudFront edge locations and passes them to your function. You can invoke CloudFront Functions when the following events occur:
When CloudFront receives a request from a viewer (viewer request)
Before CloudFront returns the response to the viewer (viewer response)

Differences between CloudFront Functions and Lambda@Edge

CloudFront Functions and Lambda@Edge both provide a way to run code in response to CloudFront events.
CloudFront Functions is ideal for lightweight, short-running functions for the following use cases:
Cache key normalization – Transform HTTP request attributes (headers, query strings, cookies, and even the URL path) to create an optimal , which can improve your cache hit ratio.
Header manipulation – Insert, modify, or delete HTTP headers in the request or response. For example, you can add a True-Client-IP header to every request.
URL redirects or rewrites – Redirect viewers to other pages based on information in the request, or rewrite all requests from one path to another.
Request authorization – Validate hashed authorization tokens, such as JSON web tokens (JWT), by inspecting authorization headers or other request metadata.

Lambda@Edge is ideal for the following use cases:
Functions that take several milliseconds or more to complete
Functions that require adjustable CPU or memory
Functions that depend on third-party libraries (including the AWS SDK, for integration with other AWS services)
Functions that require network access to use external services for processing
Functions that require file system access or access to the body of HTTP requests

Diff between CF Func & Lambda@Edge
Difference
CloudFront Functions
Lambda@Edge
1
Programming languages
JavaScript (ECMAScript 5.1 compliant)
Node.js and Python
2
Event sources
Viewer request
Viewer response
Viewer request
Viewer response
Origin request
Origin response
3
Yes
CloudFront KeyValueStore only supports
No
4
Scale
10,000,000 requests per second or more
Up to 10,000 requests per second per Region
5
Function duration
Submillisecond
Up to 5 seconds (viewer request and viewer response) Up to 30 seconds (origin request and origin response)
6
Maximum memory
For more information, see .
2 MB
128 MB – 10,240 MB (10 GB)
7
Maximum size of the function code and included libraries
10 KB
1 MB (viewer request and viewer response) 50 MB (origin request and origin response)
8
Network access
No
Yes
9
File system access
No
Yes
10
Access to the request body
No
Yes
11
Access to geolocation and device data
Yes
No (viewer request and viewer response) Yes (origin request and origin response)
12
Can build and test entirely within CloudFront
Yes
No
13
Function logging and metrics
Yes
Yes
14
Pricing
Free tier available; charged per request
No free tier; charged per request and function duration
There are no rows in this table

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.