Some tips to deploy a Python package on AWS Lambda

And keep your git clean!

Matyas Amrouche
Jun 5, 2020 · 2 min read
Image for post
Image for post
Photo by Cesar Carlevarino Aragon on Unsplash

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…

Image for post
Image for post
Not very nice to push on git…

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.

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 (those libs are generally in the lambda_venv/lib/python3.7/site-packages)
  • go back to the working directory and zip in the
  • deploy the using awscli

Now we just need to run our deployment script to actually deploy our lambda code.

You finally have a much much more readable/maintainable/pushable directory than the previous dirty one.

Image for post
Image for post
Much more clear 🙌

Et voilà ! Your turn to deploy lambdas with grace 🚀 💃

Many thanks to Olivier Guimbal who showed me this tip!

The Startup

Medium's largest active publication, followed by +773K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store