OpenCV with AWS Lambda via Layers

Written by: Chirag (Srce Cde)

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.

opencv lambda layer

Steps to configure and upload OpenCV package to S3

  1. 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
  2. Create the S3 bucket or use any existing bucket (Here, we will upload the zip file of the OpenCV package)
  3. 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
  4. Attach the IAM role to the instance
  5. Post launching the instance, SSH into the instance to configure OpenCV layer
  6. 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/
    • cd build
    • zip -r . (Archiving package)
    • aws s3 cp s3://layers-opencv (Uploading package to S3 bucket)

Creating Layer

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.

opencv lambda 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.

Bonus: Image processing using OpenCV within Lambda function