0 votes
1 view
in AWS by (19.2k points)

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:

The permissions of the zip and the project folder (including subfolders) are set to `chmod 777`. So permissions shouldn't be a problem.

The script itself is within the root folder. When you open the zip file, you directly see it.

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.

1 Answer

0 votes
by (44.6k points)

First, check whether the ZIP file is proper and all the required subfolders, code files are in the right path. If they are not on the right path, then you will get errors.

Also, to use requests module, you can simply import it from botocore.vendored. Sample code:

from botocore.vendored import requests

def lambda_handler(event, context):

   response = requests.get("https://example.com/")

   print response.json()

You can also check out AWS Lambda and enroll in AWS Training to understand the concept in detail. 

Welcome to Intellipaat Community. Get your technical queries answered by top developers !