aboutsummaryrefslogtreecommitdiff
path: root/packages/bun-lambda/README.md
blob: 015a6e26c71fe3ed9911d61e50cdcb02cc20b8e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# bun-lambda

A custom runtime layer that runs Bun on AWS Lambda.

## Setup

First, you will need to deploy the layer to your AWS account. Clone this repository and run the `publish-layer` script to get started.

```sh
git clone git@github.com:oven-sh/bun.git
cd packages/bun-lambda
bun install
bun run publish-layer
```

### `bun run build-layer`

Builds a Lambda layer for Bun and saves it to a `.zip` file.

| Flag        | Description                                                          | Default                |
| ----------- | -------------------------------------------------------------------- | ---------------------- |
| `--arch`    | The architecture, either: "x64" or "aarch64"                         | aarch64                |
| `--release` | The release of Bun, either: "latest", "canary", or a release "x.y.z" | latest                 |
| `--output`  | The path to write the layer as a `.zip`.                             | ./bun-lambda-layer.zip |

Example:

```sh
bun run build-layer -- \
  --arch x64 \
  --release canary \
  --output /path/to/layer.zip
```

### `bun run publish-layer`

Builds a Lambda layer for Bun then publishes it to your AWS account.

| Flag       | Description                               | Default |
| ---------- | ----------------------------------------- | ------- |
| `--layer`  | The layer name.                           | bun     |
| `--region` | The region name, or "\*" for all regions. |         |
| `--public` | If the layer should be public.            | false   |

Example:

```sh
bun run publish-layer -- \
  --arch aarch64 \
  --release latest \
  --output /path/to/layer.zip \
  --region us-east-1
```

## Usage

Once you publish the layer to your AWS account, you can create a Lambda function that uses the layer.

Here's an example function that can run on Lambda using the layer for Bun:

### HTTP events

When an event is triggered from [API Gateway](https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html), the layer transforms the event payload into a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request). This means you can test your Lambda function locally using `bun run`, without any code changes.

```ts
export default {
  async fetch(request: Request): Promise<Response> {
    console.log(request.headers.get("x-amzn-function-arn"));
    // ...
    return new Response("Hello from Lambda!", {
      status: 200,
      headers: {
        "Content-Type": "text/plain",
      },
    });
  },
};
```

### Non-HTTP events

For non-HTTP events — S3, SQS, EventBridge, etc. — the event payload is the body of the [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request).

```ts
export default {
  async fetch(request: Request): Promise<Response> {
    const event = await request.json();
    // ...
    return new Response();
  },
};
```
>Stop reading `"bun"` from package.jsonGravatar Jarred Sumner 1-29/+0 2022-01-27[bunfig] Implement config file formatGravatar Jarred Sumner 20-156/+667 2022-01-27Don't look like a crash when CLI args are missingGravatar Jarred Sumner 1-0/+3 2022-01-27[cli] Add support for commands which optionally have one argumentGravatar Jarred Sumner 3-12/+22 2022-01-26Update MakefileGravatar Jarred Sumner 1-1/+1 2022-01-26print file name on panicGravatar Jarred Sumner 1-1/+1 2022-01-25mergeGravatar Jarred Sumner 2-1/+2 2022-01-25Slightly saferGravatar Jarred Sumner 1-1/+1 2022-01-25Split http into filesGravatar Jarred Sumner 7-1079/+1055 2022-01-25Update DockerfileGravatar Jarred Sumner 1-1/+1 2022-01-25Fix up dev containerGravatar Jarred Sumner 2-5/+1 2022-01-25On successful connect, switch to non-blocking sockets until we're about to closeGravatar Jarred SUmner 1-2/+18 2022-01-25Fix getsockopt()Gravatar Jarred SUmner 1-2/+5 2022-01-24Automatically retry on would blockGravatar Jarred SUmner 1-8/+4 2022-01-24Fallback to readev / writevGravatar Jarred SUmner 1-7/+226 2022-01-24No io_uring for Ubuntu 20.04Gravatar Jarred SUmner 6-27/+50 2022-01-23Update io_linux.zigGravatar Jarred Sumner 1-0/+1 2022-01-23[linux][http] return errno instead of unexpectedGravatar Jarred Sumner 1-2/+31 2022-01-23[http] Remove usages of `unreachable` in syscall error handlingGravatar Jarred Sumner 1-10/+0 2022-01-23Update io_linux.zigGravatar Jarred Sumner 1-0/+1 2022-01-23Use non-cancellable syscalls for HTTP & use errno for errorsGravatar Jarred Sumner 3-78/+933 2022-01-23Improve error message when `bun upgrade` failsGravatar Jarred Sumner 1-1/+1 2022-01-23NiceGravatar Jarred Sumner 1-1/+1 2022-01-23Update analytics_thread.zigGravatar Jarred Sumner 1-0/+8