> ## Documentation Index
> Fetch the complete documentation index at: https://docs.waniwani.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# AWS CloudFront

> Run the Waniwani relay as a Lambda@Edge function on your CloudFront distribution.

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:

```js index.mjs theme={null}
import { isAgent, recordAgentVisit } from "./relay.mjs";

export const handler = async (event) => {
  const request = event.Records[0].cf.request;
  if (isAgent(request)) {
    await recordAgentVisit(request, { key: "wwt_..." });
  }
  return request;
};
```

## Deploy with CloudFormation

You need the [AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html). 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`.

```bash theme={null}
DISTRIBUTION_ID=YOUR_DISTRIBUTION_ID
sam deploy --stack-name "waniwani-relay-$DISTRIBUTION_ID" \
  --region us-east-1 --resolve-s3 --no-fail-on-empty-changeset \
  --capabilities CAPABILITY_IAM \
  --parameter-overrides "DistributionId=$DISTRIBUTION_ID" 'PathPatterns=*'
```

**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](https://docs.waniwani.ai/agent-traffic/agent-pages.md) or [blocks an agent](https://docs.waniwani.ai/agent-traffic/blocking.md), the zip from the setup screen has two functions in place of `index.mjs`:

| File | Attach as | What it does |
| - | - | - |
| `viewer-request.mjs` | **Viewer request** | Records the visits of AI agents, sends a `403` to the agents that you block, and marks the requests that get an agent page |
| `origin-request.mjs` | **Origin request** | Serves the agent pages |

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.

```js viewer-request.mjs theme={null}
import { handleViewerRequest, isAgent, recordAgentVisit } from "./relay.mjs";

export const handler = async (event) => {
  const request = event.Records[0].cf.request;
  const visit = isAgent(request) ? recordAgentVisit(request, { key: "wwt_..." }) : null;
  const answer = await handleViewerRequest(request, { key: "wwt_..." });
  await visit;
  return answer ?? request;
};
```

```js origin-request.mjs theme={null}
import { serveAgentPage } from "./relay.mjs";

export const handler = async (event) => {
  const request = event.Records[0].cf.request;
  return (await serveAgentPage(request, { key: "wwt_..." })) ?? request;
};
```

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](https://docs.waniwani.ai/agent-traffic/agent-pages.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.