# Lambda@Edge

`--preset lambda-edge` builds a function for a CloudFront origin-request trigger. It is chosen by name only. The bundle imports no `node:` module.

## Reference

|                      |                                                    |
| -------------------- | -------------------------------------------------- |
| Preset               | `lambda-edge`                                      |
| Runs on              | a Lambda@Edge origin-request trigger of CloudFront |
| `event.runtime.name` | `lambda-edge`                                      |
| Server target        | `browser`                                          |
| Lifetime, startup    | `request`, `request`                               |

### Output

```text
.output/
  hamur.json
  server/index.mjs           export { handler }   handler(event)
  public/                    for the origin (S3 or a custom origin)
```

### Config keys

This preset has no key of its own.

### Detection

None. Name it with `--preset`, `preset` in the config or `HAMUR_PRESET`; no environment variable selects it.

### Deploy command

No `hamur deploy` command: it exits 1 for this preset. Zip `.output/server/`, publish a numbered version in `us-east-1` and attach it to the cache behavior.

### Limits

- Does not run: WebSockets and `server.syncedState`, `server.pubsub`, streamed answers, `server.metrics`, image transforms at run time, private `app/env.ts` entries read at run time, Durable Object classes.
- CloudFront takes at most 1 MB, headers and body together, from an origin-request function; a larger answer becomes a 502.

The levels are the `features` of the preset: `hamur build` warns or fails when the app uses a feature the preset does not run (see [Preset features](/deploy#preset-features)).

```
.output/server/index.mjs   export { handler }   handler(event)
.output/public/            for the origin (S3 or a custom origin)
```

`handler` reads `Records[0].cf.request` (`uri`, `querystring`, `method`, `clientIp` and `headers`, whose keys are lower case and whose values are `[{ key, value }]` arrays) and answers CloudFront's shape: `status` as a string, `statusDescription`, `headers` in the same array form, `body` and `bodyEncoding` (`text`, or `base64` for an answer that is not text, JSON, XML or JavaScript). The request has a body only when the trigger has **Include Body** on; a body CloudFront cut short (`inputTruncated`) is answered `413`. Headers CloudFront does not allow in a generated response (`Connection`, `Upgrade`, `Transfer-Encoding`, `X-Amz-Cf-*`, `X-Edge-*` and the others on [the restrictions page](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/edge-function-restrictions-all.html)) are dropped, and `Content-Length` and `Via` are left to CloudFront. A 204 answer carries no body.

CloudFront accepts at most 1 MB, headers and body together, from an origin-request function (40 KB from a viewer-request one). A larger answer is replaced by `502` with `Response too large for Lambda@Edge`, so the failure is yours to read rather than CloudFront's. The `Host` header the function sees is the one CloudFront sends to the origin; forward the viewer's host with an origin request policy if the app builds absolute URLs.

Zip `.output/server/` as a Node.js function in `us-east-1`, publish a numbered version (not `$LATEST` or an alias) and attach it to the cache behavior as an origin-request trigger. Lambda@Edge has no environment variables,, so runtime config is not overridden through `HAMUR_*` variables there. The function serves no public file, does not stream, and `tasks.runOnStart` fails the build; rate limits count per instance.
