3

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.

John Welsh
  • 379
  • 4
  • 6
  • .so files are linked with the operating system. Since lambda use amazon linux 2 i.e a variant of centos. You will have to built the layer in amazon linux image docker and statically link and build it. The error u r getting is due the missing .so files that is due to not building lib in amazon linux 2 compatible os – Gunesh Shanbhag Jan 01 '23 at 05:53
  • hope you follow these steps https://stackoverflow.com/q/64016819/5235168 – Gunesh Shanbhag Jan 01 '23 at 05:56
  • 1
    you can use prebuiilt layers too https://github.com/076923/aws-lambda-python-opencv-layer – Gunesh Shanbhag Jan 01 '23 at 06:10
  • prebuilt layers worked! Thanks alot – John Welsh Jan 02 '23 at 03:58

4 Answers4

7

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:

pip install opencv-python-headless==4.6.0.66

This is a known problem with the new update. See this github issue for more info: https://github.com/opencv/opencv-python/issues/772

2

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.

ashraf minhaj
  • 504
  • 2
  • 14
  • And how exactly are you going to guarantee that this works on lambda and all binary libraries will be compatible with aws environments? – Marcin Jan 01 '23 at 11:10
  • @Marcin , since you can select lambda code language with version, you can install version specific libraries and test on your local machine first. Worked for me whenever I used it. Because that way all the libraries and their dependencies also get installed. – ashraf minhaj Jan 01 '23 at 11:17
  • @ashrafminhaj this didn't seem to work as I go above the size limit within the AWS function. The zipped file is 80mb. Receive error message 'Unzipped size must be smaller than 262144000 bytes' – John Welsh Jan 02 '23 at 04:32
  • @JohnWelsh as [the doc](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html) says - **50 MB (zipped, for direct upload) 250 MB (unzipped) This quota applies to all the files you upload, including layers and custom runtimes. 3 MB (console editor)**. Try uploading in s3 bucket then reference it in lambda. – ashraf minhaj Jan 02 '23 at 04:50
  • That is what I've done, unfortunately doesn't work even when referenced as the limits still apply. – John Welsh Jan 02 '23 at 05:03
0

You can use prebuiilt layers too. https://github.com/076923/aws-lambda-python-opencv-layer

Gunesh Shanbhag
  • 559
  • 5
  • 13
0

In your requirements.txt file you probably didn't specify a specific version for opencv-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:

  1. Always specify the specific version you are using.
  2. Specify version 4.6.0.66, as @job-heersink suggested.
Binyamin Even
  • 3,318
  • 1
  • 18
  • 45