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

使用AWS CDK部署Docker化Lambda时出现awslambdaric模块缺失错误

问题:AWS Lambda部署后提示找不到awslambdaric模块

我编写了一个Dockerfile,通过AWS CDK部署项目,但调用已部署的Lambda函数时出现错误。

原Dockerfile代码

ARG FUNCTION_DIR="/tmp/"

FROM public.ecr.aws/lambda/python:3.10.2023.03.21.20-arm64 as build-image

# Include global arg in this stage of the build
ARG FUNCTION_DIR

# Create function directory
RUN mkdir -p ${FUNCTION_DIR}

# Copy handler function
COPY query/*.py ${FUNCTION_DIR}
COPY prompt/*.py ${FUNCTION_DIR}
COPY starter ${FUNCTION_DIR}
COPY requirements-app.txt ${FUNCTION_DIR}

# Install the function's dependencies
RUN pip install --upgrade pip
RUN pip uninstall --yes jupyter
RUN pip install awslambdaric
RUN pip install --target ${FUNCTION_DIR} sentencepiece protobuf
RUN pip install -r ${FUNCTION_DIR}requirements-app.txt

FROM public.ecr.aws/lambda/python:3.10.2023.03.21.20-arm64

# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}

# Copy in the built dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}

ENTRYPOINT [ "python", "-m", "awslambdaric" ]

# This will get replaced by the proper handler by the CDK script
CMD [ "will_be_replaced" ]

报错信息

2023-03-23T13:31:36.066+05:30   /var/lang/bin/python: No module named awslambdaric

2023-03-23T13:31:36.138+05:30   /var/lang/bin/python: No module named awslambdaric

2023-03-23T13:31:36.142+05:30   START RequestId: f3e3d93b-3x4b-4dd6-5db1-2cldb63cdc70 Version: $LATEST

期望Lambda能够成功运行。


解决方案

错误原因

在build-image阶段,pip install awslambdaric直接安装到了系统默认的Python环境路径中,而最终镜像仅复制了${FUNCTION_DIR}目录下的内容,导致awslambdaric模块没有被包含到最终运行环境里。

修改后的Dockerfile

ARG FUNCTION_DIR="/tmp/"

FROM public.ecr.aws/lambda/python:3.10.2023.03.21.20-arm64 as build-image

ARG FUNCTION_DIR

RUN mkdir -p ${FUNCTION_DIR}

COPY query/*.py ${FUNCTION_DIR}
COPY prompt/*.py ${FUNCTION_DIR}
COPY starter ${FUNCTION_DIR}
COPY requirements-app.txt ${FUNCTION_DIR}

RUN pip install --upgrade pip
RUN pip uninstall --yes jupyter
# 关键修改:将awslambdaric安装到FUNCTION_DIR目录下
RUN pip install --target ${FUNCTION_DIR} awslambdaric
RUN pip install --target ${FUNCTION_DIR} sentencepiece protobuf
RUN pip install -r ${FUNCTION_DIR}requirements-app.txt

FROM public.ecr.aws/lambda/python:3.10.2023.03.21.20-arm64

ARG FUNCTION_DIR
WORKDIR ${FUNCTION_DIR}

COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}

ENTRYPOINT [ "python", "-m", "awslambdaric" ]

CMD [ "will_be_replaced" ]

修改说明

  • 将RUN pip install awslambdaric改为RUN pip install --target ${FUNCTION_DIR} awslambdaric,确保该模块被安装到指定的FUNCTION_DIR目录中,这样在后续复制步骤中就能被带到最终镜像里。
  • 保持其他步骤不变,确保所有依赖都被正确复制到Lambda运行环境中。

重新构建镜像并通过CDK部署后,Lambda函数应该可以正常调用。


内容的提问来源于stack exchange,提问作者Kislay Kunal Singh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 09:35:13