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.

share|improve this question
1  
In your question, the position of the -t option in the pip install command is incorrect - it must be pip install requests -t PATH_TO_ROOT_FOLDER . Did you just mistype it, or this is how you really ran it? – Leon Nov 22 '16 at 12:09
    
@Leon: Right. Just checked the history and I did it like you mention it here. :) So that didn't cause the problem. – TrudleR Nov 22 '16 at 12:10
    
What version of Python do you use locally? – Leon Nov 22 '16 at 12:14
    
@Leon: 2.7.12 (15chars) – TrudleR Nov 22 '16 at 12:19
    
Have you seen the guide at docs.aws.amazon.com/lambda/latest/dg/… ? – Klaus D. Nov 22 '16 at 12:19
up vote 3 down vote accepted

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

It's a bit depressing if you find such easy errors after hours of try and failure.

share|improve this answer
    
Does your script return json then? I'm trying to do similar, but keep getting 'not json serialisable'. I don't have 'import json' however... code works fine locally without 'import json' so not sure why it doesn't in Lambda. – kafka Jan 20 at 12:38
    
@kafka You may need to import the json library. Not sure though, but AWS uses python 2.7 and not 3.x. My script doesn't return json. – TrudleR Jan 20 at 12:41
    
yeah python 2.7 here. Going to attempt with explicit reference to json library. Currently re-packaging. – kafka Jan 20 at 12:42
    
OK no joy here, in your code you simply return 'hello lambda_helper', I want to return the entire JSON response from the API Get though.... no worries if you don't know I might do a new question – kafka Jan 20 at 12:46
    
I return status codes. But yeah, I'm not much into python. It was barely enough to write that script. – TrudleR Jan 20 at 12:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.