opencv layer does not work

0

Hi Team, I created an opencv layer for my python3.9 app in lambda as below, but no one works

  • arn:aws:lambda:ap-southeast-1:777606541103:layer:opencv_layer:5
  • arn:aws:lambda:ap-southeast-1:777606541103:layer:opencv_layer:4
  • arn:aws:lambda:ap-southeast-1:777606541103:layer:opencv_layer:3
  • arn:aws:lambda:ap-southeast-1:777606541103:layer:opencv_layer:2

our lambda is built with Ubuntu, and I tried building the layer with both Ubuntu and Amazonlinux, But no one works. I got the Error on the deployment and I attached the screenshot of the error

ModuleNotFoundError: No module named 'cv2' !(/media/postImages/original/IMWhXjpO-NR7y_yEyOdY6Zdg)

1. Docker file content Ubuntu Docker File FROM ubuntu RUN apt-get update &&
apt-get install -y python3.9 python3-pip &&
pip3 install opencv-python-headless`

AmazonLinux Docker File *FROM amazonlinux

RUN yum install -y python3.9 RUN yum install -y python3-pip RUN pip3 install opencv-python-headless*

  1. Extract OpenCV Artifacts: docker create --name opencv-extractor opencv-builder docker cp opencv-extractor:/usr/local/lib64/python3.9/site-packages/ ./opencv_layer/

  2. Create a Lambda Layer Package cd opencv_layer zip -r opencv_layer.zip .

  3. Upload and create the layer listed at start of the questions

  4. The below is part of my Chalice config.json file for deployment "stages": { "dev": { "layers": [ "arn:aws:lambda:ap-southeast-1:777606541103:layer:opencv_layer:5" ], "autogen_policy": true,

I swithed the os between amazonlinux and ubuntu and swithed the folder of the zip file, but I have not found the correct way on it. Please help to check.

Thank you

Gavin
asked 4 months ago226 views
2 Answers
0

The problem is how you’re installing and zipping the python libraries.

Every thing needs to be in a python folder as follows.

First, create a directory to hold everything. Replace my_layername with the name of your layer package

mkdir -p my-layername/python

Now install everything into the python directory

cd lambdalayer
cd python
pip3 install opencv-python-headless --target=.

Optional, but recommended -- remove unneeded files to reduce the size of your layer package. Note the last exit is only if you are running this inside a container:

rm -rf *.dist-info
find . -name "tests" -type d | xargs -I{} rm -rf {}
find . -name "docs" -type d | xargs -I{} rm -rf {}
find . -name "__pycache__" -type d | xargs -I{} rm -rf {}
rm -rf boto*

Now zip up the layer

cd my-layername
zip -r my-layername.zip python

Upload your layer to lambda.

profile picture
EXPERT
answered 4 months ago
0

Hello,

In order to import "opencv" module in your Lambda function as a layer you can refer to the below mentioned GitHub repository[+], it has the detailed steps mentioned on how to import cv2 in your Lambda function.

[+] https://github.com/awslabs/lambda-opencv

Additionally, you can create container based Lambda function and bake your on customer modules in the Dockerfile as it give you the ability to add modules/dependencies beyond 250MB.

[+] https://docs.aws.amazon.com/lambda/latest/dg/python-image.html

Thank you

AWS
SUPPORT ENGINEER
Pallavi
answered 4 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions