AWS SAM Local and docker lambda: keep getting Unable to import module lambda_function: no module named lambda_function,

Edit 2: The main reason was that I had several DOCKER environment variables that called my function calls to redirect to the remote Docker host, and not for SAM Local. As soon as I turned them off, the functions started to work.

Edit: I cloned docker-lambda and tried to run one of my 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 am trying to configure SAM Local using the Python lambda function and continue to give up on the import error of the module in the header.

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 lambda_function.py in the same folder as template.yaml.

I run: sam local start-apiand it starts normally:

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

"" :

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>)

(, init.py)

googled , AWS Lambda; SAM Local.

, - . node.js . 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

?

+4
2

Unable to import module 'lambda_function': No module named 'lambda_function':

, template.yaml, : Handler: index.handler. index.js, handler, exports.handler = () => {}

Python, template.yaml Handler: {file_name}.{function_name}. - lambda.py, def lambda_handler, - .yaml Handler:lambda.lambda_function. , key Resources, , .

. ? sam local api, ? , .

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"
+2

: https://www.youtube.com/watch?v=xaCbIFH_d9k

:

  • : lambda_function.lambda_handler
  • lambda_function?
  • ? , "CodeUri:" - , , . , , . , , template.yml. , CodeUri zip :

    CodeUri: lambda.zip

.

, .

+2

Source: https://habr.com/ru/post/1691766/


All Articles