55

I have recently started to use AWS Lambda to use triggers against some python code I have written. I currently have 2 lambda functions, both of which have been created with ZIP files. The second one I created is supposed to test the trigger events.

This is for testing purposes so I'm using the best code of all:

def lambda_handler(event, context):
    print ("Hello World")

However, I get this error back:

Response:
{
  "errorMessage": "Unable to import module 'lambda_function'"
}

Request ID:
"65024f16-172c-11e8-ab26-27ff3322e597"

Function Logs:
START RequestId: 65024f16-172c-11e8-ab26-27ff3322e597 Version: $LATEST
Unable to import module 'lambda_function': No module named 'requests'

END RequestId: 65024f16-172c-11e8-ab26-27ff3322e597
REPORT RequestId: 65024f16-172c-11e8-ab26-27ff3322e597  Duration: 15.93 ms  
Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 22 MB  

Everywhere I have searched for this, the answer was solved by making sure the names for the functions were correct or making sure the .zip file was readable. I have satisfied both of these conditions (the name of the file is lambda_function.py and it is in the root).

Alternatively, it seems like it might be an issue with the logs. I double checked my permission and I have the ability to create logs with all resources. Any other ideas what the issue might be?

12 Answers 12

77

requests library doesn't come by default in lambda. It looks like you are trying to import it in your function / library somewhere. To import it, you need the following line:

from botocore.vendored import requests

Alternatively, you would need to zip the requests library in the root of your zip file.

EDIT: There may be a dependency in one of your libraries that may need this. To overcome this, install requests in your application zip. To do this, run the following command in the root directory of your application: pip install requests -t ./.

A better way would be to create a file called requirements.txt and add all the dependencies in there. Use virtualenv to install all the packages defined in the requirements.txt using: pip install -r requirements.txt -t ./


UPDATE: Starting 10/21/19, the vendored version of the requests library in botocore will be removed. Refer this blog post for more details.

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

8 Comments

I saw that answer as well, just tried it again and no luck.
Install requests using the command in your application's root directory: pip install requests -t ./. Once it is completed, zip the application and upload it to the function. If you have a requirements.txt, add requests to it.
What if I don't want to install it in the same directory? pip install requests -t ./ will save it in the same directory
What is botocore.vendored?
|
24

This will surely work. Just follow the steps:

Create a "python" directory inside any empty directory and pip install the modules there

mkdir lambda_layers
cd lambda_layers
mkdir python
cd python
pip install requests -t ./
cd ..
zip -r python_modules.zip .

If you want to have multiple modules in a single layer then install them inside the same 'python' directory that you have just created.

Just make sure that you zip the "python" directory itself recursively with '-r'. That way lambda handler can locate the module in the default python version that you are using.

Now you have your 'python_modules.zip' file with all the dependent modules inside. Go to the Layers of Lambda in the AWS console and create a layer uploading this zip file. Choose the runtimes as per your python version that you are using in your lambda function, or you can select multiple python runtime versions. Add this layer to your lambda function and you should be able to import your modules flawlessly.

4 Comments

Layers solved the issue for me. Thank you very much Bhaskar.
Best answer for how to add a layer to your lambda function. This should be on the AWS website
This is surely the best way to create the layer.
This is the right answer for this problem.
14

Give it a check to this answer

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

Comments

12
Unable to import module 'lambda_function': No module named 'requests'

Please try using python3.7 as a runtime. It will fix the requests issue!

2 Comments

This solved the issue for me. Do you know why that is?
I tried the solutions above, but this solved it. I was using v3.11 of python environment.
3

step1- mkdir python

step2- pip3 install -t python requests

step3- zip python folder

step4- create new layer in aws lambda function and upload this zip file

step5- add this layer in your lambda function

Comments

2

https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/

AWS removed the vendored version of requests from Botocore.

Steps:

  1. cmd >> pip install requests

  2. Python code:

    import requests
    response = requests.get('https://...') 
    

Comments

1

So this is what resolved in my case

enter image description here

Handler :: "yfinance_lamdba.lambda_handler"

In the above line

               **yfinance_lamdba** is filename 'yfinance_script.py'

               **lambda_handler** is function 'def lambda_handler' which has actual code

3 Comments

Welcome to SO! I couldn't understand your answer properly. Could you please edit it so your suggested solution will be slightly more clear? Thanks!
Added more info, let me know if you need further clarification@MyKoryto
python 3.7 also worked, good enough for a hacky prototype
1

This usually happens if you had missed something to add to your lambda Layers. I tried replicating one of my lambda functions and forgot to import the lambda layer that I was having in my old lambda function, so after importing the file into layers, the API started working fine. May be this might help anyone.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

try using the wheel files, it will work.

    'https://pypi.org/project/pytz/#files'
    'https://pypi.org/project/pandas/#files'
    'https://pypi.org/project/numpy/#files'

Need to unzip locally and merge these files then zip it before uploading to AWS Lambda function.

Comments

0

I think the best way is to use serverless frame work for lambda deployments. It is very adaptable with its plugins. You can create a base python lambda with:

serverless create --template aws-python3 --path python_example_service

It will create also basic serverless.yml file. You can use plugin: serverless-python-requirements. It adds all packages listed in the requiremnts.txt file to the lambda zip. That is how you would usually state dependencies for a python app. Make sure serverless.yml contains the plugin:

plugins:
  - serverless-python-requirements

Obviously you would need to install the plugin with:

serverless plugin install --name serverless-python-requirements

you can then deploy lambda with e.g.:

serverless deploy --region us-west-2

Comments

0

I resolved this issue with not really the efficient solution, but it worked somehow. What I did was, I went to requests python library website's download section (https://pypi.org/project/requests/#files) And then downloaded the Source Distribution (requests-2.31.0.tar.gz) file. Extracted the file using WinRAR, copy pasted the file at lambda.py containing folder. Then uploaded the zip of all this to AWS lambda and it was working.

Comments

0

I had this issue even after creating a Layer, it results that my zip was incorrect as it was including the wrapping folder before the python one.

requests_layer.zip
    src
       python

zip must have the root at python

Comments

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.