7

I'm currently trying to use the Python requests module in an AWS Lambda function. Here are the steps that I've taken so far:

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

I then wrote a simple Python script, test_requests.py, within the directory, which looks like this:

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

I zipped the entire directory, including the requests module, using zip test_requests.zip *

I then uploaded the function to 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 invoked the function with this command: aws lambda invoke --function-name test-requests --payload {} --region us-east-1 lambda_response.txt

When I made this command, I got an unhandled exception back from Lambda. The output file, lambda_response.txt contained this: {"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've seen several questions regarding AWS lambda, and being unable to import modules properly. Those questions all seemed focused around lambda being unable to find the module. In this case, it seems that lambda has found requests, but is unable to access all of its attributes.

5
  • Check the version of python, you are trying to run the script Commented Jul 23, 2017 at 14:41
  • @bigbounty pip3 -V returns pip 9.0.1 from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (python 3.6) Commented Jul 23, 2017 at 14:52
  • Why did you zip requests module? Doesn't it come with Python distribution in Lambda? Commented Jul 23, 2017 at 15:05
  • Why not download the requests by git clone, then zip it and upload to AWS and install it by python setup.py install Commented Jul 23, 2017 at 15:12
  • I have just tested your exact process (except I'm using python 2.7), and ... it's working. I think you may have a bad requests.py or requests.pyc file in your directory that is shadowing the real requests module. Commented Jul 23, 2017 at 15:30

1 Answer 1

20

I figured out what I was doing wrong. zip test.zip * only zips the top level of the directory structure. I needed the -r flag in order to capture everything.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for documenting it! I was going to spend the whole day trying find a solution related to anything other than that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.