Reputation: 19
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
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:
layer
and enter the folderrequirements.txt
with content ofgrequests
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"
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
grequests
in your function:import json
import grequests
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Upvotes: 1
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.
Chalice Microframework for AWS, this one is special because is a microframework for python supported by 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