Introduction
Bun is a fast all-in-one JavaScript runtime & toolkit with built-in bundler, test runner, and Node.js-compatible package manager. This guide uses Hono, a lightweight and ultrafast web framework.
Prerequisites
- a UNIX-like system (see WSL if you are using Windows), in order to use the Valyent CLI
- a Valyent account, with a registered payment method (you can sign up here)
- Bun installed on your machine
Installing the CLI
curl -L https://cli.valyent.cloud | sh
You can inspect the installation script here and the GitHub repository.
Authenticating
Initializing the project
First, let’s create a new directory and initialize a Bun project:
mkdir bun-app && cd bun-app
bun init
Install the required dependencies:
bun add hono
bun add -d @types/bun
Create an index.ts
file:
import { Hono } from "hono";
const app = new Hono();
app.get("/", (c) => {
return c.text("Hello Lambda World!");
});
export default app;
Now, initialize the valyent.toml
configuration file:
This command will guide you through selecting or creating a project and an associated application.
Adding the Dockerfile
Create a Dockerfile:
Add the following content:
# use the official Bun image
FROM oven/bun:1 AS base
WORKDIR /usr/src/app
# install dependencies into temp directory
# this will cache them and speed up future builds
FROM base AS install
RUN mkdir -p /temp/dev
COPY package.json bun.lockb /temp/dev/
RUN cd /temp/dev && bun install --frozen-lockfile
# install with --production (exclude devDependencies)
RUN mkdir -p /temp/prod
COPY package.json bun.lockb /temp/prod/
RUN cd /temp/prod && bun install --frozen-lockfile --production
# copy node_modules from temp directory
# then copy all (non-ignored) project files into the image
FROM base AS prerelease
COPY --from=install /temp/dev/node_modules node_modules
COPY . .
# [optional] tests & build
ENV NODE_ENV=production
RUN bun test
RUN bun run build
# copy production dependencies and source code into final image
FROM base AS release
COPY --from=install /temp/prod/node_modules node_modules
COPY --from=prerelease /usr/src/app/index.ts .
COPY --from=prerelease /usr/src/app/package.json .
# run the app
USER bun
EXPOSE 3000/tcp
ENTRYPOINT [ "bun", "run", "index.ts" ]
Deploy the project
To expose the application’s port, we need to set up a gateway (in this case, the port 3000):
Deploy your application to Valyent:
Then, you can type the following:
to open your application in the browser.