3

I created an AWS lambda with Python 3.6, which uses an external library not already in the AWS env. Once deployed with serverless, I call the handler and it gives me the error:

START RequestId: 123456 Version: $LATEST
Unable to import module 'my_handler': No module named 'greenlet._greenlet'

END RequestId: 123456
REPORT RequestId: 123456    Duration: 0.35 ms   Billed Duration: 1 ms   Memory Size: 1024 MB    Max Memory Used: 191 MB Init Duration: 1591.04 ms

I have a requirements.txt file where I'm trying to get gevent and greenlet right from source, since I read Linux can't manage these binary files. Looks like this:

gevent @ git+git://github.com/gevent/gevent.git@c780319e87025130a06f7d2eb9e33ff6d25361dd
greenlet @ git+git://github.com/python-greenlet/greenlet.git@c0e16261293cb401b643290adbb2d824c7200388

Before, I imported them like this:

gevent
greenlet==1.0.0

In the serverless.yml I also specified

plugins:
  - serverless-python-requirements
custom:
  pythonRequirements:
    pythonBin: python3
    zip: true
    gevent: true
    greenlet: true

Any help will be appreciated, thank you so much!

Barbi
  • 139
  • 1
  • 11
  • I think greenlet has to be compiled so there's a compatible binary on your lambda. I believe Serverless provides a way to do that via Docker. See the section on "cross compiling" here: https://www.serverless.com/plugins/serverless-python-requirements – Matt Morgan May 04 '21 at 10:55
  • Thank you @MattMorgan , it seems to make sense. Unfortunately, I tried adding `dockerizePip: true` to serverless.yml , but I still get the same error when I call the handler. Don't know what I'm missing in the dependencies / configuration with this module – Barbi May 04 '21 at 12:02
  • You need to take the extra steps of creating and using the dockerfile as explained in the link I posted. Essentially, you need to replace the default linux image lambda normally deploys with your own. – Matt Morgan May 04 '21 at 12:23
  • @MattMorgan I managed to correctly import the module, and the answer came from AWS itself: [link] (https://aws.amazon.com/it/premiumsupport/knowledge-center/lambda-python-package-compatible/) . I did this with greenlet module and eventually worked on AWS. Still, the very first time I call the handler it gives `{ "errorMessage": "[Errno 13] Permission denied: '/tmp/sls-py-req/playwright/driver/playwright.sh'", "errorType": "PermissionError", "stackTrace": [..]`, then if works like a charm. – Barbi May 04 '21 at 13:51
  • Great. That's another way to do it, but be aware that it may break if AWS updates their linux environment for lambda-- the old binary may no longer run. – Matt Morgan May 04 '21 at 14:00
  • @MattMorgan That's a really good point, thank you. I'll dig into it! – Barbi May 04 '21 at 14:57
  • please consider answering your own question--it may help others in the future. – Matt Morgan May 04 '21 at 15:46

1 Answers1

0

I managed to import the module using Docker instead of import greenlet from its wheel. You need to import a Docker image already set up for playwright, otherwise it won't be able to compile, like this one: mcr.microsoft.com/playwright:v1.10.0-focal.

Another way is to to use a Ubuntu-based image and run: RUN playwright install-deps

I have to say that, even this way, it was very difficult to make the module work properly and I abandoned the idea to use lambda and playwright, but I got pretty far and I hope this info may help someone!

Barbi
  • 139
  • 1
  • 11