5

Edit 2: The root cause was I had several DOCKER environment variables set which were causing my function invocations to re-route to a remote Docker host and not hit SAM Local. Once I unset those, the functions started running.

Edit: I cloned docker-lambda and tried running one of their examples and get the same error.

docker run --rm -v "$PWD":/var/task lambci/lambda:python3.6
START RequestId: 73a433fc-1d8a-4cdb-a66d-61bd667e13ba Version: $LATEST
Unable to import module 'lambda_function': No module named 'lambda_function'
END RequestId: 73a433fc-1d8a-4cdb-a66d-61bd667e13ba
REPORT RequestId: 73a433fc-1d8a-4cdb-a66d-61bd667e13ba Duration: 1 ms Billed Duration: 100 ms Memory Size: 1536 MB Max Memory Used: 15 MB

{"errorMessage": "Unable to import module 'lambda_function'"}

I'm trying to set up SAM Local with a Python lambda function and keep getting frustrated by the module import error in the title.

My template.yaml looks like this:

AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  ProposalsTable:
    Type: "AWS::Serverless::SimpleTable"
  AddProposal:
    Type: "AWS::Serverless::Function"
    Properties:
      Handler: lambda_function.lambda_handler
      Runtime: python3.6
      Policies: AmazonDynamoDBFullAccess
      Environment:
        Variables:
          TABLE_NAME: !Ref ProposalsTable
      Events:
        Vote:
          Type: Api
          Properties:
            Path: /proposals
            Method: get

I have a lambda_function.py in the same folder as the template.yaml.

I run: sam local start-api and it starts up fine:

Mounting lambda_function.lambda_handler (python3.6) at http://127.0.0.1:3000/proposals [GET]

Then I do:

curl http://127.0.0.1:3000/proposals

Then on the "server" it shows:

Unable to import module 'lambda_function': No module named 'lambda_function'
Function returned an invalid response (must include one of: body, headers or statusCode in the response object): %!s(<nil>)

I've tried all different ways of naming the file (e.g. putting it inside a folder with an init.py)

I googled and read a dozen or more threads but most people are talking about deploying to the real AWS Lambda; there's not a lot on SAM Local.

I wonder if it's something in my environment. The node.js sample function here fails with a timeout. https://github.com/awslabs/aws-sam-local/tree/develop/samples/hello-world/node

2018/01/04 15:20:41 Invoking index.handler (nodejs6.10)
2018/01/04 15:20:41 Mounting /Users/me/code/sam-local-prototype as /var/task:ro inside runtime container
2018/01/04 15:20:46 Function index.handler timed out after 3 seconds

Ideas?

5
  • I think you are getting a timeout from the example code because the event.json in that repo appears to be invalid (at least from my experience with SAM). I wrote this last week, and I use it today. It works, and is written in python. I use sam local invoke "S3" --event event.json to initialize the docker env, and run it another window again to trigger the event. The event.json in my case triggers a real event on the bucket I have configured. Commented Jan 12, 2018 at 14:56
  • Thanks for the reply. I'm not sure that it's even getting to the point of ingesting an event.json. It doesn't seem to be executing the function at all. Commented Jan 13, 2018 at 0:31
  • You might be right, but I believe that IP is important. If I am connected to my work VPN and run the same event it times out. You will also see in the template.yaml file in my repo I renamed the Handler: lambda_function.lambda_handler to Handler: lambda.lambda_handler to match the name of the file it will be running. sam local invoke "S3" also matches the value of the Resources Key. Commented Jan 13, 2018 at 0:44
  • Did you ever get this sorted out? Commented Jan 15, 2018 at 13:43
  • No, sir, I have not. Commented Jan 16, 2018 at 18:02

2 Answers 2

2
+100

Regarding the Unable to import module 'lambda_function': No module named 'lambda_function' error:

In the repo link you provided within the template.yaml the Handler key reads: Handler: index.handler. This corresponds to the index.js file that contains the function named handler written as exports.handler = () => {}

If you are re-writing this in Python, your template.yaml handler key will need to read Handler: {file_name}.{function_name}. If the file carrying your lambda function is called lambda.py and the function within it is def lambda_handler you will need to write your handler key in the .yaml as Handler:lambda.lambda_function. I remember having to modify the key beneath Resources but give make sure your file name and function name are accurate and try again.

I just copied the code out of the repo and ran it successfully. Do you have docker installed correctly? Or did you have sam local api started when you tried to run the test code from the repo? If it attempted to connect on the same port, you may get the timeout as well.

2018/01/16 13:39:14 Successfully parsed template.yaml
2018/01/16 13:39:14 Connected to Docker 1.35
2018/01/16 13:39:14 Runtime image missing, will pull....
2018/01/16 13:39:14 Fetching lambci/lambda:nodejs6.10 image for nodejs6.10 runtime...
nodejs6.10: Pulling from lambci/lambda
f338a32fa56c: Already exists 
4926b20b634f: Already exists 
8040e979acbc: Pull complete 
160b6838355f: Pull complete 
Digest: sha256:e34f92bc0df0cf4a8ba560c6c7bf201183d5f6773ddf44440a97295486906744
Status: Downloaded newer image for lambci/lambda:nodejs6.10
2018/01/16 13:39:28 Invoking index.handler (nodejs6.10)
2018/01/16 13:39:28 Mounting /Users/me/sam_local_test as /var/task:ro inside runtime container
START RequestId: 8970d865-2d59-1d54-0825-d56f1fd035f7 Version: $LATEST
2018-01-16T20:39:32.315Z    8970d865-2d59-1d54-0825-d56f1fd035f7    LOG: Name is Bob
END RequestId: 8970d865-2d59-1d54-0825-d56f1fd035f7
REPORT RequestId: 8970d865-2d59-1d54-0825-d56f1fd035f7  Duration: 22.02 ms  Billed Duration: 0 ms   Memory Size: 0 MB   Max Memory Used: 28 MB  

"Hello Bob"
Sign up to request clarification or add additional context in comments.

7 Comments

I wonder if it's a Docker issue. I do have some Docker stuff running in the background that I suppose could be interfering. Not 100% sure you mean by "it attempted to connect on the same port"
The sam local invoke "HelloWorld" -e event.json command is going to spin up a docker container and communicate with it. I am curious if that communication is done on the same port as sam local api.
Yeah, my dev environment has a custom export DOCKER_HOST and DOCKER_CERT_PATH. I wonder if it's interfering. You may be on to something. Let me get back to you.
I tried unsetting my DOCKER env variables and I'm getting the same errors. :-(
Sorry, I replied to the wrong area. Here: "Holy crap it worked. I did unset in my terminal for DOCKER_CERT_PATH and DOCKER_HOST and DOCKER_TLS_VERIFY... it's because I was re-routing Docker. I'll give you bounty" I needed to make sure I did the unset commands in each Terminal window too.
|
2

Aaron,

Check out this video : https://www.youtube.com/watch?v=xaCbIFH_d9k

You have listed:

  • Handler: lambda_function.lambda_handler
  • Is your file called lambda_function?
  • Where is this file located? This matters because the element "CodeUri:" is where you specify the path to the file and I don't see that element in your explanation. Or if you zip up the project you can specify the file name here. For example I zip my project up then within the template.yml I specify my CodeUri to point to the zip as such:

    CodeUri: lambda.zip

.

I hope this helps.

9 Comments

Yes I named the file lambda_function.py. CodeUri doesn't apply because this isn't being deployed to lambda yet -- this is SAM Local. I tried it anyway but it made no difference. Thanks anyway.
Can you share your function?
I'm just doing a modified version of the one here: aws.amazon.com/blogs/aws/…
I wonder if it's something in my environment. The node.js sample function here fails with a timeout. github.com/awslabs/aws-sam-local/tree/develop/samples/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.