Where to put a Python Library for access in Lambda?

i have a big library of Python modules i frequently use (import). where would i make them available to AWS Lambda functions?


Related discussions:

It depends on your OS and configuration. The AWS docs for deployment package creation are here.

Use Gordon for your lambdas

OS will be Amazon Linux. if i have to put the library in every .zip file, that can become difficult. the library is almost 360MB. apparently i will need to determine what each function actually needs and then select only those files to be packaged. it would be nice if Lambda could be configured to access some file storage like EFS.

Maximum 50MB for Lambda packages
http://docs.aws.amazon.com/lambda/latest/dg/limits.html

Including things like EFS or other storage goes against the serverless model.

I’ve read where some people will download additional modules to the /tmp directory (which allows up to 500MB) on first run and then use them from there in the code.

Less than ideal for sure, but still better than managing servers.

The whole point of lambda is quick running functions…

so there is no way to package the whole library. i will have to select what each lambda function actually needs and data files go to S3.

i don’t see how that can be for EFS but not so for other data types like S3.

i don’t know what that is. got a link to a one pager?

right. i guess the point is that a huge library slows it down. the original idea was to have the whole thing already there and each lambda function just imports the part(s) it needs. this was to avoid having to figure out what each lambda function needs and package just the right files. but i certainly didn’t want to (and can’t, due to the size limit) package the whole library with each lambda function. so it looks like i have to stay with the “as needed” approch. i can do an import-like operation from S3. but that is slow.

managing servers is not a big deal for me. cutting costs is my big like of lambda. where i have an instance already running with a usable profile or the necessary access, i can put it there. that can work well for time scheduled stuff. lambda benefits from other triggers.

EFS is meant as storage to be shared amongst servers, like NFS or CIFS. S3 is just object-based and never depends on a server.

I can understand the question - serverless is a new-ish paradigm and it really is different.

In general, libraries should be installed in the root of the deployment package. You can use pip install -r requirements.txt -t . or pip install <library_name> -t . to do this before deploying your lambda.

But I noticed in your comments that you have a library which exceeds the max deployment package limit (50 MB). If your project is a REST API, you might want to consider using the Zappa Framework, which hooks lambda up to your WSGI App (Flask, Pycnic, Django, …), and has an option which circumvents the size limit with its Slim Handler.

Some people think that Lambda shouldn’t be used for large or long-running tasks, but I think that no one should have to worry about provisioning their own servers, load balancers, and shudders configuring security groups…

yes, the library is huge, but i don’t think the amount any one app needs exceeds limits. my new thought is to read stuff in from S3, selecting only a limited amount, feed it to exec(), and capture the directories it builds.

but lambda still lets you access some storage resources. i don’t see why not more. they already have namespace distinction by having a “/tmp” unique to each app. at some point it gets cleaned up. the EFS mount can get cleaned up, too. it might be enough for the mountpoint to be /tmp/efs. then having that lambda be “in” your VPC it could access it. so maybe all you need to do is test -e /tmp/efs/marker||(mkdir /tmp/efs;mount -t nfs4 172.31.0.5:/ /tmp/efs)