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

[ERROR] Runtime.ImportModuleError: Unable to import module

Đang mở
#18 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Java
Star
437
Fork
151
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu với template.yaml và điểm vào src/sample/handler/list_handler.py, sau đó chạy các lệnh sam build --use-container và sam local start-api đã được báo cáo. So sánh CodeUri và Handler với cấu trúc dự án và xác minh rằng yêu cầu /sample được import thành công cả cục bộ lẫn sau khi triển khai.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
aws, docker, python
Lĩnh vực
backend, cloud, devops
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.