11

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.

2
  • 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? Commented 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-lambda Commented Jan 14, 2016 at 5:49

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

0

You would need to build your code in Amazon Linux environment.

Please see my answer: https://stackoverflow.com/a/39649170/996926

Comments

0

You must to add cv2 like a Lambda Layer. Check this repo and do the following steps:

  1. Clone the repo
  2. Pick a version of opencv
  3. Upload to a bucket
  4. Create a Lambda Layer with the zip
  5. Add the Layer to your Lambda.

it's all, enjoy it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.