aws-samples / aws-samples/aws-sam-java-rest

[ERROR] Runtime.ImportModuleError: Unable to import module

Open
#18 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
437
Forks
151
PR merge metrics
No merged PRs in 30d

Description

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:

```bash
sam build --use-container
sam local start-api --env-vars test/sample/integration_test/environment.json
```
* curl Get :

```bash
curl http://127.0.0.1:3000/sample
```
* Getting below error

```bash
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'"}

```
------------------------------------------------------------------------------------------

### SAM :
```bash
sam --version
SAM CLI, version 0.48.0
```
------------------------------------------------------------------------------------------

### OS: MAC

------------------------------------------------------------------------------------------

### Details :

* Project structure:

```bash
.
├── 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``

```bash
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 :

```bash
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
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.