Deploy Lambda@Edge function to CloudFront

Deploy Lambda@Edge function to CloudFront

In this section, you add CloudFront as a trigger for your Lambda function. Requests to the /serverless path go through the CloudFront distribution, which invokes the Lambda@Edge function on the Origin request event before forwarding the request to the origin.

CloudFront Lab

Lambda@Edge functions must be created in the US East (N. Virginia) us-east-1 region. Make sure the region selector in the console shows United States (N. Virginia) before you continue.

Add the CloudFront trigger

  1. In the Function overview section, click Add trigger.

CloudFront Lab

  1. On the Add trigger page:
    • In Trigger configuration, select CloudFront.
    • Click Deploy to Lambda@Edge.

CloudFront Lab

Lambda@Edge has some restrictions: only Node.js and Python runtimes are supported, and environment variables, Dead Letter Queues (DLQ), and Amazon VPC cannot be used.

  1. Configure the trigger with the following settings:
    • Select an option: Configure new CloudFront trigger
    • Distribution: select the distribution you created earlier
    • Cache behavior: /serverless
    • CloudFront event: Origin request
    • Include body: leave unchecked
    • Check Confirm deploy to Lambda@Edge
    • Click Add

CloudFront Lab

If the distribution or the /serverless cache behavior does not appear in the list, click the refresh button next to the field. If /serverless is still missing, go back and check that you created that cache behavior in the earlier section.

  1. After the trigger is added, you see the message The trigger was successfully added to function. The Function overview diagram now shows CloudFront as a trigger for the function’s Version 1.

CloudFront Lab

Lambda@Edge triggers always attach to a numbered function version, never to $LATEST. If you change the code later, you must publish a new version and update the trigger to use it.

CloudFront needs about 5 minutes to deploy the change to all edge locations. Wait until the distribution’s Last modified status in the CloudFront console changes from Deploying to a date before you test.

Check the function replicas

  1. In the left navigation pane of the Lambda console, click Replicas. You see a replica named with the description Replica created by Lambda@Edge.

CloudFront Lab

  1. Click the replica name to open it. The page shows This is a replica function, and the Master ARN field points to the original function version in us-east-1.

CloudFront Lab

Lambda@Edge copies the function to AWS Regions that have a CloudFront Regional Edge Cache. The Replicas list only shows replicas in the region currently selected, so switch regions to see others. A replica usually appears in a region only after requests have been served there. Replicas are read-only, and you cannot edit or delete them directly.

Test the distribution

  1. When the distribution has finished deploying, open a browser and go to distribution domain. The Lambda@Edge Lab page appears with the table of request headers sent by the API.

CloudFront Lab

If you get an error or old content, wait a few more minutes for the deployment to finish, then refresh. To check the function’s logs, open CloudWatch Logs in the region closest to you, not in us-east-1. Lambda@Edge writes logs in the region where the function ran, in a log group named /aws/lambda/us-east-1.LambdaEdgeImmersionDayLabFunction.