0

When I zip "grequest" and attempt to use it in a AWS Lambda function I get this error:

[ERROR] RuntimeError: Gevent is required for grequests.
Traceback (most recent call last):
  File "/var/lang/lib/python3.8/imp.py", line 234, in load_module
    return load_source(name, filename, file)
  File "/var/lang/lib/python3.8/imp.py", line 171, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 702, in _load
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/var/task/fiveDay_updater.py", line 11, in <module>
    import grequests as asyncReq
  File "/var/task/grequests.py", line 18, in <module>
    raise RuntimeError('Gevent is required for grequests.')

It seems like others have had this problem. Unable to import grequests for AWS Lambda Unfortunately the solution proposed in the above link does not work for me. Does anyone have a "grequest" zip that will work for the later python, or know of a solution to the problem?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
tenichols
  • 19
  • 4

2 Answers2

1

I would recommend an approach which has never failed me and is based on the docker tool lambci/lambda and creating lambda layers.

This has been described in the link below:

To verify, i created layer called grequestslayer.zip as follows:

  1. Create folder layer and enter the folder
  2. In the folder, create requirements.txt with content of
grequests
  1. Runt the following command:
docker run -v "$PWD":/var/task "lambci/lambda:build-python3.8" /bin/sh -c "pip install -r requirements.txt -t python/lib/python3.8/site-packages/; exit"
  1. Zip the layer created:
zip -r grequestslayer.zip python

The layer will contain gevent automatically:

python/lib/python3.8/site-packages/
├── bin
├── certifi
├── certifi-2020.6.20.dist-info
├── chardet
├── chardet-3.0.4.dist-info
├── easy_install.py
├── gevent
├── gevent-20.6.2.dist-info
├── greenlet-0.4.16.dist-info
├── greenlet.cpython-38-x86_64-linux-gnu.so
├── grequests-0.6.0.dist-info
├── grequests.py
├── idna
├── idna-2.10.dist-info
├── include
├── pkg_resources
├── __pycache__
├── requests
├── requests-2.24.0.dist-info
├── setuptools
├── setuptools-49.2.1.dist-info
├── urllib3
├── urllib3-1.25.10.dist-info
├── zope
├── zope.event-4.4.dist-info
├── zope.event-4.4-py2.7-nspkg.pth
├── zope.interface-5.1.0.dist-info
└── zope.interface-5.1.0-py3.8-nspkg.pth
  1. Create lambda layer:

enter image description here

  1. Add the layer to your function:

enter image description here

  1. Use grequests in your function:
import json

import grequests

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

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36
Marcin
  • 215,873
  • 14
  • 235
  • 294
0

I'm not sure if you are using a tool to deploy your functions, if you are not using a linux machine you'll face some issues to generate the zip. My recommendation for you is to use one of these tools to deploy your python functions on AWS.

Those are my recommendations for you so you don't have to deal with the zip generation manually, if you aren't interested probably you will need to use a linux instance to generate your zip.

  • So what is Chalice Microframework? I'm not sure how to use it.. – tenichols Aug 06 '20 at 02:43
  • It is a framework provided by AWS to write serverless apps using Python. You just need to define a virtualenv then install the library and all the libs you require, there are good examples here https://github.com/aws/chalice – esteban ceron Aug 13 '20 at 15:43