> ## Documentation Index
> Fetch the complete documentation index at: https://bun.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 在 AWS Lambda 上部署 Bun 应用

[AWS Lambda](https://aws.amazon.com/lambda/) 是一项无服务器计算服务，允许您无需配置或管理服务器即可运行代码。

在本指南中，我们将使用 `Dockerfile` 将 Bun HTTP 服务器部署到 AWS Lambda。

<Note>
  继续之前，请确保您已经具备：

  * 一个准备好部署的 Bun 应用
  * 一个 [AWS 账户](https://aws.amazon.com/)
  * 已安装并配置好的 [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)
  * 已安装并添加到 `PATH` 的 [Docker](https://docs.docker.com/get-started/get-docker/)
</Note>

***

<Steps>
  <Step title="创建一个新的 Dockerfile">
    确保您处于项目目录下，然后在项目根目录创建一个新的 `Dockerfile`。此文件包含初始化容器、将本地项目文件复制到容器、安装依赖以及启动应用的指令。

    ```docker Dockerfile icon="docker" theme={"theme":{"light":"github-light","dark":"dracula"}}
    # 使用官方 AWS Lambda 适配器镜像来处理 Lambda 运行时
    FROM public.ecr.aws/awsguru/aws-lambda-adapter:0.9.0 AS aws-lambda-adapter

    # 使用官方 Bun 镜像来运行应用
    FROM oven/bun:debian AS bun_latest

    # 将 Lambda 适配器复制到容器中
    COPY --from=aws-lambda-adapter /lambda-adapter /opt/extensions/lambda-adapter

    # 设置端口为 8080，AWS Lambda 适配器需要此端口
    ENV PORT=8080

    # 设置工作目录为 `/var/task`，这是 Lambda 的默认工作目录
    WORKDIR "/var/task"

    # 复制 package.json 和 bun.lock 到容器中
    COPY package.json bun.lock ./

    # 安装依赖
    RUN bun install --production --frozen-lockfile

    # 复制其余应用文件到容器中
    COPY . /var/task

    # 运行应用
    CMD ["bun", "index.ts"]
    ```

    <Note>
      确保启动命令对应您的应用入口点。如果您在 `package.json` 中有启动脚本，也可以使用 `CMD ["bun", "run", "start"]`。

      该镜像会在容器内使用 Bun 安装依赖并运行您的应用。如果您的应用没有依赖，可以省略 `RUN bun install --production --frozen-lockfile` 这行。
    </Note>

    在项目根目录创建一个新的 `.dockerignore` 文件，里面列出应从容器镜像中 *排除* 的文件或目录，例如 `node_modules`。这样可以让构建更快且更小：

    ```docker .dockerignore icon="Docker" theme={"theme":{"light":"github-light","dark":"dracula"}}
    node_modules
    Dockerfile*
    .dockerignore
    .git
    .gitignore
    README.md
    LICENSE
    .vscode
    .env
    # 任何其他想排除的文件或目录
    ```
  </Step>

  <Step title="构建 Docker 镜像">
    确保您处于包含 `Dockerfile` 的目录，然后构建 Docker 镜像。本例中，我们将镜像命名为 `bun-lambda-demo`，并标记为 `latest`。

    ```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
    # cd /path/to/your/app
    docker build --provenance=false --platform linux/amd64 -t bun-lambda-demo:latest .
    ```
  </Step>

  <Step title="创建 ECR 仓库">
    要将镜像推送到 AWS Lambda，首先需要创建一个 [ECR 仓库](https://aws.amazon.com/ecr/)。

    运行以下命令将会：

    * 在 `us-east-1` 区域创建一个名为 `bun-lambda-demo` 的 ECR 仓库
    * 获取仓库 URI，并将其导出为环境变量（可选，但便于接下来的步骤）

    ```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
    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 theme={"theme":{"light":"github-light","dark":"dracula"}}
    [id].dkr.ecr.us-east-1.amazonaws.com/bun-lambda-demo
    ```

    <Note>
      如果您使用 IAM Identity Center (SSO) 或为 AWS CLI 配置了多个 profile，需要为 AWS CLI 命令添加 `--profile` 参数。

      例如，若您的 profile 名为 `my-sso-app`，则使用 `--profile my-sso-app`。您可通过 `aws configure list-profiles` 查看可用的 profile。

      ```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
      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
      ```
    </Note>
  </Step>

  <Step title="认证登录 ECR 仓库">
    登录到 ECR 仓库：

    ```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
    aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin $ECR_URI
    ```

    ```txt theme={"theme":{"light":"github-light","dark":"dracula"}}
    Login Succeeded
    ```

    <Note>
      若使用 profile，需添加 `--profile` 参数：

      ```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
      aws ecr get-login-password --region us-east-1 --profile my-sso-app | docker login --username AWS --password-stdin $ECR_URI
      ```
    </Note>
  </Step>

  <Step title="为 Docker 镜像打标签并推送到 ECR 仓库">
    确保您处于包含 `Dockerfile` 的目录下，然后对 Docker 镜像使用 ECR 仓库的 URI 打标签。

    ```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
    docker tag bun-lambda-demo:latest ${ECR_URI}:latest
    ```

    接着，将镜像推送到 ECR 仓库。

    ```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
    docker push ${ECR_URI}:latest
    ```
  </Step>

  <Step title="创建 AWS Lambda 函数">
    打开 **AWS 控制台** > **Lambda** > [**创建函数**](https://us-east-1.console.aws.amazon.com/lambda/home?region=us-east-1#/create/function?intent=authorFromImage) > 选择 **容器映像**

    <Warning>请确认您选择了正确的区域，该链接默认为 `us-east-1`。</Warning>

    <Frame>
      <img src="https://mintcdn.com/bun-zhcndoc/7hwCkUCcx3ux5DPj/images/guides/lambda1.png?fit=max&auto=format&n=7hwCkUCcx3ux5DPj&q=85&s=de14e2026156ee152a1470dd37fa1e4d" alt="创建函数" width="3116" height="2084" data-path="images/guides/lambda1.png" />
    </Frame>

    为函数命名，例如 `my-bun-function`。
  </Step>

  <Step title="选择容器映像">
    然后，前往 **容器映像 URI** 部分，点击 **浏览映像**。选择刚才推送到 ECR 仓库的映像。

    <Frame>
      <img src="https://mintcdn.com/bun-zhcndoc/7hwCkUCcx3ux5DPj/images/guides/lambda2.png?fit=max&auto=format&n=7hwCkUCcx3ux5DPj&q=85&s=d442861c485c7408647bf4a0b3bb3198" alt="选择容器仓库" width="4128" height="2412" data-path="images/guides/lambda2.png" />
    </Frame>

    接着选择 `latest` 标签的映像，点击 **选择映像**。

    <Frame>
      <img src="https://mintcdn.com/bun-zhcndoc/7hwCkUCcx3ux5DPj/images/guides/lambda3.png?fit=max&auto=format&n=7hwCkUCcx3ux5DPj&q=85&s=89df361d86d83c16d92f7afcf98ed14b" alt="选择容器映像" width="4128" height="2172" data-path="images/guides/lambda3.png" />
    </Frame>
  </Step>

  <Step title="配置函数">
    要获取函数的公共 URL，需要前往 **附加配置** > **网络** > **函数 URL**。

    将其设置为 **启用**，认证类型选择 **NONE**。

    <Frame>
      <img src="https://mintcdn.com/bun-zhcndoc/7hwCkUCcx3ux5DPj/images/guides/lambda4.png?fit=max&auto=format&n=7hwCkUCcx3ux5DPj&q=85&s=58e34c2017bd9bff300a07f41403b514" alt="设置函数 URL" width="3116" height="1524" data-path="images/guides/lambda4.png" />
    </Frame>
  </Step>

  <Step title="创建函数">
    点击页面底部的 **创建函数**，函数即被创建。

    <Frame>
      <img src="https://mintcdn.com/bun-zhcndoc/7hwCkUCcx3ux5DPj/images/guides/lambda6.png?fit=max&auto=format&n=7hwCkUCcx3ux5DPj&q=85&s=7a1829578a6c725c241c1081b586eb05" alt="创建函数" width="4836" height="2516" data-path="images/guides/lambda6.png" />
    </Frame>
  </Step>

  <Step title="获取函数 URL">
    函数创建完成后，您会被重定向到函数页面，在 **“函数 URL”** 部分可以看到函数的 URL。

    <Frame>
      <img src="https://mintcdn.com/bun-zhcndoc/7hwCkUCcx3ux5DPj/images/guides/lambda5.png?fit=max&auto=format&n=7hwCkUCcx3ux5DPj&q=85&s=793c84feed33534b2ac1119808f263e0" alt="函数 URL" width="4792" height="2500" data-path="images/guides/lambda5.png" />
    </Frame>
  </Step>

  <Step title="测试函数">
    🥳 您的应用已上线！要测试函数，您可以前往 **测试** 标签页，或直接调用函数 URL。

    ```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
    curl -X GET https://[your-function-id].lambda-url.us-east-1.on.aws/
    ```

    ```txt theme={"theme":{"light":"github-light","dark":"dracula"}}
    Hello from Bun on Lambda!
    ```
  </Step>
</Steps>
