Using Bun.js with AWS Lambda Custom Runtimes

Mustafa Acar
Yıldız Tech
Published in
4 min readNov 2, 2023

AWS Lambda supports Java, Go, PowerShell, Node.js, C#, Python, and Ruby programming languages. For languages other than these, we can integrate the desired programming language using the Runtime API.

Runtime API

It is the API through which we communicate with the Lambda service. Normally, within Lambda, we only have a handler function that processes the request. With Custom Runtime, in addition to the handler function, we have a separate layer integrated with the Runtime API responsible for creating, executing, and reporting responses and errors.

The difference here is that we write the function which is responsible for running the handler.

Runtime Function is responsible for executing the Handler Function and transmitting the processes to the Lambda Service via the Runtime API.

Next invocation

GET /runtime/invocation/next

We can retrieve the request/event that comes to our Lambda function from the Lambda service here. In the data, we obtain the content of the request (payload), where it was triggered, and the ID of the execution, such as Runtime-Aws-Request-Id

Initialization error

This is the endpoint where we report errors we encounter when managing our handler function during the function creation phase.

POST — /runtime/init/error

Invocation error

POST — /runtime/invocation/AwsRequestId/error

This is the endpoint where we report errors that occur when running our handler with the payload.

Creating Custom Runtime

To create a Custom Runtime, we select Provide Your own bootstrap on Amazon Linux 2in the Runtime option. By default, our Lambda is created as follows.

Lambda will initially execute the shell script file named bootstrap as the entry point. We can either upload this file to Lambda or add it as a Layer. Here, we see variables like $_HANDLERand AWS_LAMBDA_RUNTIME_API; these are environment variables defined by AWS. To see all of them, visit: AWS Environment Variables

If you want to test it, you should download the files to your computer, rename the bootstrap.sh.sample file to bootstrap and the hello.sh.sample file to hello.sh, and grant executable permissions with the chmod +x bootstrap and chmod +x hello.sh commands before re-uploading it to Lambda.

Custom Runtimes now with BUN.JS 🚀

Bun.js is a JavaScript runtime, just like Node.js.

There is an official integration of Bun.js written by Bun.js developers.

To do this, we download the Bun.js repository:

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

Here, we are building our Layer:

bun run build-layer -- \
--arch x64 \
--release canary \
--output ~/bun-layer.zip

Inside the resulting zip file, we have:

  1. The bootstrap file, which will serve as the entry point for Lambda.

2. bun an executable x64 Bun.js.

3. runtime.ts, which integrates with the Runtime API and orchestrates the Handler Function. It receives information from the Lambda service for the request/event created with the Runtime API, then executes the handler function with the payload of the request, and then communicates with the client to send the response to the client through the Runtime API.

Now attach the generated zip file as a layer.

We just have created our layer. Now, let’s add to our Lambda function.

Now, let’s integrate our Lambda’s handler function.

Conclusion

--

--