Skip to content

[ERROR] Runtime.ImportModuleError: Unable to import module #18

@debojit777

Description

@debojit777

Hello SAM + LAMBDA users,

I am facing below issue in both Local scenario and after deploying app to aws and hitting test endpoint via API Gateway


Error info :

Steps

  • Build & Start:
sam build --use-container
sam local start-api --env-vars test/sample/integration_test/environment.json
  • curl Get :
curl http://127.0.0.1:3000/sample
  • Getting below error
START RequestId: 156a4eec-0ec1-182f-d06e-04bfce652b1f Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'list_handler': No module named 'src'
END RequestId: 156a4eec-0ec1-182f-d06e-04bfce652b1f
REPORT RequestId: 156a4eec-0ec1-182f-d06e-04bfce652b1f  Init Duration: 537.49 ms    Duration: 4.27 ms   Billed Duration: 100 ms Memory Size: 1024 MB    Max Memory Used: 32 MB  
Lambda returned empty body!
Invalid API Gateway Response Keys: {'errorType', 'errorMessage'} in {'errorType': 'Runtime.ImportModuleError', 'errorMessage': "Unable to import module 'list_handler': No module named 'src'"}
<class 'samcli.local.apigw.local_apigw_service.LambdaResponseParseException'>

SAM :

sam --version
SAM CLI, version 0.48.0

OS: MAC


Details :

  • Project structure:
.
├── README.md
├── api-event.json
├── environment.json
├── events
│   └── event.json
├── src
│   └── sample
│       ├── handler
│       │   ├── create_handler.py
│       │   ├── get_handler.py
│       │   ├── list_handler.py
│       │   ├── requirements.txt
│       └── helper
│           ├── dynamodb_helper.py
│           └── logging_helper.py
├── template.yaml
└── test
    └── sample
        ├── integration_test
        │   ├── api_tests.py
        │   └── environment.json
        ├── test_helper.py
        └── unit_test
  • template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sample-sam-app

  SAM Template for sample-sam-app

Globals:
  Function:
    Timeout: 20
    MemorySize: 1024
    Environment:
      Variables:
        TABLE_NAME: !Ref SampleTable
        APPLICATION_NAME: "SAMPLE"

Resources:
  CreateFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: src/sample/handler
      Handler: create_handler.lambda_handler
      Runtime: python3.7
      Policies:
        - DynamoDBCrudPolicy:
            TableName: !Ref SampleTable
      Events:
        Create:
          Type: Api 
          Properties:
            Path: /sample
            Method: post

  GetFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src/sample/handler
      Handler: get_handler.lambda_handler
      Runtime: python3.7
      Policies:
        - DynamoDBReadPolicy:
            TableName: !Ref SampleTable
      Events:
        Get:
          Type: Api 
          Properties:
            Path: /sample/{data_id}
            Method: get

  ListFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: src/sample/handler
      Handler: list_handler.lambda_handler
      Runtime: python3.7
      Policies:
        - DynamoDBReadPolicy:
            TableName: !Ref SampleTable
      Events:
        Get:
          Type: Api 
          Properties:
            Path: /sample
            Method: get

  SampleTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: sample
      AttributeDefinitions:
        - AttributeName: user_id
          AttributeType: S
        - AttributeName: data_id
          AttributeType: S
      KeySchema:
        - AttributeName: user_id
          KeyType: HASH
        - AttributeName: data_id
          KeyType: RANGE
      BillingMode: PAY_PER_REQUEST 

Outputs:
    CreateFunction:
      Description: "Create Lambda Function ARN"
      Value: !GetAtt CreateFunction.Arn

    GetFunction:
      Description: "Get Lambda Function ARN"
      Value: !GetAtt GetFunction.Arn

    ListFunction:
      Description: "List Lambda Function ARN"
      Value: !GetAtt ListFunction.Arn
  • docker doesnt show sam-app :
docker ps

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                    NAMES
2e45a561d2ab        amazon/dynamodb-local   "java -jar DynamoDBL…"   19 minutes ago      Up 19 minutes       0.0.0.0:8000->8000/tcp   romantic_mahavira

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions