The Lambda@Edge is an extension of AWS Lambda, which helps to execute functions that customize the content CloudFront delivers though the events, such as viewer request, origin request, origin response, and viewer response.
This article describes how to trigger an edge function on a CloudFront Distribution to deliver dynamic content based on the device using the origin request event.
Create a Lambda@Edge to serve dynamic content based on the device.
CloudFront Distribution created:
- The CloudFront must be configured to cache based on the Device Type. In this article, we only use two headers:
- CloudFront adds the CloudFront-Is-*-Viewer headers after the viewer request event.
- The CloudFront Origin must be configurated as a custom origin type.
Static website hosted on Amazon S3:
- The S3 Bucket must be configured as Static Website Hosting.
- The content for each specific device must be separated into different folders:
- Mobile path: /mobile
- Desktop path: /desktop
STEPS TO CREATE THE INFRASTRUCTURE
1. Create an IAM Role for Lambda@EdgeNote: The role must have a Trust relationship configuration for Lambda@Edge.
Enter the AWS console, go to the IAM service, and in the left panel, select the Roles option. Then, click over "Create Role," choose the "Lambda" option to use case and tap on Next.
Filter by the lambda word and mark the checkbox for the AWSLambdaRole policy.
Then, tap on the "Next" button until the Review page. Enter a role name to identify it. Consider that we will need it later. Once the review was validated, create the role.
In the policy, the document adds the edgelambda.amazonaws.com line into Service and update it. Configure the role as follows to avoid syntax errors.
The trust entity must be successfully added.
2 Create a Lambda@Edge for CloudFront
a. Lambda Function
On the AWS console, go to Lambda service and tap on Create function. On the next screen, the "Author from scratch" must be selected, then enter a Lambda name and choose the "Node.js 12.x" version as runtime.
Next, the dropdown box of the "Choose or create an execution role" options, select "Use an existing role," and choose the role created in the first step.
Once the Lambda is created, you can copy and paste the code below on the index.js file and save the changes.
b. Deploy to CloudFront
To deploy the Lambda@Edge on CloudFront, it is necessary to publish a new version of the Lambda. In the menu at the top of the page, choose "Actions" and "Publish new version," leave the description blank, and tap on "Publish."
After that, choose "Add trigger" and in the Designer section select CloudFront as a trigger, then complete the following options and tap on "Add":
- Configure CloudFront trigger
- Distribution: Select your Distribution ID.
- Cache behavior: Leave the value as default, or if CloudFront has more than one Cache behaviors configured, select the corresponding for the S3 Bucket.
- CloudFront event: For this example, you must choose the origin request event.
- Confirm deployment to Lambda@Edge
- Check the box to enable CloudFront as a trigger for a Lambda function and the automatic replication across multiple Regions.
The trigger should be successfully added, as shown below:
TEST THE LAMBDA@EDGE
After deploying the lambda@edge, CloudFront Distribution should be displayed the status "in progress" and, after several minutes, must change to "Deployed."
One all services were deployed correctly, open any browser (Chrome, Firefox, Safari) on a desktop or laptop and a smartphone. Enter the URL of your website and compare the results.
Note: For testing the article, we used the "dev.safetycloud.ioconnectservices.com" domain created on Amazon Route 53 that points to our CloudFront, but if you do not have a custom domain name, use the CloudFront domain name.
1. Desktop test
2. Smartphone test
Lambda@Edge is an excellent solution for CloudFront distributions if you want to improve performance, generate dynamic content based on attributes of requests or response, add security layers, or handle different origins, to mention some uses. In this article, we show how to associate with a trigger a CloudFront Distribution to serve dynamic content based on the device and use the origin request event for the cache behavior.