Lambda@Edge example functions - Amazon CloudFront
See the following examples to use Lambda functions with Amazon CloudFront.
Note
If you choose runtime Node.js 18 or later for your Lambda@Edge function, an
index.mjs file is created for you automatically. To use the
following code examples, rename the index.mjs file to
index.js instead.
Topics
General examples
The following examples show common ways to use Lambda@Edge in CloudFront.
Example: A/B testing
You can use the following example to test two different versions of an image without creating redirects or changing the URL. This example reads the cookies in the viewer request and modifies the request URL accordingly. If the viewer doesn't send a cookie with one of the expected values, the example randomly assigns the viewer to one of the URLs.
The following example shows how to change the value of a response header based on the value of another header.
Generate responses - examples
The following examples show how you can use Lambda@Edge to generate responses.
Topics
Example: Serve static content (generated response)
The following example shows how to use a Lambda function to serve static website content, which reduces the load on the origin server and reduces overall latency.
Example: Generate an HTTP redirect (generated response)
The following example shows how to generate an HTTP redirect.
Query strings - examples
The following examples show ways that you can use Lambda@Edge with query strings.
Topics
The following example shows how to get the key-value pair of a query string parameter, and then add a header based on those values.
Example: Normalize query string parameters to improve the cache hit ratio
The following example shows how to improve your cache hit ratio by making the following changes to query strings before CloudFront forwards requests to your origin:
-
Alphabetize key-value pairs by the name of the parameter.
-
Change the case of key-value pairs to lowercase.
For more information, see Cache content based on query string parameters.
Example: Redirect unauthenticated users to a sign-in page
The following example shows how to redirect users to a sign-in page if they haven't entered their credentials.
Personalize content by country or device type headers - examples
The following examples show how you can use Lambda@Edge to customize behavior based on location or the type of device used by the viewer.
Topics
Example: Redirect viewer requests to a country-specific URL
The following example shows how to generate an HTTP redirect response with a country-specific URL and return the response to the viewer. This is useful when you want to provide country-specific responses. For example:
-
If you have country-specific subdomains, such as us.example.com and tw.example.com, you can generate a redirect response when a viewer requests example.com.
-
If you're streaming video but you don't have rights to stream the content in a specific country, you can redirect users in that country to a page that explains why they can't view the video.
Note the following:
-
You must configure your distribution to cache based on the
CloudFront-Viewer-Countryheader. For more information, see Cache based on selected request headers. -
CloudFront adds the
CloudFront-Viewer-Countryheader after the viewer request event. To use this example, you must create a trigger for the origin request event.
Example: Serve different versions of an object based on the device
The following example shows how to serve different versions of an object based on the type of device that the user is using, for example, a mobile device or a tablet. Note the following:
-
You must configure your distribution to cache based on the
CloudFront-Is-*-Viewerheaders. For more information, see Cache based on selected request headers. -
CloudFront adds the
CloudFront-Is-*-Viewerheaders after the viewer request event. To use this example, you must create a trigger for the origin request event.
Content-based dynamic origin selection - examples
The following examples show how you can use Lambda@Edge to route to different origins based on information in the request.
Topics
Example: Use an origin request trigger to change from a custom origin to an Amazon S3 origin
Example: Use an origin-request trigger to change the Amazon S3 origin Region
Example: Use an origin request trigger to change from an Amazon S3 origin to a custom origin
Example: Use an origin request trigger to change the origin domain name based on the country header
Example: Use an origin request trigger to change from a custom origin to an Amazon S3 origin
This function demonstrates how an origin-request trigger can be used to change from a custom origin to an Amazon S3 origin from which the content is fetched, based on request properties.
Example: Use an origin-request trigger to change the Amazon S3 origin Region
This function demonstrates how an origin-request trigger can be used to change the Amazon S3 origin from which the content is fetched, based on request properties.
In this example, we use the value of the
CloudFront-Viewer-Country header to update the S3 bucket domain
name to a bucket in a Region that is closer to the viewer. This can be useful in
several ways:
-
It reduces latencies when the Region specified is nearer to the viewer's country.
-
It provides data sovereignty by making sure that data is served from an origin that's in the same country that the request came from.
To use this example, you must do the following:
-
Configure your distribution to cache based on the
CloudFront-Viewer-Countryheader. For more information, see Cache based on selected request headers. -
Create a trigger for this function in the origin request event. CloudFront adds the
CloudFront-Viewer-Countryheader after the viewer request event, so to use this example, you must make sure that the function executes for an origin request.
Note
The following example code uses the same origin access identity (OAI) for all S3 buckets that you're using for your origin. For more information, see Origin access identity.
Example: Use an origin request trigger to change from an Amazon S3 origin to a custom origin
This function demonstrates how an origin-request trigger can be used to change the custom origin from which the content is fetched, based on request properties.
Example: Use an origin request trigger to gradually transfer traffic from one Amazon S3 bucket to another
This function demonstrates how you can gradually transfer traffic from one Amazon S3 bucket to another in a controlled way.
Example: Use an origin request trigger to change the origin domain name based on the country header
This function demonstrates how you can change the origin domain name based on
the CloudFront-Viewer-Country header, so content is served from an
origin closer to the viewer's country.
Implementing this functionality for your distribution can have advantages such as the following:
-
Reducing latencies when the Region specified is nearer to the viewer's country
-
Providing data sovereignty by making sure that data is served from an origin that's in the same country that the request came from
Note that to enable this functionality you must configure your distribution to
cache based on the CloudFront-Viewer-Country header. For more
information, see Cache based on selected request headers.
Update error statuses - examples
The following examples provide guidance for how you can use Lambda@Edge to change the error status that is returned to users.
Topics
Example: Use an origin response trigger to update the error status code to 200
This function demonstrates how you can update the response status to 200 and generate static body content to return to the viewer in the following scenario:
-
The function is triggered in an origin response.
-
The response status from the origin server is an error status code (4xx or 5xx).
Example: Use an origin response trigger to update the error status code to 302
This function demonstrates how you can update the HTTP status code to 302 to redirect to another path (cache behavior) that has a different origin configured. Note the following:
-
The function is triggered in an origin response.
-
The response status from the origin server is an error status code (4xx or 5xx).
Access the request body - examples
The following examples show how you can use Lambda@Edge to work with POST requests.
Note
To use these examples, you must enable the include body option in the distribution's Lambda function association. It is not enabled by default.
-
To enable this setting in the CloudFront console, select the check box for Include Body in the Lambda Function Association.
-
To enable this setting in the CloudFront API or with CloudFormation, set the
IncludeBodyfield totrueinLambdaFunctionAssociation.
Topics
Example: Use a request trigger to read an HTML form
This function demonstrates how you can process the body of a POST request generated by an HTML form (web form), such as a "contact us" form. For example, you might have an HTML form like the following:
<html>
<form action="https://example.com" method="post">
Param 1: <input type="text" name="name1"><br>
Param 2: <input type="text" name="name2"><br>
input type="submit" value="Submit">
</form>
</html>
For the example function that follows, the function must be triggered in a CloudFront viewer request or origin request.
Example: Use a request trigger to modify an HTML form
This function demonstrates how you can modify the body of a POST request generated by an HTML form (web form). The function is triggered in a CloudFront viewer request or origin request.