106

I need to do a rest-call within a python script, that runs once per day. I can't pack the "requests" package into my python-package using the AWS Lambdas. I get the error: "Unable to import module 'lambda_function': No module named lambda_function"

I broke it down to the hello_world predefined script. I can pack it into a zip and upload it. Everything works. As soon as I put "import requests" into the file, I get this error.

Here is what I already did:

  1. The permissions of the zip and the project folder (including subfolders) are set to `chmod 777`. So permissions shouldn't be a problem.
  2. The script itself is within the root folder. When you open the zip file, you directly see it.
  3. I installed the requests package into the root-folder of the project using `sudo pip install requests -t PATH_TO_ROOT_FOLDER`

The naming of everything looks like this:

  • zip-file: lambda_function.zip
  • py-file: lambda_function.py
  • handler method: lambda_handler(event, context)
  • handler-definition in the "webconfig: lambda_function.lambda_handler

The file I want to run in the end looks like this:

import requests
import json


def lambda_handler(event, context):
    url = 'xxx.elasticbeanstalk.com/users/login'
    headers = {"content-type": "application/json", "Authorization": "Basic Zxxxxxxxxx3NjxxZxxxxzcw==" }
    response = requests.put(url, headers=headers, verify=False)
    return 'hello lambda_handler'

I'm glad for ANY kind of help. I already used multiple hours on this issue.

8
  • 2
    In your question, the position of the -t option in the pip install command is incorrect - it must be pip install requests -t PATH_TO_ROOT_FOLDER . Did you just mistype it, or this is how you really ran it?
    – Leon
    Commented Nov 22, 2016 at 12:09
  • @Leon: Right. Just checked the history and I did it like you mention it here. :) So that didn't cause the problem.
    – codepleb
    Commented Nov 22, 2016 at 12:10
  • What version of Python do you use locally?
    – Leon
    Commented Nov 22, 2016 at 12:14
  • @Leon: 2.7.12 (15chars)
    – codepleb
    Commented Nov 22, 2016 at 12:19
  • Have you seen the guide at docs.aws.amazon.com/lambda/latest/dg/… ?
    – Klaus D.
    Commented Nov 22, 2016 at 12:19

9 Answers 9

242

EDIT: On Oct-21-2019 Botocore removed the vendored version of requests: https://github.com/boto/botocore/pull/1829.

EDIT 2: (March 10, 2020): The deprecation date for the Lambda service to bundle the requests module in the AWS SDK is now January 30, 2021. https://aws.amazon.com/blogs/compute/upcoming-changes-to-the-python-sdk-in-aws-lambda/

EDIT 3: (Nov 22, 2022): AWS cancelled the deprecation so you can continue to use requests as described below. AWS Blog

To use requests module, you can simply import requests from botocore.vendored. For example:

from botocore.vendored import requests

def lambda_handler(event, context):
   response = requests.get("https://httpbin.org/get", timeout=10)
   print(response.json())

you can see this gist to know more modules that can be imported directly in AWS lambda.

11
  • 24
    Is there a concern that these might go away in later versions? " While these vendored dependencies are still in the botocore package they should not be used as they will be removed in the future" botocore.amazonaws.com/v1/documentation/api/latest/…
    – Joshka
    Commented Nov 26, 2018 at 20:27
  • 5
    I would not use this method anymore. The official repo for botocore has a warning saying that botocore.vendored requests will be removed in the future.
    – cvb
    Commented Sep 18, 2019 at 20:45
  • 2
    @MichaelBrant, you'll need to upload the requests library as part of your function. docs.aws.amazon.com/lambda/latest/dg/…
    – Sathed
    Commented Oct 10, 2019 at 0:09
  • 44
    A note - this no longer seems to work. AttributeError: module 'botocore.vendored.requests' has no attribute 'get'
    – Penumbra
    Commented Dec 12, 2019 at 1:07
  • 4
    Right, the blog says "The Lambda runtimes for Python 3.8 and later do not include the ‘requests’ module."
    – Molossus
    Commented Dec 12, 2022 at 12:34
73

If you're working with Python on AWS Lambda, and need to use requests, you better use urllib3, it is currently supported on AWS Lambda and you can import it directly, check the example on urllib3 site.

import urllib3

http = urllib3.PoolManager()
r = http.request('GET', 'http://httpbin.org/robots.txt')

r.data
# b'User-agent: *\nDisallow: /deny\n'
r.status
# 200
1
  • 3
    This is the best answer.
    – CLAbeel
    Commented Jun 16, 2021 at 16:41
26

I finally solved the problem: The structure in my zip file was broken. It is important that the python script and the packed dependencies (as folders) are in the root of the zip file. This solved my problem.

It's a bit depressing if you find such easy errors after hours of try and failure.

9
  • Does your script return json then? I'm trying to do similar, but keep getting 'not json serialisable'. I don't have 'import json' however... code works fine locally without 'import json' so not sure why it doesn't in Lambda.
    – kafka
    Commented Jan 20, 2017 at 12:38
  • @kafka You may need to import the json library. Not sure though, but AWS uses python 2.7 and not 3.x. My script doesn't return json.
    – codepleb
    Commented Jan 20, 2017 at 12:41
  • yeah python 2.7 here. Going to attempt with explicit reference to json library. Currently re-packaging.
    – kafka
    Commented Jan 20, 2017 at 12:42
  • OK no joy here, in your code you simply return 'hello lambda_helper', I want to return the entire JSON response from the API Get though.... no worries if you don't know I might do a new question
    – kafka
    Commented Jan 20, 2017 at 12:46
  • I return status codes. But yeah, I'm not much into python. It was barely enough to write that script.
    – codepleb
    Commented Jan 20, 2017 at 12:53
24

I believe you have lambda_function.py on the Lambda console. You need to first create the Lambda function deployment package, and then use the console to upload the package.

  • You create a directory, for example project-dir on your system (locally)
  • create lambda_function.py in project-dir, copy the content of lambda_function.py from lambda console and paste it in project-dir/lambda_function.py
  • pip install requests -t /path/to/project-dir
  • Zip the content of the project-dir directory, which is your deployment package (Zip the directory content, not the directory)

Go to the Lambda console, select upload zip file in code entry type and upload your deployment package. Import requests should work without any error.

1
  • 2
    you can use pip install requests -t . (that's a dot after the -t) from within the directory containing your lambda_function.py rather than typing out the full project dir path. (form qarly_blue's answer). Bonus: Also works on windows
    – Hansang
    Commented Jan 13, 2020 at 11:33
17

Most of the comments somehow correct, but not enough informative for AWS beginners. Here is my long resume what needs to be done for accessing requests functionality:

1. Creates root folder for AWS Lambda function
% mkdir lambda-function
2. Go inside crated root folder
% cd lambda-function
3. Create entry point Python file for AWS Lambda.
% vi lambda_function.py
4. Paste a code into lambda_function.py
import requests
def lambda_handler(event, context):   
    response = requests.get("https://www.test.com/")
    print(response.text)
    return response.text 
5. Install requests library. Note:package folder created
% pip install --target ./package requests
6. Go inside package
% cd package
7. Zip package
zip -r ../deployment-package.zip .
8. Go into parent folder
% cd ..
9. Zip deployment packge and lambda function file
% zip -g deployment-package.zip lambda_function.py
  1. In the AWS Lambda functions tap "Upload from" and pick ".zip file". Navigate to your zip package zip file: deployment-package.zip.
  2. After upload all files will be inside AWS Lambda function.

enter image description here

11

With this command download the folder package

pip install requests -t .

Run this command on your local machine, then zip your working directory, then upload to aws.

2
  • 4
    don't think this applies to AWS Lambda Commented Apr 1, 2019 at 21:51
  • this answer comined with the one below by pramod munemanik together is currently the best answer
    – Hansang
    Commented Jan 13, 2020 at 11:53
8

python 3.8 windows 10

lambda is looking for a specific folder structure and we are going to recreate in this manner in the steps below (https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-create): https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-create

  1. make a folder on your desktop called "python," open a cmd terminal: cd desktop
  2. pip install --target python requests
  3. right click your python folder and zip it and rename the zip to 'requests.zip' - now if you look inside the zip you should see the python folder.
  4. aws console > lambda > layers > create layer => name layer/upload requests.zip
  5. aws console > functions > create function => in the "designer" box select layers and then "add layers." Choose custom layers and select your layer.
  6. Go back to the function screen by clicking on the lambda symbol in the designer box. Now you can see "function code" again. Click lambda_function.py

Now you can import requests like this:

import json
import requests

def lambda_handler(event, context):
    # TODO implement
    response = requests.get('your_URL')
    return {
        'statusCode': 200,
        'body': json.dumps(response.json())
    }
1
  1. Copy whatever you have in the lambda_function fron AWS lambda console and paste it in a new python script and save it as lambda_function.py.

  2. Make a new folder (I name it as package) and save requests module in it by running the following code in terminal: pip install -t package requests

  3. Move lambda_function.py into the folder (package).

  4. Go to the folder and select all content and zip them.

  5. Go back to the AWS Lambda console. select the function and under the Function code section, click on 'Action' (on the right side) and select Upload a .zip file.

  6. Upload the folder. lambda_function should be uploaded automatically.

  7. Run and Enjoy.

1
0

Add a layer to your lambda function by specifying this arn (ap-south-1)

 arn:aws:lambda:ap-south-1:770693421928:layer:Klayers-p38-requests-html:10
1
  • 5
    Where does this ARN come from? Adding non-official code to your lambda seems like a dangerous action
    – Ryan Amos
    Commented May 10, 2023 at 17:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.