keithrozario / Klayers

Python Packages as AWS Lambda Layers
Other
2.18k stars 313 forks source link

[BUG] OpenCV Headless doesn't work with lambda #115

Open Ryan-Daly opened 4 years ago

Ryan-Daly commented 4 years ago

Describe the bug OpenCV Headless isn't working with Lambda Layers. Getting an error:

{
  "errorMessage": "Unable to import module 'lambda_function': libgthread-2.0.so.0: cannot open shared object file: No such file or directory",
  "errorType": "Runtime.ImportModuleError"
}

Lambda function:

import json
import cv2

def lambda_handler(event, context):
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

Layer Version ARN: arn:aws:lambda:eu-west-2:770693421928:layer:Klayers-python38-opencv-python-headless:11

Framework: Console

adriaanbd commented 4 years ago

Just tested it on us-east-1 and can confirm same error

image

image

keithrozario commented 4 years ago

I suspect it doesn't have opencv itself, and not just the headless bit. Let me check.

adriaanbd commented 4 years ago

If I change the Python version to 3.7 it does say there is no module cv2 image

vliegenthart commented 4 years ago

Are there perhaps any updates on this bug? I'm running into the same error for this layer

keithrozario commented 3 years ago

Sorry guys, haven't had time to look into this. Overall, we just pip install the package, and zip that up into a layer.

I suspect in order to use this, we'll need that shared-object file which isn't installed with pip. :(

vliegenthart commented 3 years ago

Gotcha, thank's for the heads up 👍

tbarusseau commented 3 years ago

I'm having the same issue.

opencv-python-headless depends on a native library (libglib), and AFAIK it's not possible to create a Lambda layer which installs system dependencies for Lambda.

Is there a workaround? I know that this exists, but generating a shared library on an Amazon Linux EC2 instance to ship it later in a ZIP archive for a Lambda function seems... Not very robust? I mean if that's the only way to get it to work, I'll try!

keithrozario commented 3 years ago

FIX:

I created a new layer that contains the missing dependencies, would you mind testing it out.

The layer arn is: arn:aws:lambda::770693421928:layer:Klayers-python38-libgthread-so:1

If you include both the opencv layer and the layer above, the error message no longer appears.

Screen Shot 2021-08-08 at 1 41 36 PM

more info here (for further reference): https://www.keithrozario.com/2021/08/missing-so-files-in-lambda-functions.html