简体   繁体   English

在 lambda 层中导入库

[英]Import libraries in lambda layers

I wanted to import jsonschema library in my AWS Lambda in order to perform request validation.我想在我的 AWS Lambda 中导入jsonschema库以执行请求验证。 Instead of bundling the dependency with my app , I am looking to do this via Lambda Layers.我没有将依赖项与我的应用程序捆绑在一起,而是希望通过 Lambda 层来做到这一点。 I zipped all the dependencies under venv/lib/python3.6/site-packages/ .我压缩了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.我将它作为 lambda 层上传,并分别使用publish-layer-versionaws lambda update-function-configuration命令将其添加到我的 aws lambda 中。 The zip folder is name "lambda-dep.zip" and all the files are under it. zip 文件夹的名称为“lambda-dep.zip”,所有文件都在其下。 However when I try to import jsonschema in my lambda_function , I see the error below -但是,当我尝试在我的 lambda_function 中导入 jsonschema 时,我看到以下错误 -

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?我是否遗漏了任何步骤,是否有不同的机制可以在 lambda 层中导入任何内容?

You want to make sure your .zip follows this folder structure when unzipped您要确保您的 .zip 在解压缩时遵循此文件夹结构

python/lib/python3.6/site-packages/{LibrariesGoHere}. python/lib/python3.6/site-packages/{LibrariesGoHere}。

Upload that zip, make sure the layer is added to the Lambda function and you should be good to go.上传该 zip,确保该层已添加到 Lambda 函数中,您应该一切顺利。

This is the structure that has worked for me.这是对我有用的结构。

Here the script that I use to upload a layer:这是我用来上传图层的脚本:

#!/usr/bin/env bash

LAYER_NAME=$1 # input layer, retrived as arg
ZIP_ARTIFACT=${LAYER_NAME}.zip
LAYER_BUILD_DIR="python"

# note: put the libraries in a folder supported by the runtime, means that should by python

rm -rf ${LAYER_BUILD_DIR} && mkdir -p ${LAYER_BUILD_DIR}

docker run --rm -v `pwd`:/var/task:z lambci/lambda:build-python3.6 python3.6 -m pip --isolated install -t ${LAYER_BUILD_DIR} -r requirements.txt

zip -r ${ZIP_ARTIFACT} .

echo "Publishing layer to AWS..."
aws lambda publish-layer-version --layer-name ${LAYER_NAME} --zip-file fileb://${ZIP_ARTIFACT} --compatible-runtimes python3.6

# clean up
rm -rf ${LAYER_BUILD_DIR}
rm -r ${ZIP_ARTIFACT}

I added the content above to a file called build_layer.sh , then I call it as bash build_layer.sh my_layer .我将上面的内容添加到名为build_layer.sh的文件中,然后我将其称为bash build_layer.sh my_layer The script requires a requirements.txt in the same folder, and it uses Docker to have the same runtime used for Python3.6 Lambdas.该脚本需要在同一文件夹中的requirements.txt ,并且它使用 Docker 具有用于 Python3.6 Lambda 的相同运行时。 The arg of the script is the layer name.脚本的 arg 是图层名称。

After uploading a layer to AWS, be sure that the right layer's version is referenced inside your Lambda.将图层上传到 AWS 后,请确保在您的 Lambda 中引用了正确的图层版本。

There is an easier method.有一个更简单的方法。 Just install the packages into a python folder.只需将软件包安装到 python 文件夹中。 Then install the packages using the -t (Target) option.然后使用 -t (Target) 选项安装软件包。 Note the "."注意“。” in the zip file.在 zip 文件中。 this is a wild card.这是一张通配符。

mkdir lambda_function
cd lambda_function
mkdir python
cd python
pip install yourPackages -t ./
cd ..
zip /tmp/labmda_layer.zip .

The zip file is now your lambda layer. zip 文件现在是您的 lambda 层。

The step by step instructions includeing video instructions can be found here.可在此处找到包含视频说明的分步说明。

https://geektopia.tech/post.php?blogpost=Create_Lambda_Layer_Python https://geektopia.tech/post.php?blogpost=Create_Lambda_Layer_Python

Update from previous answers: Per AWS documentation, requirements have been changed to simply be placed in a /python directory without the rest of the directory structure.从以前的答案更新:根据 AWS 文档,要求已更改为简单地放在 /python 目录中,而没有其余的目录结构。

https://aws.amazon.com/premiumsupport/knowledge-center/lambda-import-module-error-python/ https://aws.amazon.com/premiumsupport/knowledge-center/lambda-import-module-error-python/

Be sure your unzipped directory structure has libraries within a /python directory.确保解压后的目录结构在 /python 目录中有库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM