Import libraries in lambda layers

爷,独闯天下 提交于 2019-12-21 17:52:27

问题


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?

回答1:


You want to make sure your .zip follows this folder structure when unzipped

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.

This is the structure that has worked for me.




回答2:


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. The script requires a requirements.txt in the same folder, and it uses Docker to have the same runtime used for Python3.6 Lambdas. The arg of the script is the layer name.

After uploading a layer to AWS, be sure that the right layer's version is referenced inside your Lambda.




回答3:


There is an easier method. Just install the packages into a python folder. Then install the packages using the -t (Target) option. Note the "." in the zip file. this is a wild card.

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

The zip file is now your lambda layer.

The step by step instructions includeing video instructions can be found here.

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



来源:https://stackoverflow.com/questions/55695187/import-libraries-in-lambda-layers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!