How to get fbprophet working on AWS Lambda

I assume you’re reading this post because you’re looking for ways to use the awesome fbprophet (Facebook open source forecasting) library on AWS Lambda and you’re already familiar with the various issues around getting it done. I will be using a python 3.6 example, but the approach is applicable to other runtimes as well as other large ML libraries. Without further ado:

Install Docker locally. An alternative is to run a EC2 instance and copy files between the instance and local machine, which feels more complicated and less reliable to me.

Run a shell on a relevant Lambda image (take a look at lambci) from the working directory with your code, that is supposed to run on Lambda (which has the lambda handler):

What happens here is that you’re getting rid of matplotlib (if you don’t need plotting), tests, and the pystan source codes (the heaviest part of the package). Last command displays the size of the venv, which should be below 250Mb.

6. Make sure your code is still working after the cleanup: python lambda_handler.py

7. Create a .zip to upload to Lambda:

pushd $VIRTUAL_ENV/lib/python3.6/site-packages/

zip -r -9 -q /var/task/name_of_your_zip.zip *

popd

zip -9r name_of_your_zip.zip name_of_your_code_folder/

zip -9 name_of_your_zip.zip lambda_handler.py

8. You now have a .zip in your working directory that you can use for Lambda. You can stop/quit Docker now — the zip will still be in your folder.