How to reduce the size of packaged python zip files for AWS Lambda

If you're using Python libraries, you can get rid of botocore, boto3 as they are already present in AWS's lambdas functions.


The package that you upload to lambda should not contain anything but the code and support modules required for Lambda to run your code. The Lambda console UI limits the file size to 10MB but you can upload zip files up to 50MB if you place them in an S3 bucket and then request that Lambda load them from S3.

Any other assets that you require for execution such as machine learning models should be uploaded separately to S3 and then downloaded from within your Lambda function at execution time. The Lambda function can write to a /tmp folder but keep in mind it only has access to 512MB of disk space. Also keep in mind that the Lambda function has a maximum runtime of 300 seconds so downloading really large files will take time away from your function doing real work with the data you're downloading.


To get the smallest possible zip file use option -9

$ zip -9

Try using Zappa. Add slim_handler to true in the zappa_settings.json which you make using zappa init.