[fix] fix request, prepare run on aws lambda
Browse files- dockerfiles/dockerfile-base-webserver +5 -6
- requirements_dev.txt +2 -0
- src/app.py +50 -11
- src/utilities/type_hints.py +1 -1
dockerfiles/dockerfile-base-webserver
CHANGED
@@ -7,6 +7,11 @@ ARG RIE="https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/l
|
|
7 |
|
8 |
# Set working directory to function root directory
|
9 |
WORKDIR ${LAMBDA_TASK_ROOT}
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
RUN curl -Lo /usr/local/bin/aws-lambda-rie ${RIE}
|
12 |
RUN chmod +x /usr/local/bin/aws-lambda-rie
|
@@ -15,11 +20,5 @@ COPY ./scripts/lambda-entrypoint.sh /lambda-entrypoint.sh
|
|
15 |
RUN chmod +x /lambda-entrypoint.sh
|
16 |
RUN ls -l /lambda-entrypoint.sh
|
17 |
|
18 |
-
COPY requirements_dev.txt ${LAMBDA_TASK_ROOT}/requirements_dev.txt
|
19 |
-
COPY ./src ${LAMBDA_TASK_ROOT}/src
|
20 |
-
|
21 |
-
RUN apt update && apt install -y python3-pip
|
22 |
-
RUN python -m pip install -r requirements_dev.txt
|
23 |
-
|
24 |
ENTRYPOINT ["/lambda-entrypoint.sh"]
|
25 |
CMD [ "src.app.lambda_handler" ]
|
|
|
7 |
|
8 |
# Set working directory to function root directory
|
9 |
WORKDIR ${LAMBDA_TASK_ROOT}
|
10 |
+
COPY requirements_dev.txt ${LAMBDA_TASK_ROOT}/requirements_dev.txt
|
11 |
+
COPY ./src ${LAMBDA_TASK_ROOT}/src
|
12 |
+
|
13 |
+
RUN apt update && apt install -y curl python3-pip
|
14 |
+
RUN python -m pip install -r ${LAMBDA_TASK_ROOT}/requirements_dev.txt --target ${LAMBDA_TASK_ROOT}
|
15 |
|
16 |
RUN curl -Lo /usr/local/bin/aws-lambda-rie ${RIE}
|
17 |
RUN chmod +x /usr/local/bin/aws-lambda-rie
|
|
|
20 |
RUN chmod +x /lambda-entrypoint.sh
|
21 |
RUN ls -l /lambda-entrypoint.sh
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
ENTRYPOINT ["/lambda-entrypoint.sh"]
|
24 |
CMD [ "src.app.lambda_handler" ]
|
requirements_dev.txt
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
awslambdaric
|
2 |
aws_lambda_powertools
|
|
|
|
|
3 |
jmespath
|
4 |
|
|
|
1 |
awslambdaric
|
2 |
aws_lambda_powertools
|
3 |
+
fastjsonschema
|
4 |
+
pydantic
|
5 |
jmespath
|
6 |
|
src/app.py
CHANGED
@@ -1,22 +1,61 @@
|
|
1 |
import json
|
2 |
-
|
3 |
from aws_lambda_powertools import Logger
|
4 |
from aws_lambda_powertools.utilities.typing import LambdaContext
|
|
|
|
|
|
|
|
|
5 |
|
6 |
logger = Logger()
|
7 |
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def lambda_handler(event: dict, context: LambdaContext) -> dict[str]:
|
|
|
|
|
11 |
try:
|
12 |
-
logger.
|
13 |
-
logger.debug("
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
except Exception as e:
|
21 |
logger.error(f"exception:{e}.")
|
22 |
-
|
|
|
|
|
|
|
|
1 |
import json
|
2 |
+
import time
|
3 |
from aws_lambda_powertools import Logger
|
4 |
from aws_lambda_powertools.utilities.typing import LambdaContext
|
5 |
+
from pydantic import BaseModel, ValidationError
|
6 |
+
|
7 |
+
from src.utilities.type_hints import input_floatlist, input_floatlist2
|
8 |
+
|
9 |
|
10 |
logger = Logger()
|
11 |
|
12 |
|
13 |
+
class BBoxWithPointInput(BaseModel):
|
14 |
+
bbox: input_floatlist
|
15 |
+
points: input_floatlist2
|
16 |
+
|
17 |
+
|
18 |
+
def get_response(status: int, start_time: float, output: BaseModel = None) -> dict[str]:
|
19 |
+
"""
|
20 |
+
Return a response for frontend clients.
|
21 |
+
|
22 |
+
Args:
|
23 |
+
status: status response
|
24 |
+
start_time: request start time (float)
|
25 |
+
output: dict we embed into our response
|
26 |
+
|
27 |
+
Returns:
|
28 |
+
dict: response
|
29 |
+
|
30 |
+
"""
|
31 |
+
messages = {200: "ok", 422: "validation error", 500: "internal server error"}
|
32 |
+
body = {"duration_run": time.time() - start_time, "message": messages[status]}
|
33 |
+
if status == 200:
|
34 |
+
body = {"output": output.model_dump_json(), **body}
|
35 |
+
return {
|
36 |
+
"statusCode": status,
|
37 |
+
"headers": {'Content-type': 'application/json', 'Accept': 'application/json'},
|
38 |
+
"body": json.dumps(body)
|
39 |
+
}
|
40 |
+
|
41 |
+
|
42 |
def lambda_handler(event: dict, context: LambdaContext) -> dict[str]:
|
43 |
+
logger.info(f"start with aws_request_id:{context.aws_request_id}.")
|
44 |
+
start_time = time.time()
|
45 |
try:
|
46 |
+
logger.debug(f"event:{json.dumps(event)}...")
|
47 |
+
logger.debug(f"context:{context}...")
|
48 |
+
|
49 |
+
try:
|
50 |
+
bbox_points = BBoxWithPointInput(bbox=event["bbox"], points=event["points"])
|
51 |
+
logger.info(f"validation ok, bbox_points:{bbox_points}...")
|
52 |
+
response = get_response(200, start_time, bbox_points)
|
53 |
+
except ValidationError as ve:
|
54 |
+
logger.error(f"validation error:{ve}.")
|
55 |
+
response = get_response(422, start_time)
|
56 |
except Exception as e:
|
57 |
logger.error(f"exception:{e}.")
|
58 |
+
response = get_response(500, start_time)
|
59 |
+
|
60 |
+
logger.info(f"response:{response}...")
|
61 |
+
return response
|
src/utilities/type_hints.py
CHANGED
@@ -3,4 +3,4 @@ from typing import List
|
|
3 |
|
4 |
input_floatlist = List[float]
|
5 |
input_floatlist2 = List[input_floatlist]
|
6 |
-
|
|
|
3 |
|
4 |
input_floatlist = List[float]
|
5 |
input_floatlist2 = List[input_floatlist]
|
6 |
+
ts_dict_str2 = dict[str, str]
|