AWS Lambda with moviepy

I recently found this neat library for working with videos in python moviepy. It provides a super simple way of installing ffmpeg and acts as a python wrapper to perform some nifty audio/video processing. 

Turns out using moviepy inside an AWS Lambda function is not so trivial!

If you can't be bothered to follow all the steps download my moviepy.zip file and jump straight to adding your code (step 6). Just remember if you have other python dependencies you will also need to add those to the zip.  

TL;DR

After a lot of googling here are the steps to follow to get moviepy up and running in your AWS Lambda project:

  1. spin up an EC2 instance using Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type

  2. ssh onto your box and install Python 3.6 and virtualenv using the following steps:

    • sudo yum install -y gcc zlib zlib-devel openssl openssl-devel
    • wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz
    • tar -xzvf Python-3.6.1.tgz
    • cd Python-3.6.1 && ./configure && make
    • sudo make install
    • sudo /usr/local/bin/pip3 install virtualenv
  3. choose the virtual environment that was installed via pip3
/usr/local/bin/virtualenv

    • ~/shrink_venv
source
    • ~/shrink_venv/bin/activate
  4. install libraries in the virtual environment

    • pip install moviepy
    • pip install requests
  5. add the contents of lib and lib64 site-packages to your .zip file.
    Note that the following steps assume you used Python runtime version 3.6. If you used version 2.7 you will need to update accordingly.

    • cd $VIRTUAL_ENV/lib/python3.6/site-packages
    • zip -r9 ~/moviepy.zip .
  6. Add your python code to the .zip file

    • cd ~
    • zip -g moviepy.zip lambda_function.py
  7. set IMAGEIO_USERDIR environment variable to /tmp on your lambda to allow image.io write it's necessary files.