I wrote a Lambda function (Python 2.7) that uses OpenCV. I need to import CV2 and I'm having trouble providing the library in my .zip. Has anyone used OpenCV with Lambda? How can I provide the CV2 module.
-
I'n trying to do the same at the moment. I was going to try with a static version of openCV but looks like I still have some shared dependencies while trying to run it on lambda. Did you get any luck in that?Romanzo Criminale– Romanzo Criminale2015-12-29 23:49:19 +00:00Commented Dec 29, 2015 at 23:49
-
I haven't had time to go through and test this, and saw @RomanzoCriminale's similar query on another question. Have you tried anything like this with EC2? markn.ca/2015/10/python-extension-modules-in-aws-lambdaMichael Wills– Michael Wills2016-01-14 05:49:34 +00:00Commented Jan 14, 2016 at 5:49
4 Answers
You must copy the OpenCV inside the zip that you send to AWS Lambda
This method helps to deploy with OpenCV https://github.com/aeddi/aws-lambda-python-opencv/blob/master/build.sh
Comments
If you're only dealing with cv2, I would suggest adding it as a layer - That way you can have access to opencv throughout all your lambda functions which have the same runtime.
I had to package both cv2 and scipy, where the package size was a huge problem, and I came to the following solutions in the end.
Using the serverless-python-requirements package on Serverless helped me streamline this whole process and reduce the package size as well. Would definitely recommend checking it out.
This is the guide that I followed
Serverless python-requirements plugin
Make sure to leave the strip flag to false to avoid stripping binaries which leads to the problem "ELF load command address/offset not properly aligned",
This is what my final serverless.yml came out to be which gave me the results I wanted to package sklearn + cv2 as a layer:
custom:
pythonRequirements:
dockerizePip: true
useDownloadCache: true
useStaticCache: false
slim: true
strip: false
layer:
name: ${self:provider.stage}-cv2-sklearn
description: Python requirements lambda layer
compatibleRuntimes:
- python3.8
allowedAccounts:
- '*'
Comments
You would need to build your code in Amazon Linux environment.
Please see my answer: https://stackoverflow.com/a/39649170/996926
Comments
You must to add cv2 like a Lambda Layer. Check this repo and do the following steps:
- Clone the repo
- Pick a version of opencv
- Upload to a bucket
- Create a Lambda Layer with the zip
- Add the Layer to your Lambda.
it's all, enjoy it