为 Python lambda 运行 SAM 本地调用时出现“未找到模块错误”

问题描述

我有一个典型的 Python AWS lambda 代码,从它的 Hello World 示例中重构出 at this link,section "Invoke your Lambda function directly"。 hello world 模板按预期工作,然后我将 Lambda 的主体更改为 (abriged)

import requests
 
def handler(event,context):
# some code...
         response = requests.post(root_url + "/data",headers={'Content-Type': 'application/x-www-form-urlencoded'},data={'input': input_object},timeout=30)

运行后

sam local invoke "MyFunction" -e test-events/event.json

我明白

Invoking handler.handler (python3.8)
Skip pulling image and use local one: amazon/aws-sam-cli-emulation-image-python3.8:rapid-1.15.0.

Mounting C:\project\lambda as /var/task:ro,delegated inside runtime container
START RequestId: 9169ecce-9775-4423-ba31-45ef3917b1c9 Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'handler': No module named 'requests'
END RequestId: 9169ecce-9775-4423-ba31-45ef3917b1c9
REPORT RequestId: 9169ecce-9775-4423-ba31-45ef3917b1c9  Init Duration: 0.17 ms  Duration: 109.74 ms     Billed Duration: 200 ms Memory Size: 128 MB     Max Memory Used: 128 MB
{"errorMessage": "Unable to import module 'handler': No module named 'requests'","errorType": "Runtime.ImportModuleError"}

如何让 SAM 成为模块的 avare?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)