waniwani-relay.zip. The zip has:
index.mjs, the function, with your key and your regionrelay.mjs, the relay, with its license noticetemplate.yaml, a CloudFormation template that creates the function and attaches it to your distribution
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.
Deploy in the AWS console
Select AWS console in the setup screen. It shows each click. In short:- In
us-east-1, create a Node.js function. Lambda@Edge functions must be in this region. - Replace the trust policy of its role with the policy from the setup screen. The policy lets
edgelambda.amazonaws.comrun the function. - Upload the zip. Make sure that the handler is
index.handler. - Publish a numbered version of the function, and copy its ARN. CloudFront accepts only a numbered version.
- 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 ofindex.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
- Set the handler of the first function to
viewer-request.handler. - Create a second function, for example
waniwani-relay-pages, from the same zip. Set its handler toorigin-request.handlerand its timeout to 10 seconds. Publish a version of it. - 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
recordAgentVisitwaits 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/blogand/blog/*. Also add the paths of your agent pages.