Packaging local python dependencies for Zappa deployment

Viewed 1100

Context

I have a local folder containing multiple Python Flask applications and a commons python package. As you can see I have created a virtualenv for each of the Flask applications, because I would like to deploy to AWS Lambda using Zappa and want to include only the relevant dependencies in the package zip that goes to AWS.

project_folder/
 +-- commons/
 |   +-- __init__.py
 |   +-- setup.py
 |   +-- module1.py
 +-- application1/
 |   +-- __init__.py
 |   +-- app.py
 |   +-- env/
 +-- application2/
 |   +-- __init__.py
 |   +-- app.py
 |   +-- env/

Problem

I am not able to include the commons package in the Flask applications. I assume this has to do because it is collateral and not a subdirectory.

  • I would like to avoid adding a copy into each Flask application.
  • I do not want to create the whole project into one gigantic package because it will get too large for AWS Lambda

Question

How can I make sure the commons package is included when I call zappa deploy from inside project_folder/application1/?

1 Answers

One of the solutions is to install the package in your virtual environment (pip install . in the commons directory). Zappa would use all the packages that are installed in it.

Of course, this is a bit cumbersome because you'd need to re-install the package every time you modify it. You can either:

  1. Create a script that would help you by re-installing and then calling zappa deploy.
  2. Try to create your own module and register it in the callbacks section of zappa_settings.json. This module would be called by the deployment/update process and could theoretically re-install the package beforehand. However, this is option is just my guess that it might be possible. I've never tried anything like that.
Related