Some tips to deploy a Python package on AWS Lambda
And keep your git clean!

This post is very straightforward. I haven’t seen much resources mentioning this technique which is quite simple and useful to know in order to keep a clean and readable working directory 🤓
When deploying a python package (< few MB) on AWS Lambda with external libraries (like numpy, pandas…), you must put all these libs in the lambda folder you want to deploy. Then zip all the files it contains and finally deploy the zip file.
You may end with a messy working directory (see image below) which will make things much harder for you or for other devs to work with. Besides, it’s pretty ugly…

We don’t want that ☝️
To keep it clean go to your lambda function directory (lambda folder in our example). Let’s first create and activate a virtualenv for your lambda. Then install all the libs you need for the lambda to run.
virtualenv lambda_venv
source lambda_venv/bin/activate
pip install numpy, pandas, ...
pip freeze > requirements.txt
Now that we are ready let’s build a simple bash deployment script. This script will do the following:
- look into the virtualenv and zip all the libs in lambda.zip (those libs are generally in the lambda_venv/lib/python3.7/site-packages)
- go back to the working directory and zip lambda_function.py in the lambda.zip
- deploy the lambda.zip using awscli
cd lambda_venv/lib/python3.7/site-packages
zip -r9 ${OLDPWD}/lambda.zip .
cd $OLDPWD
zip -g lambda.zip lambda_function.py
aws lambda update-function-code --function-name lambda --zip-file fileb://lambda.zip
Now we just need to run our deployment script to actually deploy our lambda code.
sh deployment.sh
You finally have a much much more readable/maintainable/pushable directory than the previous dirty one.

Et voilà ! Your turn to deploy lambdas with grace 🚀 💃
Many thanks to Olivier Guimbal who showed me this tip!