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

使用SAM本地调用Lambda函数失败/超时问题求助

Troubleshooting Timeout Issues with SAM Local Invoke for Python Lambda on Windows

我帮你梳理几个在Windows环境下排查SAM Local Invoke超时的常见方向,都是实际踩过坑的经验:

1. 优先检查Docker的WSL 2后端配置

Windows上的Docker如果用Hyper-V后端,很容易和SAM Local出现网络兼容性问题。建议切换到WSL 2模式:

  • 打开Docker Desktop,进入Settings > General,勾选"Use the WSL 2 based engine"
  • 再到Settings > Resources > WSL Integration,确保你正在使用的WSL发行版(比如Ubuntu)是启用状态
  • 重启Docker Desktop后,再尝试执行sam local invoke

2. 修复文件挂载路径的潜在问题

从你的日志里看到挂载路径是C:\xxxxxxx\lambda-python3.8.aws-sam\build\HelloWorldFunction,这里.aws-sam前面少了一个反斜杠(应该是C:\xxxxxxx\lambda-python3.8\.aws-sam\build\HelloWorldFunction),虽然SAM Build理论上会自动处理路径,但Windows的路径解析偶尔会出问题:

  • 手动检查.aws-sam/build/HelloWorldFunction目录是否存在,里面是否有正确的app.py和依赖文件
  • 尝试切换到WSL终端运行命令:打开WSL(比如Ubuntu),切换到项目目录(/mnt/c/xxxxxxx/lambda-python3.8),再执行sam build和sam local invoke,WSL的Linux风格路径对容器挂载更友好

3. 重置SAM和Docker的运行状态

有时候容器镜像损坏或者进程缓存会导致无响应:

  • 停止所有运行中的Docker容器:docker stop $(docker ps -aq)
  • 删除SAM的Python运行时镜像:docker rmi public.ecr.aws/sam/emulation-python3.8:rapid-1.51.0-x86_64
  • 重启Docker Desktop和VSCode,然后重新执行sam build和sam local invoke,让SAM重新拉取干净的镜像

4. 排查防火墙/杀毒软件的拦截

Windows Defender或者第三方杀毒软件可能会拦截SAM和容器之间的本地通信:

  • 临时关闭Windows Defender防火墙(测试用,之后记得打开),或者把SAM CLI(sam.exe)和Docker加入防火墙信任列表
  • 如果有第三方杀毒软件,也暂时关闭后测试是否能正常调用

5. 升级SAM CLI和Docker版本

你当前使用的SAM CLI是1.51.0,这个版本比较旧,很多Windows环境的bug已经在后续版本修复了:

  • 执行sam update升级到最新版SAM CLI
  • 同时把Docker Desktop升级到最新稳定版,确保两者版本兼容

以下是你提供的命令行执行日志:

C:\xxxxxxx\lambda-python3.8>sam build
Your template contains a resource with logical ID "ServerlessRestApi", which is a reserved logical ID in AWS SAM. It could result in unexpected behaviors and is not recommended.
Building codeuri: C:\xxxxxxx\lambda-python3.8\hello_world runtime: python3.8 metadata: {} architecture: x86_64 functions: ['HelloWorldFunction']
Running PythonPipBuilder:ResolveDependencies
Running PythonPipBuilder:CopySource
Build Succeeded
Built Artifacts : .aws-sam\build
Built Template : .aws-sam\build\template.yaml

C:\xxxxxxx\lambda-python3.8>sam local invoke
Invoking app.lambda_handler (python3.8)
Skip pulling image and use local one: public.ecr.aws/sam/emulation-python3.8:rapid-1.51.0-x86_64.
Mounting C:\xxxxxxx\lambda-python3.8.aws-sam\build\HelloWorldFunction as /var/task:ro,delegated inside runtime container
Function 'HelloWorldFunction' timed out after 10 seconds
No response from invoke container for HelloWorldFunction

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 16:23:13