AWS Lambda has the wonderful feature called Function layers. Layers equip you with custom runtime, additional package/library deployment, and one can further use or import the same in the Lambda functions. It keeps the deployment package small and it can be used across multiple lambda functions.

In this tutorial, I’m going to take you through creating the layer for the image and video processing library i.e. OpenCV and leveraging the power in the lambda function. Here, we will use multiple AWS service like

  1. IAM
  2. S3
  3. EC2
  4. Lambda function
  5. Layers

Well, we will start with the IAM management console and proceed with rest of the service one after another.

IAM

Here we will start with creating single role for the EC2 instance access to S3.
IAM role for the EC2 instance (For uploading the package to S3 from EC2) (Write Permission)
Name: layers-ec2-s3-admin
Permission: AmazonS3FullAccess

If you are unaware about how to create the IAM role, then here is the step by step tutorial.

S3

Here, we will create the bucket to upload the OpenCV layer package from EC2 instance. Let’s go ahead and create the bucket. In my case, I have created layers-opencv

EC2

Initially we will launch an EC2 instance. I believe t2.micro (Free tier) with Ubuntu operating system is sufficient for the purpose of this tutorial. And will setting up the instance make sure to attach the IAM role that we have created in the first step. However, one can bind the IAM role at later point of time.

Post launching the instance, we will SSH into the instance and start executing few commands.

  1. sudo apt-get update
  2. python3 -V (Here, we are checking the version of the Python. Since we are creating the layers for Python 3.6 runtime)
  3. sudo apt-get install python3-pip (Installing python package manager)
  4. mkdir -p build/python/lib/python3.6/sites-packages (Creating the directory structure)
  5. pip3 install opencv-python -t build/python/lib/python3.6/sites-packages/ –system (Installing OpenCV package/module in the custom directory i.e. sites-packages)
  6. cd build
  7. zip -r package.zip . (Creating package in the form of zip file)
  8. aws s3 cp package.zip s3://layers-opencv (Uploading package to S3 bucket)

Note: The path is the crucial part here. Make sure the path is correct in step 4.

Layers

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. Here, we have successfully created the Layer for the image/video processing library i.e. OpenCV

opencv with aws lambda

Lambda Function

Let’s create the lambda function and use the package. We will create the service role, since we have not create any IAM role explicitly in the first step. So we will say Create new role from AWS policy templates. I will mention role name as lambda-layer-opencv-s3. And within Policy templates, we will select Amazon S3 object read-only permissions. And create the function.

Once the function is created, click on Layers in the Designer Pane. Scroll down to Referenced layers pane and Add a layer. In the layer dropdown, we will see the opencv-layer and select the relevant version and Add and save the function.

Now, we are ready to use the image/video processing library in our lambda function. Below is the video tutorial for the same.

In the next tutorial we will perform simple image processing operation using Layers and Lambda functions. Keep sharing. Stay tuned for more. Follow me on Twitter