在 lambda 层中导入库 [英] Import libraries in lambda layers
问题描述
我想在我的 AWS Lambda 中导入 jsonschema 库以执行请求验证.我没有将依赖项与我的应用程序捆绑在一起,而是希望通过 Lambda 层来做到这一点.我压缩了 venv/lib/python3.6/site-packages/
下的所有依赖项.我将其作为 lambda 层上传,并分别使用 publish-layer-version
和 aws lambda update-function-configuration
命令将其添加到我的 aws lambda 中.zip 文件夹名为lambda-dep.zip",所有文件都在其下.但是,当我尝试在 lambda_function 中导入 jsonschema 时,我看到以下错误 -
I wanted to import jsonschema library in my AWS Lambda in order to perform request validation. Instead of bundling the dependency with my app , I am looking to do this via Lambda Layers. I zipped all the dependencies under venv/lib/python3.6/site-packages/
. I uploaded this as a lambda layer and added it to my aws lambda using publish-layer-version
and aws lambda update-function-configuration
commands respectively. The zip folder is name "lambda-dep.zip" and all the files are under it. However when I try to import jsonschema in my lambda_function , I see the error below -
from jsonschema import validate
{
"errorMessage": "Unable to import module 'lambda_api': No module named 'jsonschema'",
"errorType": "Runtime.ImportModuleError"
}```
Am I missing any steps are is there a different mechanism to import anything within lambda layers?
推荐答案
你想确保你的 .zip 在解压时遵循这个文件夹结构
You want to make sure your .zip follows this folder structure when unzipped
python/lib/python3.6/site-packages/{LibrariesGoHere}.
python/lib/python3.6/site-packages/{LibrariesGoHere}.
上传该 zip,确保将图层添加到 Lambda 函数中,然后您就可以开始使用了.
Upload that zip, make sure the layer is added to the Lambda function and you should be good to go.
这是对我有用的结构.
这篇关于在 lambda 层中导入库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!