You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Actions使用Docker构建CDK Lambda Layer失败求助

解决GitHub Actions中CDK打包Lambda Layer时Docker找不到asset-input文件的问题

你的问题核心是GitHub Actions的Docker-in-Docker环境下,CDK的Code.fromAsset未正确将本地目录挂载到Docker容器的/asset-input路径,导致打包命令找不到依赖文件。以下是针对性的解决步骤:

1. 确认GitHub Actions工作目录与CDK路径匹配

在GitHub Actions workflow中,确保checkout代码后,CDK执行的工作目录和本地开发环境一致。例如如果CDK项目在仓库根目录的cdk文件夹下,需显式切换目录:

- name: Deploy CDK
  working-directory: ./cdk
  run: cdk deploy --require-approval never

2. 调整Bundling命令的路径与逻辑

当前打包命令存在相对路径失效、冗余文件复制的问题,修改为直接在容器内完成依赖安装与代码生成,再按Lambda Layer要求整理文件结构:

const prismaLayer = new LayerVersion(this, "PrismaLayer", {
  compatibleRuntimes: [Runtime.NODEJS_18_X],
  description: "Prisma Layer",
  code: Code.fromAsset(path.join(__dirname, "../../packages/database"), {
    bundling: {
      image: Runtime.NODEJS_18_X.bundlingImage,
      workingDirectory: "/asset-input",
      user: "root",
      command: [
        "bash",
        "-c",
        [
          "npm install --production",
          "npx prisma generate",
          "rm -rf node_modules/.cache",
          "rm -rf node_modules/.prisma/client/*darwin* || true",
          "rm -rf node_modules/.prisma/client/*windows* || true",
          "rm -rf node_modules/@prisma/engines/node_modules",
          "rm -rf node_modules/@prisma/engines/*darwin* || true",
          "rm -rf node_modules/prisma/*darwin* || true",
          "mkdir -p /asset-output/nodejs",
          "cp -r package.json package-lock.json index.ts prisma node_modules /asset-output/nodejs/",
        ].join(" && "),
      ],
    },
  }),
  layerVersionName: `prisma-layer`,
});

关键修改说明:

  • 放弃提前复制文件:直接在/asset-input目录执行依赖安装和代码生成,避免本地node_modules的平台兼容性问题。
  • 符合Lambda Layer结构要求:Node.js层的文件必须放在nodejs目录下,因此创建/asset-output/nodejs并将所有必要文件复制到该目录,确保Lambda能正确加载层内容。

3. 修复Docker-in-Docker的权限问题

GitHub Actions的Docker环境可能存在文件挂载权限不足的情况,可在打包命令中添加权限修复步骤:

bundling: {
  // 保留原有配置
  command: [
    "bash",
    "-c",
    [
      "chmod -R 755 /asset-input",
      // 后续打包命令
    ].join(" && ")
  ]
}

4. 验证环境中的文件路径

在workflow中添加调试步骤,确认要打包的目录和文件存在:

- name: Verify database package files
  run: |
    ls -la ./packages/database
    pwd

内容的提问来源于stack exchange,提问作者Callum McClean

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 00:02:08