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

AWS CDK在GitLab CI中报错RuntimeError:打包未生成输出

问题:本地AWS CDK栈运行正常,GitLab CI执行时出现打包输出为空错误

本地运行AWS CDK栈完全正常,但在GitLab CI中执行时抛出错误:

RuntimeError: Bundling did not produce any output. Check that content is written to /asset-output

相关代码片段

CDK栈代码

class CdkLambdaInfraStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        myapp = _lambda.Function(
            self,
            "myapp",
            code=_lambda.Code.from_asset(
                get_bundle_image_path(),
                exclude=["*.pyc"],
                bundling={
                    "image": core.DockerImage.from_build(get_bundle_image_path()),
                    "command": [
                        "sh",
                        "-c",
                        f"rsync -rLv {generate_exclude_command(exclude_list)} \
                            /asset-input/ /asset-output \
                        && poetry export --with-credentials --format requirements.txt --output /asset-output/requirements.txt --without-hashes \
                        && cd /asset-output \
                        &&  python -m pip install -r requirements.txt -t /asset-output \
                        && ls -la ",

                    ],
                },
            ),
            handler="lambda_infra_cdk.compute.my_lambda.handler",
            runtime=_lambda.Runtime.PYTHON_3_8,
        ) 

app.py调用代码

app = cdk.App()
mystack = CdkLambdaInfraStack(
    app,
    "CdkLambdaInfraStack"
)
app.synth()

gitlab-ci.yml配置

stages:
  - build

# Build and push the Docker image to the GitLab image registry
# using Docker-in-Docker.
dind-build:
  stage: build

  image:
    # An alpine-based image with the `docker` CLI installed.
    name: docker:19.03

  before_script:
    # Install the `docker` CLI.
    - apk update
    - apk add gcc libc-dev libffi-dev 
    - apk add --update --no-cache python3 python3-dev && ln -sf python3 /usr/bin/python
    - python3 -m ensurepip
    - pip3 install --no-cache --upgrade pip setuptools
    - pip install poetry
    - poetry install
    - source `poetry env info --path`/bin/activate

  services:
   - name: docker:dind
     alias: dockerdaemon

  variables:
    # Tell docker CLI how to talk to Docker daemon.
    DOCKER_HOST: tcp://dockerdaemon:2375/
    # Use the overlayfs driver for improved performance.
    DOCKER_DRIVER: overlay2
    # Disable TLS since we're running inside local network.
    DOCKER_TLS_CERTDIR: ""
    FF_NETWORK_PER_BUILD: "true"

  script:
    - apk add --update nodejs npm curl
    - npm install -g aws-cdk
    - python app.py

调试时已在Docker打包命令末尾添加ls -la,可正常列出所有依赖及Lambda处理程序,但错误仍存在。


修复方案

1. 修正打包镜像的目录权限

GitLab CI的Docker-in-Docker环境中,打包容器的/asset-output目录可能存在权限问题,导致CDK无法读取产物。在构建打包镜像的Dockerfile中添加:

RUN mkdir -p /asset-output && chmod 777 /asset-output

确保容器内该目录可读写。

2. 调整rsync同步路径格式

rsync的源路径末尾斜杠可能导致同步逻辑异常,修改命令确保文件正确复制到/asset-output:

rsync -rLv ${generate_exclude_command(exclude_list)} /asset-input/* /asset-output/

避免因路径格式问题导致文件未被同步。

3. 指定CDK临时目录并赋予权限

GitLab CI环境的默认临时目录权限可能不足,在CI脚本中手动设置CDK资产目录:
在script部分添加:

mkdir -p /tmp/cdk-asset && chmod -R 777 /tmp/cdk-asset
export CDK_ASSETS_DIR=/tmp/cdk-asset

4. 优化打包命令的检查逻辑

明确指定ls的目标路径,确保每个步骤的产物都正确生成:

command=[
    "sh",
    "-c",
    f"mkdir -p /asset-output \
        && rsync -rLv {generate_exclude_command(exclude_list)} /asset-input/* /asset-output/ \
        && poetry export --with-credentials --format requirements.txt --output /asset-output/requirements.txt --without-hashes \
        && ls -la /asset-output/requirements.txt \
        && cd /asset-output \
        && python -m pip install -r requirements.txt -t /asset-output \
        && ls -la /asset-output ",
],

5. 升级CDK版本

旧版本CDK在Docker-in-Docker环境可能存在兼容性问题,升级到最新稳定版:

npm install -g aws-cdk@latest

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 00:14:55