Skip to main content
On CloudFront, the relay is a Lambda@Edge function on the viewer request of the cache behaviors that serve your pages. The setup screen gives you a zip, waniwani-relay.zip. The zip has:
  • index.mjs, the function, with your key and your region
  • relay.mjs, the relay, with its license notice
  • template.yaml, a CloudFormation template that creates the function and attaches it to your distribution
Upload the zip with no changes. The function in the zip looks like this:
index.mjs

Deploy with CloudFormation

You need the AWS SAM CLI. You also need permission to create CloudFormation stacks, IAM roles and Lambda functions, and to change your distribution.
1

Download the zip

In the setup screen, select CloudFormation and click Download waniwani-relay.zip. Unzip it into a new folder.
2

Deploy the stack

Find the ID of your distribution in the ID column of the CloudFront console. In the folder, run these commands. Put your distribution ID in place of YOUR_DISTRIBUTION_ID.
3

Check

Wait until the distribution shows Deployed. This can take some minutes. Then, in the setup screen, click Check now.
To update the relay, download the zip again and run the same commands.

Deploy in the AWS console

Select AWS console in the setup screen. It shows each click. In short:
  1. In us-east-1, create a Node.js function. Lambda@Edge functions must be in this region.
  2. Replace the trust policy of its role with the policy from the setup screen. The policy lets edgelambda.amazonaws.com run the function.
  3. Upload the zip. Make sure that the handler is index.handler.
  4. Publish a numbered version of the function, and copy its ARN. CloudFront accepts only a numbered version.
  5. On each behavior that serves pages, set Viewer request to Lambda@Edge and paste the version ARN. Leave Include body off.

With agent pages or blocking

When the website serves agent pages or blocks an agent, the zip from the setup screen has two functions in place of index.mjs: A Lambda@Edge response can be 40 KB at most on a viewer request and 1 MB on an origin request. Most agent pages are larger than 40 KB. Thus the agent page comes from the origin request function. People and crawlers keep the normal cache entry. The zip also has relay.mjs, its license notice and template.yaml. The CloudFormation commands are the same. The template creates the two functions and attaches them.
viewer-request.mjs
origin-request.mjs
In the AWS console, the steps change:
  1. Set the handler of the first function to viewer-request.handler.
  2. Create a second function, for example waniwani-relay-pages, from the same zip. Set its handler to origin-request.handler and its timeout to 10 seconds. Publish a version of it.
  3. On each behavior, set Viewer request to the version ARN of the first function and Origin request to the version ARN of the second function. Leave Include body off for both.

Notes

  • recordAgentVisit waits for its event, with a limit of 1 second, because Lambda stops when the handler returns. Only agent requests wait.
  • With Only some paths, you make one behavior for each path pattern. /blog/* needs /blog and /blog/*. Also add the paths of your agent pages.