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:

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

npx bknd create -i aws

Serve the API

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

index.mjs
import { serveLambda } from "bknd/adapter/aws";

export const handler = serveLambda({
   connection: {
      url: process.env.DB_URL!,
      authToken: process.env.DB_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.

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:

npx bknd copy-assets --out static

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

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: {
      mode: "local",
      root: "./static"
   }
});

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 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:

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.

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:

npm run clean