OpenCV with AWS Lambda via Layers
AWS Lambda has wonderful functionality called Lambda layers. The layer is the zip file that enables you to add custom runtime, additional package/library, data or any dependency which does not come inbuilt as a part of the default Lambda function runtime. The good part is that the lambda layers can be leveraged across multiple lambda functions, hence it is reusable and keeps the deployment package small. This allows you to focus on business logic.
Here, we will look at the steps to create the lambda layer for the image and video processing python library i.e. OpenCV and use it as a part of the Lambda function. Here, we will create the OpenCV layer for Python3.8 runtime within the lambda function.
Steps to configure and upload OpenCV package to S3
- Create an IAM role for EC2 service along with S3 permission to upload a zip file to S3. Attach a managed policy: AmazonS3FullAccess Or create the custom policy that allows uploading a file from EC2 to S3
- Create the S3 bucket or use any existing bucket (Here, we will upload the zip file of the OpenCV package)
- Spin-up an EC2 instance (t2.micro) with Ubuntu 20.04. Within Ubuntu 20.04, python version 3.8 comes pre-installed. And if you want to create the package for Python 3.9 runtime then one can upgrade the Python version and follow the rest of the steps
- Attach the IAM role to the instance
- Post launching the instance, SSH into the instance to configure OpenCV layer
- After SSH into an instance, execute below commands
- sudo apt-get update
- python3 -V (Here, we are checking the version of the Python. Since we are creating the layers for Python 3.8 runtime)
- sudo apt-get install python3-pip (Installing python package manager)
- sudo apt install zip (Installing zip functionality to create the zip file)
- mkdir -p build/python/lib/python3.8/site-packages (Creating the directory structure). However, once can also create short path i.e. mkdir -p build/python
Note: The path is crucial, please double check
- pip3 install opencv-python-headless -t build/python/lib/python3.8/sites-packages/ (Installing OpenCV package/module in the custom directory i.e. sites-packages). If you have create the short path then the command would be pip3 install opencv-python-headless -t build/python/
Note: If you install opencv-python then it will throw an exception, hence we are installing the headless version of OpenCV. To learn more, please refer to this video on OpenCV libGL.so.1 error resolved | Lambda Layers
- cd build
- zip -r opencv-layer.zip . (Archiving package)
- aws s3 cp opencv-layer.zip s3://layers-opencv (Uploading package to S3 bucket)
Navigate to the Lambda Management Console -> Layers -> Create Layer. Fill up the asked configuration details as shown in the reference image below and Create Layer.
Create Lambda function
As a next step, create the lambda function with Python runtime 3.8. Once the function is created follow below steps.
- Scroll down to Layers in the Designer Pane
- Add a Layer
- Select Custom layers under Choose a layer
- From the drop-down select the opencv-layer and the relevant version
- Click Add
Now, we are ready to use the image/video processing library in our lambda function so go ahead and import cv2 package in the lambda function. You can also refer the video tutorial for the same.