Import libraries in lambda layers Import libraries in lambda layers python-3.x python-3.x

Import libraries in lambda layers


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.


Here the script that I use to upload a layer:

#!/usr/bin/env bashLAYER_NAME=$1 # input layer, retrived as argZIP_ARTIFACT=${LAYER_NAME}.zipLAYER_BUILD_DIR="python"# note: put the libraries in a folder supported by the runtime, means that should by pythonrm -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.txtzip -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 uprm -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.


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_functioncd lambda_functionmkdir pythoncd pythonpip install yourPackages -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