Skip to content

Instantly share code, notes, and snippets.

@qinjie
Forked from joseph-zhong/BUILD_AWS_LAMBDA.md
Last active June 6, 2017 06:44
Show Gist options
  • Save qinjie/b837d9ec170fd8d2acd19df8f84dd200 to your computer and use it in GitHub Desktop.
Save qinjie/b837d9ec170fd8d2acd19df8f84dd200 to your computer and use it in GitHub Desktop.
Building OpenCV for AWS Lambda Python

To build OpenCV3.2 and its extra modules for AWS Lambda Python

Summary

Because AWS Lambda runs in a Amazon Linux environment, to run external modules you must

Create the OpenCV build environment

sudo yum update -y
sudo yum install gcc-c++ cmake python27-pip -y

To build the OpenCV run the following in the home directory of the EC2

  1. Download OpenCV
  2. Create a project virtual environment and install numpy
  3. Build OpenCV
cd ~
git clone https://github.com/Itseez/opencv.git
cd opencv
git checkout 3.2.0

cd ~
git clone https://github.com/Itseez/opencv_contrib.git
cd opencv_contrib
git checkout 3.2.0

virtualenv project
source project/bin/activate
pip install numpy
mkdir local
cd opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D BUILD_SHARED_LIBS=NO \
      -D WITH_FFMPEG=ON \
      -D BUILD_opencv_python2=ON \
      -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
      -D CMAKE_INSTALL_PREFIX=~/local ..

IMPORTANT!

Make sure under Python 2, the attribute for "Interpreter", "Libraries", "Numpy" and "package path" are set properly.

The Interpreter points to the Python 2.7 binary in the cv virtual environment.

Libraries points to the Python 2.7 library (which we installed during the final step of Step #1).

The numpy value points to our NumPy installation in the cv virtual environment.

And finally, the packages path points to lib/python2.7/site-packages . When combined with the CMAKE_INSTALL_PREFIX , this means that after compiling OpenCV, we’ll #find our cv2.so bindings in /usr/local/lib/python2.7/site-packages/

make
sudo make install

cp ~/local/lib/python2.7/site-packages/cv2.so ~/project/lib64/python2.7/site-packages/

As an example, to run the hello.py in AWS Lambda.

from __future__ import print_function

import cv2
import numpy as np
import boto3

def lambda_handler(event, context):
    print(cv2.__version__)
    print(np.__version__)
    print("hello")
    return event['key1']  # Echo back the first key value

Run the following command to zip the dependencies and hello.py and upload to s3 Important Note: The script must be world readable

chmod u=rwx,go=r hello.py
zip -9 bundle.zip hello.py 
cd $VIRTUAL_ENV/lib/python2.7/site-packages
zip -r9 ~/bundle.zip *
cd $VIRTUAL_ENV/lib64/python2.7/site-packages/
zip -r9 ~/bundle.zip *
cd
aws s3 cp bundle.zip s3://<S3 BUCKET HERE>/S3/KEY/HERE/bundle.zip

This will allow you to run manual tests in AWS Lambda via the console

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment