Skip to main content
AWS Lambda is a serverless compute service that runs code without requiring you to provision or manage servers. Bun can run on AWS Lambda via a Docker container image using the AWS Lambda Web Adapter.
Before continuing, make sure you have:
1

Create a Dockerfile

The AWS Lambda Web Adapter bridges Lambda’s event-based invocation model with a standard HTTP server. Create a Dockerfile in your project root:
Dockerfile
# Use the AWS Lambda adapter to handle the Lambda runtime protocol
FROM public.ecr.aws/awsguru/aws-lambda-adapter:0.9.0 AS aws-lambda-adapter

# Use the official Bun image (Debian-based for glibc compatibility)
FROM oven/bun:debian AS bun_latest

# Copy the Lambda adapter binary into the container
COPY --from=aws-lambda-adapter /lambda-adapter /opt/extensions/lambda-adapter

# Port 8080 is required by the AWS Lambda adapter
ENV PORT=8080

# /var/task is the default working directory for Lambda
WORKDIR /var/task

# Install dependencies
COPY package.json bun.lock ./
RUN bun install --production --frozen-lockfile

# Copy application source
COPY . /var/task

# Start the app
CMD ["bun", "index.ts"]
Adjust the CMD to match your application’s entry point. If you have a start script in package.json, you can use CMD ["bun", "run", "start"] instead.
Also create a .dockerignore to keep the image small:
.dockerignore
node_modules
Dockerfile*
.dockerignore
.git
.gitignore
README.md
.env
2

Write a Bun HTTP server

Your server must listen on the port defined by the PORT environment variable (Lambda sets this to 8080):
index.ts
const port = parseInt(process.env.PORT ?? "8080");

Bun.serve({
  port,
  fetch(req) {
    const url = new URL(req.url);

    if (url.pathname === "/") {
      return new Response("Hello from Bun on Lambda!");
    }

    return new Response("Not Found", { status: 404 });
  },
});

console.log(`Listening on port ${port}`);
3

Build the Docker image

Build the image targeting the linux/amd64 platform, which Lambda requires. The --provenance=false flag avoids a known ECR compatibility issue.
docker build --provenance=false --platform linux/amd64 -t bun-lambda-demo:latest .
4

Create an ECR repository

Create an Amazon Elastic Container Registry (ECR) repository to host the image, and export its URI:
export ECR_URI=$(aws ecr create-repository \
  --repository-name bun-lambda-demo \
  --region us-east-1 \
  --query 'repository.repositoryUri' \
  --output text)

echo $ECR_URI
123456789.dkr.ecr.us-east-1.amazonaws.com/bun-lambda-demo
If you use IAM Identity Center (SSO), append --profile <your-profile> to every AWS CLI command.
5

Authenticate and push the image

Log in to ECR and push the image:
aws ecr get-login-password --region us-east-1 \
  | docker login --username AWS --password-stdin $ECR_URI

docker tag bun-lambda-demo:latest ${ECR_URI}:latest
docker push ${ECR_URI}:latest
6

Create an AWS Lambda function

In the AWS Console, navigate to Lambda → Create function → Container image.
  • Give the function a name (e.g., my-bun-function).
  • Under Container image URI, click Browse images and select the image you just pushed, choosing the latest tag.
7

Configure a public URL

To expose the function over HTTPS without an API Gateway:
  1. Open Additional configurations → Networking → Function URL.
  2. Set Auth type to NONE and click Enable.
  3. Click Create function.
The function URL appears on the function’s overview page.
8

Test the function

Call the function URL directly:
curl https://<your-function-id>.lambda-url.us-east-1.on.aws/
Hello from Bun on Lambda!

Environment variables

Set environment variables in the Lambda console under Configuration → Environment variables, or via the AWS CLI:
aws lambda update-function-configuration \
  --function-name my-bun-function \
  --environment "Variables={DATABASE_URL=postgres://...,API_KEY=secret}"
Access them in your code with process.env or Bun.env:
const dbUrl = process.env.DATABASE_URL;

IAM permissions

Lambda functions execute with the permissions of their associated IAM execution role. To grant your function access to other AWS services (e.g., S3, DynamoDB), attach the relevant managed policies or inline policies to the execution role in IAM → Roles.

Build docs developers (and LLMs) love