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?
event.jsonin 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 usesam local invoke "S3" --event event.jsonto initialize the docker env, and run it another window again to trigger the event. Theevent.jsonin my case triggers a real event on the bucket I have configured.template.yamlfile in my repo I renamed theHandler: lambda_function.lambda_handlertoHandler: lambda.lambda_handlerto match the name of the file it will be running.sam local invoke "S3"also matches the value of theResourcesKey.