Using moviepy, scipy and numpy in amazon lambda
With the help of all posts in this thread here is a solution for the records:
To get this to work you'll need to:
start a
EC2
instance with at least 2GO RAM (to be able to compileNumPy
&SciPy
)Install the needed dependencies
sudo yum -y update
sudo yum -y upgrade
sudo yum -y groupinstall "Development Tools"
sudo yum -y install blas --enablerepo=epel
sudo yum -y install lapack --enablerepo=epel
sudo yum -y install Cython --enablerepo=epel
sudo yum install python27-devel python27-pip gcc
virtualenv ~/env
source ~/env/bin/activate
pip install scipy
pip install numpy
pip install moviepyCopy to your locale machine all the content of the directories (except _markerlib, pip*, pkg_resources, setuptools* and easyinstall*) in a
stack
folder:home/ec2-user/env/lib/python2.7/dist-packages
home/ec2-user/env/lib64/python2.7/dist-packages
get all required shared libraries from you
EC2
instance:libatlas.so.3
libf77blas.so.3
liblapack.so.3
libptf77blas.so.3
libcblas.so.3
libgfortran.so.3
libptcblas.so.3
libquadmath.so.0
Put them in a
lib
subfolder of thestack
folderimageio
is a dependency ofmoviepy
, you'll need to download some binary version of its dependencies:libfreeimage
and offfmpeg
; they can be found here. Put them at the root of your stack folder and renamelibfreeimage-3.16.0-linux64.so
tolibfreeimage.so
You should now have a
stack
folder containing:- all python dependencies at root
- all shared libraries in a
lib
subfolder ffmpeg
binary at rootlibfreeimage.so
at root
Zip this folder:
zip -r9 stack.zip . -x ".*" -x "*/.*"
Use the following
lambda_function.py
as an entry point for yourlambda
from __future__ import print_function
import os
import subprocess
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
LIB_DIR = os.path.join(SCRIPT_DIR, 'lib')
FFMPEG_BINARY = os.path.join(SCRIPT_DIR, 'ffmpeg')
def lambda_handler(event, context):
command = 'LD_LIBRARY_PATH={} IMAGEIO_FFMPEG_EXE={} python movie_maker.py'.format(
LIB_DIR,
FFMPEG_BINARY,
)
try:
output = subprocess.check_output(command, shell=True)
print(output)
except subprocess.CalledProcessError as e:
print(e.output)write a
movie_maker.py
script that depends onmoviepy
,numpy
, ...add those to script to your stack.zip file
zip -r9 lambda.zip *.py
upload the zip to
S3
and use it as a source for yourlambda
You can also download the stack.zip
here.
Installing numpy on Mac to work on AWS Lambda
Building on @MarkB's comment, it would not be possible to build numpy
on a Mac to use on AWS Lambda. So why do some packages work and others don't?
Python extension modules, as explained on Mark Nunnikhoven's blog here, are
written in C or C++ that can either extend python or call C or C++
libraries.
Since these modules are compiled specific to the system you're on, and AWS Lambda is a Linux environment, you'll need to install any extension modules on a Linux environment.
Related Topics
How to Implement a Binary Tree
How to Insert Pandas Dataframe via MySQLdb into Database
Elegant Way to Check If a Nested Key Exists in a Dict
How to Create an Encrypted Zip File
Python Regular Expressions - How to Capture Multiple Groups from a Wildcard Expression
Appending to an Empty Dataframe in Pandas
How to Distribute Python Programs
How to Force Django to Ignore Any Caches and Reload Data
How to Share Variables Across Scripts in Python
Intercepting Stdout of a Subprocess While It Is Running
Python How to Pad Numpy Array with Zeros
How to Stop Flask from Initialising Twice in Debug Mode
How to Set Folder Permissions in Windows
How to Flatten Lists Without Splitting Strings
Is There Any Simple Way to Benchmark Python Script