Because AWS Lambda runs in a Amazon Linux environment, to run external modules you must
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
- Download OpenCV
- Create a project virtual environment and install numpy
- 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 ..
Make sure under Python 2, the attribute for "Interpreter", "Libraries", "Numpy" and "package path" are set properly.
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