In Python, trying to run the opencv package in an AWS lambda layer. Using opencv-python-headless but keep getting this error.
Response
{
"errorMessage": "Unable to import module 'lambda_function': /lib64/libz.so.1: version `ZLIB_1.2.9' not found (required by /opt/python/lib/python3.8/site-packages/cv2/../opencv_python_headless.libs/libpng16-186fce2e.so.16.37.0)",
"errorType": "Runtime.ImportModuleError",
"stackTrace": []
}
Have tried different versions of opencv to no avail. And different versions of python.
4
Answers
You can create layer or just (if making layer isn’t all mandatory) –
install the necessary libraries in the same directory your lambda code in using
pip install opencv-contrib-python -t .
(. Means current directory, change if needed).After downloading all libraries zip them (along with the lambda) and store on a
s3 bucket
. Then just source lambda from that zip file and you should be good to go.Best wishes.
I experienced the same issue as you. This issue occurred only after the latest update of openCV (4.7.0.68). Going back to the previous version 4.6.0.66 fixed the problem for me:
This is a known problem with the new update. See this github issue for more info: https://github.com/opencv/opencv-python/issues/772
You can use prebuiilt layers too.
https://github.com/076923/aws-lambda-python-opencv-layer
In your
requirements.txt
file you probably didn’t specify a specific version foropencv-python-headless
– Thus each time you deploy a new image it installs the newest one. And… guess what… the newest release was 2 weeks ago – and it appears not to be compatible with your environment. So:Always
specify the specific version you are using.