我最近开始使用 AWS SAM 来构建 API 和 AWS Lambda。该代码在 AWS 中运行良好,但我在设置本地测试和调试时遇到了一些困难(这是我首先想使用 SAM 的主要原因之一)。
lambda_handler 函数非常简单。它看起来类似于下面。我只是在运行我的函数,比如 some_function_here,它在依赖函数之一中使用“请求”模块。在 SAM 项目的 requirements.txt 中,我有“请求”以及其他一些依赖项。这似乎完成了它的工作,因为我可以看到请求被安装在 AWS Lambda 中(下面的屏幕截图)。
令人惊讶的是,当我在本地运行 SAM 时(在 VsCode 上,我按 F5)。当我运行时sam local invoke
(因为我不需要事件),当它应该根据“requirements.txt”下载请求时,我收到一条错误消息“没有名为请求的模块”,就像它在 AWS 云上所做的那样。任何建议表示赞赏。
Lambda 处理程序代码
def lambda_handler(event, context):
try:
some_function_here()
return {
"statusCode": 200,
"body": json.dumps({
"message": "Job ran successfully."
}),
}
except Exception as e:
return {
"statusCode": 500,
"body": json.dumps({
"message": "Something went wrong!"
}),
}
错误
(base) anojshrestha% sam local invoke
Invoking app.lambda_handler (python3.8)
Skip pulling image and use local one: amazon/aws-sam-cli-emulation-image-python3.8:rapid-1.7.0.
Mounting /... as /var/task:ro,delegated inside runtime container
START RequestId: 17ce7573-87ed-1ce0-5584-31a7f3f0823d Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'app': No module named 'requests'
END RequestId: 17ce7573-87ed-1ce0-5584-31a7f3f0823d
REPORT RequestId: 17ce7573-87ed-1ce0-5584-31a7f3f0823d Init Duration: 188.03 ms Duration: 3.89 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 24 MB
{"errorType":"Runtime.ImportModuleError","errorMessage":"Unable to import module 'app': No module named 'requests'"}