Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import libraries in lambda layers

Tags:

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? 
like image 841
Punter Vicky Avatar asked Apr 15 '19 18:04

Punter Vicky


People also ask

Can the packages be used in AWS Lambda?

Your AWS Lambda function's code consists of scripts or compiled programs and their dependencies. You use a deployment package to deploy your function code to Lambda. Lambda supports two types of deployment packages: container images and . zip file archives.


1 Answers

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.

like image 177
guitarhero23 Avatar answered Sep 22 '22 15:09

guitarhero23