Connect with us

Get more updates and further details about your project right in your mailbox.

Thank you!
Oops! Something went wrong while submitting the form.
October 6, 2023

How to deploy Bun.js in AWS Lambda?

The best time to establish protocols with your clients is when you onboard them.

Heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

Why Bun?

JavaScript is one of the most popular and widely used programming languages in the world. It powers web applications, mobile apps, desktop software, and even servers. However, JavaScript also has some drawbacks, such as slow startup times, high memory consumption, and compatibility issues with different engines and platforms.

That’s where Bun JS comes in. Bun JS is a new JavaScript runtime that aims to solve these problems by offering a fast, lightweight, and cross-platform alternative to Node.js. Bun JS is written in Zig, a low-level programming language that focuses on performance and safety. It uses JavaScript Core as its engine, which is the same engine that powers Safari and iOS devices. This means that Bun JS can run JavaScript code faster and more efficiently than Node.js, which uses V8 as its engine.

Bun JS also provides a complete toolkit for JavaScript development, including a bundler, a test runner, and a package manager. These tools are designed to be compatible with Node.js APIs, so developers can easily migrate their existing code and use their favorite libraries and frameworks. Bun JS also supports TypeScript, which adds static typing and other features.

Bun JS is still in development and has not reached a stable version yet. However, it has already attracted a lot of attention and interest from the JavaScript community. Some of the features that are planned for future releases include Web Assembly support, native modules, and more built-in commands.

Today let’s focus on deploying Bun.js in AWS Lambda.

There are two ways to deploy Bun in Lambda, one is through adding a custom layer in Lambda, the other one through containerizing the Bun application using Docker and deploying it in Lambda.

Deploying Bun by adding layer:

To run Bun in Lambda first you need to add a custom Lambda layer. To do this you need a Unix(Mac or Linux) based operating system to install Bun.js in your system. You need to install it in your system in order to build the bun layer.

  1. Install Bun.js in a local system: By using the following command install Bun.js.
curl -fsSL https://bun.sh/install | bash

Reboot the system if required.

2. Clone the GitHub repo: Clone the official Bun.js repo to build the Lambda layer and publish.

git clone https://github.com/oven-sh/bun.git

3. Install and publish layer: Follow the commands to install and publish layer.

Note: Before continuing to build the layer make sure the aws cli is properly configured with your access ID and secret.

You need to get something like this

Note: The layer supports only arm64 architecture as of now.

4. Create and configure Lambda function: In the AWS console create a function with a desired name with the runtime as Amazon Linux 2 and architecture as arm64.

After creating the function, add the custom layer that has been published to the Lambda function.

5. Add handler: In the created Lambda function get runtime settings and add the handler as “index.fetch”.

That’s it, Lambda with custom runtime is ready to serve. You can now host your Bun.js application.

Here is a sample code that I have added to test the application.

Response:

Deploying Bun by containerizing:

In this method you need to containerize the Bun.js application to deploy it into Lambda. This is a common way to deploy an application that has custom runtimes that are not supported by Lambda.

Add the Dockerfile to the root folder of the Bun.js application that you want to deploy.

Add the following code to the Dockerfile.

“FROM oven/bun:latest” here Docker uses the pre-built image oven/bun where the image is already configured with Bun.js.

The third line “EXPOSE 3000” is to port forward the application since my application runs on port 3000.

The code for the simple Bun.js application is as follows:

console.log(`Listening on localhost:${server.port}`);

Now we need to containerize the application. To containerize follow the command.

sudo docker build -t bun-runtime .

This command is used to build a Docker image with the tag “bun-runtime.” Make sure you have installed docker in your local system.

After containerizing the application we need to push it to ECR.

So let’s create an ECR repository to store our container image which will be later accessed by Lambda to deploy the container.

Now follow the commands to push the container image into the ECR

  1. Tag the docker image to push it to ECR
sudo docker tag bun-runtime 837010728556.dkr.ecr.us-east-1.amazonaws.com/bun-repo:bun-runtime

2. Now login to the ECR through the terminal

aws ecr get-login-password — region us-east-1 | sudo docker login — username AWS — password-stdin 837010728556.dkr.ecr.us-east-1.amazonaws.com

3. Push the container image into the ECR

sudo docker push 837010728556.dkr.ecr.us-east-1.amazonaws.com/bun-repo:bun-runtime

Now you can see in the console the container image is pushed into the repo

Now go to the console and create a new Lambda function and select “Container image”

Now select the container image that is already pushed into ECR.

Then hit create function. That’s it, Lambda will deploy the container image. Now you can check the application using the function URL.

These are the two common ways to deploy Bun.js in AWS Lambda, even though we have alternate methods to deploy Bun.js to Lambda, the performance of lambda is compromised by these methods. Although we can’t use these methods to deploy production applications we can use it to experiment and learn things.

Happy Hacking!!!


CodeStax.Ai
Profile
October 6, 2023
-
6
min read
Subscribe to our newsletter
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Share this article:

More articles