Python in AWS Lambda: "requests" modules have no "get" attribute

I am currently trying to use a Python module requests

in an AWS Lambda function. Here are the steps I have taken so far:

I created a new directory and installed the requests module in it using the command pip3 install requests -t .

Then I wrote a simple Python script file test_requests.py

in a directory that looks like this:

import requests
def my_handler(event, context):
    r = requests.get("http://google.com")
    return r

      

I have pinned the entire directory including the requests module using zip test_requests.zip *

Then I loaded the function into AWS with the following command: aws lambda create-function --function-name test_requests --zip-file fileb://test_requests.zip --handler test_requests.my_handler --runtime python3.6 --region us-east-1 --role xxxMY_ROLE_ARNxxx

Finally, I called the function with this command: aws lambda invoke --function-name test-requests --payload {} --region us-east-1 lambda_response.txt

When I did this command, I got an unhandled exception from Lambda. The output file lambda_response.txt

contains the following:{"errorMessage": "module 'requests' has no attribute 'get'", "errorType": "AttributeError", "stackTrace": [["/var/task/test_requests.py", 3, "my_handler", "r = requests.get('http://google.com')"]]}

I saw several questions regarding AWS lambda and was unable to import modules correctly. All these questions seemed to be centered around a lambda unable to find a module. In this case, it appears that the lambda has detected the requests, but cannot access all of its attributes.

+3


source to share


1 answer


I figured out what I was doing wrong. zip test.zip *

just buttons up the top level of the directory structure. I needed a flag -r

to capture everything.



+11


source







All Articles