tenichols
tenichols

Reputation: 19

Unable to import grequest in AWS Lambda (Python 3.X)

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?

Upvotes: 0

Views: 866

Answers (2)

Marcin
Marcin

Reputation: 238249

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!')
    }

Upvotes: 1

esteban ceron
esteban ceron

Reputation: 11

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.

Upvotes: 0

Related Questions