diff --git a/docs/docs.json b/docs/docs.json index f9791f4081..9eb1f9513f 100644 --- a/docs/docs.json +++ b/docs/docs.json @@ -298,7 +298,14 @@ { "group": "Deployment", "icon": "rocket", - "pages": ["/guides/deployment/vercel", "/guides/deployment/railway", "/guides/deployment/render"] + "pages": [ + "/guides/deployment/vercel", + "/guides/deployment/railway", + "/guides/deployment/render", + "/guides/deployment/aws-lambda", + "/guides/deployment/digital-ocean", + "/guides/deployment/google-cloud-run" + ] }, { "group": "Runtime & Debugging", diff --git a/docs/guides/deployment/aws-lambda.mdx b/docs/guides/deployment/aws-lambda.mdx new file mode 100644 index 0000000000..31e728b217 --- /dev/null +++ b/docs/guides/deployment/aws-lambda.mdx @@ -0,0 +1,204 @@ +--- +title: Deploy a Bun application on AWS Lambda +sidebarTitle: Deploy on AWS Lambda +mode: center +--- + +[AWS Lambda](https://aws.amazon.com/lambda/) is a serverless compute service that lets you run code without provisioning or managing servers. + +In this guide, we will deploy a Bun HTTP server to AWS Lambda using a `Dockerfile`. + + + Before continuing, make sure you have: + + - A Bun application ready for deployment + - An [AWS account](https://aws.amazon.com/) + - [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html) installed and configured + - [Docker](https://docs.docker.com/get-started/get-docker/) installed and added to your `PATH` + + + +--- + + + + Make sure you're in the directory containing your project, then create a new `Dockerfile` in the root of your project. This file contains the instructions to initialize the container, copy your local project files into it, install dependencies, and start the application. + + ```docker Dockerfile icon="docker" + # Use the official AWS Lambda adapter image to handle the Lambda runtime + FROM public.ecr.aws/awsguru/aws-lambda-adapter:0.9.0 AS aws-lambda-adapter + + # Use the official Bun image to run the application + FROM oven/bun:debian AS bun_latest + + # Copy the Lambda adapter into the container + COPY --from=aws-lambda-adapter /lambda-adapter /opt/extensions/lambda-adapter + + # Set the port to 8080. This is required for the AWS Lambda adapter. + ENV PORT=8080 + + # Set the work directory to `/var/task`. This is the default work directory for Lambda. + WORKDIR "/var/task" + + # Copy the package.json and bun.lock into the container + COPY package.json bun.lock ./ + + # Install the dependencies + RUN bun install --production --frozen-lockfile + + # Copy the rest of the application into the container + COPY . /var/task + + # Run the application. + CMD ["bun", "index.ts"] + ``` + + + Make sure that the start command corresponds to your application's entry point. This can also be `CMD ["bun", "run", "start"]` if you have a start script in your `package.json`. + + This image installs dependencies and runs your app with Bun inside a container. If your app doesn't have dependencies, you can omit the `RUN bun install --production --frozen-lockfile` line. + + + Create a new `.dockerignore` file in the root of your project. This file contains the files and directories that should be _excluded_ from the container image, such as `node_modules`. This makes your builds faster and smaller: + + ```docker .dockerignore icon="Docker" + node_modules + Dockerfile* + .dockerignore + .git + .gitignore + README.md + LICENSE + .vscode + .env + # Any other files or directories you want to exclude + ``` + + + Make sure you're in the directory containing your `Dockerfile`, then build the Docker image. In this case, we'll call the image `bun-lambda-demo` and tag it as `latest`. + + ```bash terminal icon="terminal" + # cd /path/to/your/app + docker build --provenance=false --platform linux/amd64 -t bun-lambda-demo:latest . + ``` + + + To push the image to AWS Lambda, we first need to create an [ECR repository](https://aws.amazon.com/ecr/) to push the image to. + + By running the following command, we: + - Create an ECR repository named `bun-lambda-demo` in the `us-east-1` region + - Get the repository URI, and export the repository URI as an environment variable. This is optional, but make the next steps easier. + + ```bash terminal icon="terminal" + export ECR_URI=$(aws ecr create-repository --repository-name bun-lambda-demo --region us-east-1 --query 'repository.repositoryUri' --output text) + echo $ECR_URI + ``` + ```txt + [id].dkr.ecr.us-east-1.amazonaws.com/bun-lambda-demo + ``` + + + If you're using IAM Identity Center (SSO) or have configured AWS CLI with profiles, you'll need to add the `--profile` flag to your AWS CLI commands. + + For example, if your profile is named `my-sso-app`, use `--profile my-sso-app`. Check your AWS CLI configuration with `aws configure list-profiles` to see available profiles. + + ```bash terminal icon="terminal" + export ECR_URI=$(aws ecr create-repository --repository-name bun-lambda-demo --region us-east-1 --profile my-sso-app --query 'repository.repositoryUri' --output text) + echo $ECR_URI + ``` + + + + + Log in to the ECR repository: + + ```bash terminal icon="terminal" + aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin $ECR_URI + ``` + ```txt + Login Succeeded + ``` + + + If using a profile, use the `--profile` flag: + + ```bash terminal icon="terminal" + aws ecr get-login-password --region us-east-1 --profile my-sso-app | docker login --username AWS --password-stdin $ECR_URI + ``` + + + + + + Make sure you're in the directory containing your `Dockerfile`, then tag the docker image with the ECR repository URI. + + ```bash terminal icon="terminal" + docker tag bun-lambda-demo:latest ${ECR_URI}:latest + ``` + + Then, push the image to the ECR repository. + + ```bash terminal icon="terminal" + docker push ${ECR_URI}:latest + ``` + + + Go to **AWS Console** > **Lambda** > [**Create Function**](https://us-east-1.console.aws.amazon.com/lambda/home?region=us-east-1#/create/function?intent=authorFromImage) > Select **Container image** + + Make sure you've selected the right region, this URL defaults to `us-east-1`. + + + ![Create Function](/images/guides/lambda1.png) + + + Give the function a name, like `my-bun-function`. + + + Then, go to the **Container image URI** section, click on **Browse images**. Select the image we just pushed to the ECR repository. + + + ![Select Container Repository](/images/guides/lambda2.png) + + + Then, select the `latest` image, and click on **Select image**. + + + ![Select Container Image](/images/guides/lambda3.png) + + + + To get a public URL for the function, we need to go to **Additional configurations** > **Networking** > **Function URL**. + + Set this to **Enable**, with Auth Type **NONE**. + + + ![Set the Function URL](/images/guides/lambda4.png) + + + + Click on **Create function** at the bottom of the page, this will create the function. + + + ![Create Function](/images/guides/lambda6.png) + + + + + Once the function has been created you'll be redirected to the function's page, where you can see the function URL in the **"Function URL"** section. + + + ![Function URL](/images/guides/lambda5.png) + + + + 🥳 Your app is now live! To test the function, you can either go to the **Test** tab, or call the function URL directly. + + ```bash terminal icon="terminal" + curl -X GET https://[your-function-id].lambda-url.us-east-1.on.aws/ + ``` + ```txt + Hello from Bun on Lambda! + ``` + + + diff --git a/docs/guides/deployment/digital-ocean.mdx b/docs/guides/deployment/digital-ocean.mdx new file mode 100644 index 0000000000..473b7a7a28 --- /dev/null +++ b/docs/guides/deployment/digital-ocean.mdx @@ -0,0 +1,161 @@ +--- +title: Deploy a Bun application on DigitalOcean +sidebarTitle: Deploy on DigitalOcean +mode: center +--- + +[DigitalOcean](https://www.digitalocean.com/) is a cloud platform that provides a range of services for building and deploying applications. + +In this guide, we will deploy a Bun HTTP server to DigitalOcean using a `Dockerfile`. + + + Before continuing, make sure you have: + + - A Bun application ready for deployment + - A [DigitalOcean account](https://www.digitalocean.com/) + - [DigitalOcean CLI](https://docs.digitalocean.com/reference/doctl/how-to/install/#step-1-install-doctl) installed and configured + - [Docker](https://docs.docker.com/get-started/get-docker/) installed and added to your `PATH` + + + +--- + + + + Create a new Container Registry to store the Docker image. + + + + In the DigitalOcean dashboard, go to [**Container Registry**](https://cloud.digitalocean.com/registry), and enter the details for the new registry. + + + ![DigitalOcean registry dashboard](/images/guides/digitalocean-7.png) + + + Make sure the details are correct, then click **Create Registry**. + + + + ```bash terminal icon="terminal" + doctl registry create bun-digitalocean-demo + ``` + ```txt + Name Endpoint Region slug + bun-digitalocean-demo registry.digitalocean.com/bun-digitalocean-demo sfo2 + ``` + + + + + You should see the new registry in the [**DigitalOcean registry dashboard**](https://cloud.digitalocean.com/registry): + + + ![DigitalOcean registry dashboard](/images/guides/digitalocean-1.png) + + + + Make sure you're in the directory containing your project, then create a new `Dockerfile` in the root of your project. This file contains the instructions to initialize the container, copy your local project files into it, install dependencies, and start the application. + + ```docker Dockerfile icon="docker" + # Use the official Bun image to run the application + FROM oven/bun:debian + + # Set the work directory to `/app` + WORKDIR /app + + # Copy the package.json and bun.lock into the container + COPY package.json bun.lock ./ + + # Install the dependencies + RUN bun install --production --frozen-lockfile + + # Copy the rest of the application into the container + COPY . . + + # Expose the port (DigitalOcean will set PORT env var) + EXPOSE 8080 + + # Run the application + CMD ["bun", "index.ts"] + ``` + + + Make sure that the start command corresponds to your application's entry point. This can also be `CMD ["bun", "run", "start"]` if you have a start script in your `package.json`. + + This image installs dependencies and runs your app with Bun inside a container. If your app doesn't have dependencies, you can omit the `RUN bun install --production --frozen-lockfile` line. + + + Create a new `.dockerignore` file in the root of your project. This file contains the files and directories that should be _excluded_ from the container image, such as `node_modules`. This makes your builds faster and smaller: + + ```docker .dockerignore icon="Docker" + node_modules + Dockerfile* + .dockerignore + .git + .gitignore + README.md + LICENSE + .vscode + .env + # Any other files or directories you want to exclude + ``` + + + Before building and pushing the Docker image, authenticate Docker with the DigitalOcean Container Registry: + + ```bash terminal icon="terminal" + doctl registry login + ``` + ```txt + Successfully authenticated with registry.digitalocean.com + ``` + + + This command authenticates Docker with DigitalOcean's registry using your DigitalOcean credentials. Without this step, the build and push command will fail with a 401 authentication error. + + + + Make sure you're in the directory containing your `Dockerfile`, then build and push the Docker image to the DigitalOcean registry in one command: + + ```bash terminal icon="terminal" + docker buildx build --platform=linux/amd64 -t registry.digitalocean.com/bun-digitalocean-demo/bun-digitalocean-demo:latest --push . + ``` + + + If you're building on an ARM Mac (M1/M2), you must use `docker buildx` with `--platform=linux/amd64` to ensure compatibility with DigitalOcean's infrastructure. Using `docker build` without the platform flag will create an ARM64 image that won't run on DigitalOcean. + + + Once the image is pushed, you should see it in the [**DigitalOcean registry dashboard**](https://cloud.digitalocean.com/registry): + + + ![DigitalOcean registry dashboard](/images/guides/digitalocean-2.png) + + + + In the DigitalOcean dashboard, go to [**App Platform**](https://cloud.digitalocean.com/apps) > **Create App**. We can create a project directly from the container image. + + + ![DigitalOcean App Platform project dashboard](/images/guides/digitalocean-3.png) + + + Make sure the details are correct, then click **Next**. + + + ![DigitalOcean App Platform service dashboard](/images/guides/digitalocean-4.png) + + + Review and configure resource settings, then click **Create app**. + + + ![DigitalOcean App Platform service dashboard](/images/guides/digitalocean-6.png) + + + + 🥳 Your app is now live! Once the app is created, you should see it in the App Platform dashboard with the public URL. + + + ![DigitalOcean App Platform app dashboard](/images/guides/digitalocean-5.png) + + + + diff --git a/docs/guides/deployment/google-cloud-run.mdx b/docs/guides/deployment/google-cloud-run.mdx new file mode 100644 index 0000000000..153f61fc69 --- /dev/null +++ b/docs/guides/deployment/google-cloud-run.mdx @@ -0,0 +1,197 @@ +--- +title: Deploy a Bun application on Google Cloud Run +sidebarTitle: Deploy on Google Cloud Run +mode: center +--- + +[Google Cloud Run](https://cloud.google.com/run) is a managed platform for deploying and scaling serverless applications. Google handles the infrastructure for you. + +In this guide, we will deploy a Bun HTTP server to Google Cloud Run using a `Dockerfile`. + + + Before continuing, make sure you have: + + - A Bun application ready for deployment + - A [Google Cloud account](https://cloud.google.com/) with billing enabled + - [Google Cloud CLI](https://cloud.google.com/sdk/docs/install) installed and configured + + + +--- + + +Initialize gcloud by select/creating a project}> + +Make sure that you've initialized the Google Cloud CLI. This command logs you in, and prompts you to either select an existing project or create a new one. + +For more help with the Google Cloud CLI, see the [official documentation](https://docs.cloud.google.com/sdk/gcloud/reference/init). + +```bash terminal icon="terminal" +gcloud init +``` + +```txt +Welcome! This command will take you through the configuration of gcloud. + +You must sign in to continue. Would you like to sign in (Y/n)? Y +You are signed in as [email@example.com]. + +Pick cloud project to use: + [1] existing-bun-app-1234 + [2] Enter a project ID + [3] Create a new project +Please enter numeric choice or text value (must exactly match list item): 3 + +Enter a Project ID. my-bun-app +Your current project has been set to: [my-bun-app] + +The Google Cloud CLI is configured and ready to use! +``` + + + +Set variables for your project ID and number so they're easier to reuse in the following steps. + +```bash terminal icon="terminal" +PROJECT_ID=$(gcloud projects list --format='value(projectId)' --filter='name="my bun app"') +PROJECT_NUMBER=$(gcloud projects list --format='value(projectNumber)' --filter='name="my bun app"') + +echo $PROJECT_ID $PROJECT_NUMBER +``` + +```txt +my-bun-app-... [PROJECT_NUMBER] +``` + + + +List your available billing accounts and link one to your project: + +```bash terminal icon="terminal" +gcloud billing accounts list +``` + +```txt +ACCOUNT_ID NAME OPEN MASTER_ACCOUNT_ID +[BILLING_ACCOUNT_ID] My Billing Account True +``` + +Link your billing account to your project. Replace `[BILLING_ACCOUNT_ID]` with the ID of your billing account. + +```bash terminal icon="terminal" +gcloud billing projects link $PROJECT_ID --billing-account=[BILLING_ACCOUNT_ID] +``` + +```txt +billingAccountName: billingAccounts/[BILLING_ACCOUNT_ID] +billingEnabled: true +name: projects/my-bun-app-.../billingInfo +projectId: my-bun-app-... +``` + + + +Activate the necessary services and grant Cloud Build permissions: + +```bash terminal icon="terminal" +gcloud services enable run.googleapis.com cloudbuild.googleapis.com +gcloud projects add-iam-policy-binding $PROJECT_ID \ + --member=serviceAccount:$PROJECT_NUMBER-compute@developer.gserviceaccount.com \ + --role=roles/run.builder +``` + + +These commands enable Cloud Run (`run.googleapis.com`) and Cloud Build (`cloudbuild.googleapis.com`), which are required for deploying from source. Cloud Run runs your containerized app, while Cloud Build handles building and packaging it. + +The IAM binding grants the Compute Engine service account (`$PROJECT_NUMBER-compute@developer.gserviceaccount.com`) permission to build and deploy images on your behalf. + + + + + +Create a new `Dockerfile` in the root of your project. This file contains the instructions to initialize the container, copy your local project files into it, install dependencies, and start the application. + +```docker Dockerfile icon="docker" +# Use the official Bun image to run the application +FROM oven/bun:latest + +# Copy the package.json and bun.lock into the container +COPY package.json bun.lock ./ + +# Install the dependencies +# Install the dependencies +RUN bun install --production --frozen-lockfile + +# Copy the rest of the application into the container +COPY . . + +# Run the application +CMD ["bun", "index.ts"] +``` + + + Make sure that the start command corresponds to your application's entry point. This can also be `CMD ["bun", "run", "start"]` if you have a start script in your `package.json`. + + This image installs dependencies and runs your app with Bun inside a container. If your app doesn't have dependencies, you can omit the `RUN bun install --production --frozen-lockfile` line. + + This image installs dependencies and runs your app with Bun inside a container. If your app doesn't have dependencies, you can omit the `RUN bun install --production --frozen-lockfile` line. + + + +Create a new `.dockerignore` file in the root of your project. This file contains the files and directories that should be _excluded_ from the container image, such as `node_modules`. This makes your builds faster and smaller: + +```docker .dockerignore icon="Docker" +node_modules +Dockerfile* +.dockerignore +.git +.gitignore +README.md +LICENSE +.vscode +.env +# Any other files or directories you want to exclude +``` + + + +Make sure you're in the directory containing your `Dockerfile`, then deploy directly from your local source: + + + Update the `--region` flag to your preferred region. You can also omit this flag to get an interactive prompt to + select a region. Update the `--region` flag to your preferred region. You can also omit this flag to get an + interactive prompt to select a region. + + +```bash terminal icon="terminal" +gcloud run deploy my-bun-app --source . --region=us-west1 --allow-unauthenticated +``` + +```txt +Deploying from source requires an Artifact Registry Docker repository to store built containers. A repository named +[cloud-run-source-deploy] in region [us-west1] will be created. + +Do you want to continue (Y/n)? Y + +Building using Dockerfile and deploying container to Cloud Run service [my-bun-app] in project [my-bun-app-...] region [us-west1] +✓ Building and deploying... Done. + ✓ Validating Service... + ✓ Uploading sources... + ✓ Building Container... Logs are available at [https://console.cloud.google.com/cloud-build/builds...]. + ✓ Creating Revision... + ✓ Routing traffic... + ✓ Setting IAM Policy... +Done. +Service [my-bun-app] revision [my-bun-app-...] has been deployed and is serving 100 percent of traffic. +Service URL: https://my-bun-app-....us-west1.run.app +``` + + + + +🎉 Your Bun application is now live! + +Visit the Service URL (`https://my-bun-app-....us-west1.run.app`) to confirm everything works as expected. + + + diff --git a/docs/images/guides/digitalocean-1.png b/docs/images/guides/digitalocean-1.png new file mode 100644 index 0000000000..365e2b168b Binary files /dev/null and b/docs/images/guides/digitalocean-1.png differ diff --git a/docs/images/guides/digitalocean-2.png b/docs/images/guides/digitalocean-2.png new file mode 100644 index 0000000000..ca26be7d0b Binary files /dev/null and b/docs/images/guides/digitalocean-2.png differ diff --git a/docs/images/guides/digitalocean-3.png b/docs/images/guides/digitalocean-3.png new file mode 100644 index 0000000000..bb085ef7c7 Binary files /dev/null and b/docs/images/guides/digitalocean-3.png differ diff --git a/docs/images/guides/digitalocean-4.png b/docs/images/guides/digitalocean-4.png new file mode 100644 index 0000000000..dae2d4dd8c Binary files /dev/null and b/docs/images/guides/digitalocean-4.png differ diff --git a/docs/images/guides/digitalocean-5.png b/docs/images/guides/digitalocean-5.png new file mode 100644 index 0000000000..827c8fae13 Binary files /dev/null and b/docs/images/guides/digitalocean-5.png differ diff --git a/docs/images/guides/digitalocean-6.png b/docs/images/guides/digitalocean-6.png new file mode 100644 index 0000000000..780ac69569 Binary files /dev/null and b/docs/images/guides/digitalocean-6.png differ diff --git a/docs/images/guides/digitalocean-7.png b/docs/images/guides/digitalocean-7.png new file mode 100644 index 0000000000..640f2ac978 Binary files /dev/null and b/docs/images/guides/digitalocean-7.png differ diff --git a/docs/images/guides/lambda1.png b/docs/images/guides/lambda1.png new file mode 100644 index 0000000000..7678946ee9 Binary files /dev/null and b/docs/images/guides/lambda1.png differ diff --git a/docs/images/guides/lambda2.png b/docs/images/guides/lambda2.png new file mode 100644 index 0000000000..1dabc19824 Binary files /dev/null and b/docs/images/guides/lambda2.png differ diff --git a/docs/images/guides/lambda3.png b/docs/images/guides/lambda3.png new file mode 100644 index 0000000000..6444131a6a Binary files /dev/null and b/docs/images/guides/lambda3.png differ diff --git a/docs/images/guides/lambda4.png b/docs/images/guides/lambda4.png new file mode 100644 index 0000000000..dc7dad1046 Binary files /dev/null and b/docs/images/guides/lambda4.png differ diff --git a/docs/images/guides/lambda5.png b/docs/images/guides/lambda5.png new file mode 100644 index 0000000000..67e11098be Binary files /dev/null and b/docs/images/guides/lambda5.png differ diff --git a/docs/images/guides/lambda6.png b/docs/images/guides/lambda6.png new file mode 100644 index 0000000000..c674055154 Binary files /dev/null and b/docs/images/guides/lambda6.png differ diff --git a/docs/style.css b/docs/style.css index c0b47d1a93..b840262777 100644 --- a/docs/style.css +++ b/docs/style.css @@ -91,6 +91,7 @@ html.dark .shiki span { footer#footer a[href*="mintlify.com"] { display: none; } + .nav-tabs { width: 100% !important; } @@ -144,6 +145,10 @@ html.dark .code-block + .code-block[language="text"] div[data-component-part="co border-bottom-left-radius: 0px; } +div.callout .code-block { + margin-bottom: 0px; +} + .code-block[language="shellscript"] code span.line:not(:empty):has(span)::before { content: "$ "; color: #6272a4;