Unable to use Requests-Module on AWS Lambda

I need to make a rest call in a python script that runs once a day. I cannot pack the "requests" package into my python package using AWS Lambdas. I get an error: "Unable to import module" lambda_function ": no module named lambda_function"

I broke it into a predefined hello_world script. I can pack it into a ZIP file and download it. Everything works. As soon as I put the “import requests” into the file, I get this error.

Here is what I have already done:

  • The zip permissions and project folders (including subfolders) are set to `chmod 777`. Therefore, permissions should not be a problem.
  • The script itself is in the root folder. When you open a zip file, you directly see it.
  • I installed the request package in 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 "webconfig: lambda_function.lambda_handler

The file I want to run at the end is as follows:

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 am happy for ANY help. I have already used several hours on this issue.

+8
python amazon-web-services aws-lambda
source share
3 answers

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

This is a little depressing if you find such simple errors after hours of trying and failing.

+8
source share

To use the request 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://example.com/") print response.json 

you can see this meaning to find out more modules that can be imported directly into AWS lambda

+18
source share

I suppose you have lambda_function.py on the Lambda console. You must first create a Lambda feature deployment package, and then use the console to download the package.

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

Go to the Lambda console, choose to upload the zip file to the code input type and upload your deployment package. Import requests should work without errors.

+2
source share

All Articles