Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use Requests-Module on AWS Lambda

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.

like image 886
codepleb Avatar asked Nov 22 '16 11:11

codepleb


People also ask

Is requests available in AWS Lambda?

Update (April 26, 2022):The version of the AWS SDK included in the AWS Lambda runtimes for Python 2.7, Python 3.6 and Python 3.7 will continue to include the 'requests' module in Botocore.

How do I resolve the unable to import module error that I receive when I run Lambda code in Python?

To resolve this error, create a deployment package or Lambda layer that includes the libraries that you want to use in your Python code for Lambda. Important: Make sure that you put the library that you import for Python inside the /python folder.

Are Python requests deprecated?

Overview. As we enter our third year of Python 2.7 reaching end-of-life, Requests has decided it's time to start deprecating our support. While we have yet to confirm a date, we want to provide early notice that this is coming at some point in 2022.


1 Answers

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/

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.

like image 60
Sining Liu Avatar answered Oct 07 '22 15:10

Sining Liu