# AWS Lambda

`--preset aws-lambda` builds a Lambda function. It is chosen by name only.

## Reference

|                      |                                                 |
| -------------------- | ----------------------------------------------- |
| Preset               | `aws-lambda`                                    |
| Runs on              | AWS Lambda behind API Gateway or a function URL |
| `event.runtime.name` | `aws-lambda`                                    |
| Server target        | `node`                                          |
| Lifetime, startup    | `request`, `request`                            |

### Output

```text
.output/
  hamur.json
  server/index.mjs           export { handler }   handler(event, context)
  public/                    for S3 or CloudFront
```

### Config keys

| Key                   | Default | What it does                                                                          |
| --------------------- | ------- | ------------------------------------------------------------------------------------- |
| `awsLambda.streaming` | `false` | `true` exports the handler through `awslambda.streamifyResponse` and streams the body |

### 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 or upload `.output/server/` as the function, and put `.output/public/` in S3 behind CloudFront.

### Limits

- Runs: private `app/env.ts` entries read at run time.
- Does not run: WebSockets and `server.syncedState`, `server.pubsub`, streamed answers, `server.metrics`, image transforms at run time, Durable Object classes.
- `tasks.runOnStart` fails the build; rate limits count per instance.
- With `awsLambda.streaming: true`, streamed answers run (stable).

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, context)
.output/public/            for S3 or CloudFront
```

`handler` reads an API Gateway REST event (payload 1.0), an API Gateway HTTP API event (payload 2.0) or a function URL event, and answers in the same shape. Repeated headers, the `cookies` array of a 2.0 request and the `set-cookie` lines of the answer are handled for you: a 2.0 answer returns them in `cookies`, a REST answer in `multiValueHeaders`. An answer whose type is not text, JSON, XML or JavaScript is returned base64 with `isBase64Encoded: true`; a request body that arrives base64 is decoded. Zip `.output/server/` as the function and set its handler to `index.handler` on a Node.js runtime. Work the app hands to `waitUntil` finishes before the answer is returned, since Lambda freezes the function after it.

The function does not serve `.output/public/`. Upload it to S3 and put it behind CloudFront (route the paths of the files to the bucket and the rest to the function); the build log says so. `tasks.runOnStart` fails the build and rate limits count per instance.

## Response streaming

```ts
export default defineConfig({ preset: "aws-lambda", awsLambda: { streaming: true } })
```

With `awsLambda.streaming: true` the handler is wrapped in `awslambda.streamifyResponse`, which Lambda's Node.js runtime provides. It writes the status and headers (and `cookies`) first and then the body as the app produces it, so streamed pages reach the client early. Streaming needs a function URL with invoke mode `RESPONSE_STREAM` or the API Gateway proxy integration with response streaming, and is not available through a function URL inside a VPC. Streamed answers may be up to 200 MB, against 6 MB buffered.
