Cannot use Requests-Module on AWS Lambda

codepleb picture codepleb · Nov 22, 2016 · Viewed 74k times · Source

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.

Answer

Sining  Liu picture Sining Liu · Jan 29, 2018

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.