---
title: "AWS Lambda"
description: "Run bknd inside AWS Lambda"
tags: ["documentation"]
---

## Installation

To get started with AWS Lambda and bknd you can either install the package manually and follow the descriptions below, or use the CLI starter:

### CLI Starter

Create a new Bun CLI starter project by running the following command:

```sh
npx bknd create -i aws
```

### Manual

Create a new AWS Lambda project and then install bknd as a dependency:

<Tabs groupId='package-manager' persist items={[ 'npm', 'pnpm', 'yarn', 'bun']}>

```bash tab="npm"
npm install bknd
```

```bash tab="pnpm"
pnpm install bknd
```

```bash tab="yarn"
yarn add bknd
```

```bash tab="bun"
bun add bknd
```

</Tabs>

## Serve the API

To serve the API, you can use the `serveLambda` function of the AWS Lambda adapter.

```tsx title="index.mjs"
import { serveLambda } from "bknd/adapter/aws";
import { libsql } from "bknd";

export const handler = serveLambda({
  connection: libsql({
    url: "libsql://your-database-url.turso.io",
    authToken: "your-auth-token",
  }),
});
```

Although the runtime would support database as a file, we don't recommend it. You'd need to also bundle the native dependencies which increases the deployment size and cold start time. Instead, we recommend you to use [LibSQL on Turso](/usage/database#sqlite-using-libsql-on-turso).

## Serve the Admin UI

Lambda functions should be as small as possible. Therefore, the static files for the admin panel should not be served from node_modules like with the Node adapter.

Instead, we recommend to copy the static files and bundle them with the lambda function. To copy the static files, you can use the `copy-assets` command:

```bash
npx bknd copy-assets --out static
```

This will copy the static files to the `static` directory and then serve them from there:

```tsx title="index.mjs"
import { serveLambda } from "bknd/adapter/aws";

export const handler = serveLambda({
  connection: {
    url: process.env.DB_URL!,
    authToken: process.env.DB_AUTH_TOKEN!,
  },
  assets: {
    // [!code highlight]
    mode: "local", // [!code highlight]
    root: "./static", // [!code highlight]
  }, // [!code highlight]
});
```

## Deployment

To deploy a lambda function, you could follow these steps:

1. Create an IAM role with a trust policy that allows lambda to assume the role.
2. Attach the `AWSLambdaBasicExecutionRole` policy to the role.
3. Bundle the lambda function with the static files (e.g. using esbuild)
4. Create a zip file with the bundled lambda function
5. Create a lambda function
6. Create a function URL for the lambda function & make it publicly accessible (optional)

Depending on your use case, you may want to skip step 6 and use the AWS API Gateway to serve the lambda function. Here is an [example deployment script](https://github.com/bknd-io/bknd/blob/main/examples/aws-lambda/deploy.sh) which creates the AWS resources described above, bundles the lambda function and uploads it.

### Using the CLI starter

The CLI starter example includes a basic build script that creates the required AWS resources, copies the static files, bundles the lambda function and uploads it. To deploy the lambda function, you can run:

```bash
npm run deploy
```

To make adjustments to the lambda function created (e.g. architecture, memory, timeout, etc.) you can edit the head section of the `deploy.sh` script.

```sh title="deploy.sh"
# cat deploy.sh | head -12
FUNCTION_NAME="bknd-lambda"
ROLE_NAME="bknd-lambda-execution-role"
RUNTIME="nodejs22.x"
HANDLER="index.handler"
ARCHITECTURE="arm64" # or "x86_64"
MEMORY="1024" # in MB, 128 is the minimum
TIMEOUT="30"
ENTRY_FILE="index.mjs"
ZIP_FILE="lambda.zip"
# ...
```

To clean up AWS resources created by the deployment script, you can run:

```bash
npm run clean
```
