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

无法复现AWS Lambda Python+Docker指南,遇Handler缺失错误

排查Lambda容器镜像的HandlerNotFound错误

正在排查错误Traceback (most recent call last):andler 'handler' missing on module 'lambda_function',推测Dockerfile中的CMD命令无法定位目标函数,但已严格遵循AWS官方指南操作。当前使用Windows的WSL环境执行docker build、docker run及curl测试命令,怀疑遗漏了指南未提及的必要配置,恳请提供排查建议。

目录结构

── project
├── lambda_function.py
├── Dockerfile
├── requirements.txt

Python代码

import sys
def handler(event, context):
    return 'Hello from AWS Lambda using Python' + sys.version + '!'

Dockerfile内容

FROM public.ecr.aws/lambda/python:3.11

# Copy requirements.txt
COPY requirements.txt ${LAMBDA_TASK_ROOT}

# Install the specified packages
RUN pip install -r requirements.txt

# Copy function code
COPY lambda_function.py ${LAMBDA_TASK_ROOT}

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "lambda_function.handler" ]

测试命令

  • 构建镜像:
docker build --platform linux/amd64 -t docker-image:test .
  • 运行容器:
docker run -p 9000:8080 docker-image:test
  • 发起测试请求(新终端执行):
curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'

curl返回错误

{"errorMessage": "Handler 'handler' missing on module 'lambda_function'", "errorType": "Runtime.HandlerNotFound", "requestId": "f76bd6cb-e91e-4197-842c-d8ae0c077a65", "stackTrace": []}

容器日志

START RequestId: 0598dfd5-bd4d-406f-8c9d-d61ef4dffee4 Version: $LATEST
28 Oct 2023 17:03:02,249 [INFO] (rapid) extensionsDisabledByLayer(/opt/disable-extensions-jwigqn8j) -> stat /opt/disable-extensions-jwigqn8j: no such file or directory
28 Oct 2023 17:03:02,249 [INFO] (rapid) Configuring and starting Operator Domain
28 Oct 2023 17:03:02,249 [INFO] (rapid) Starting runtime domain
28 Oct 2023 17:03:02,249 [WARNING] (rapid) Cannot list external agents error=open /opt/extensions: no such file or directory
28 Oct 2023 17:03:02,249 [INFO] (rapid) Starting runtime without AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN , Expected?: false
Traceback (most recent call last):andler 'handler' missing on module 'lambda_function'
END RequestId: f76bd6cb-e91e-4197-842c-d8ae0c077a65
REPORT RequestId: f76bd6cb-e91e-4197-842c-d8ae0c077a65  Init Duration: 0.08 ms  Duration: 59.60 ms      Billed Duration: 60 ms  Memory Size: 3008 MB    Max Memory Used: 3008 MB

排查建议

  • 检查文件换行符:WSL环境下,Windows的CRLF换行符可能导致Python模块解析异常,将lambda_function.py转换为LF格式,可使用dos2unix lambda_function.py命令处理。
  • 验证文件复制情况:进入运行中的容器,检查/var/task(即${LAMBDA_TASK_ROOT})目录下是否存在完整的lambda_function.py,命令:docker exec -it <容器ID> bash,然后执行cat /var/task/lambda_function.py。
  • 修正CMD引号格式:确保Dockerfile中CMD命令使用英文双引号,避免中文引号导致的解析错误。
  • 手动指定handler测试:启动容器时通过环境变量覆盖handler配置,命令:docker run -p 9000:8080 -e AWS_LAMBDA_FUNCTION_HANDLER=lambda_function.handler docker-image:test,再重新发起curl请求。
  • 检查文件名大小写:Linux环境下文件名区分大小写,确认lambda_function.py文件名无拼写或大小写错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 14:06:35