Member-only story
Creating a Python OpenCV Layer for AWS Lambda
A simple approach to creating an AWS Lambda layer for Python’s OpenCV wrapper
Lambda is a serverless offering by AWS that enables running scalable code without needing to configure servers. Each Lambda environment (corresponding to different versions of the compatible languages) comes with a selection of packages pre-installed.
However, what happens when we require a package that is not provided? There are a few approaches to addressing this shortcoming, one of which is the use of a Lambda layer.
In the next section, we will go over a programmatic solution to create a Lambda layer for a Python 3.7 application that requires OpenCV.
1. Install the Library in a Compatible OS
Lambda functions run on Amazon Linux (AMI). Therefore, to ensure that our version of OpenCV will run on the Lambda, we need to compile it for AMI. One way to do this is to install the library on an EC2 instance.
However, for expediency, we will use Docker. The Dockerfile below creates a Docker image with OpenCV installed: