aletrn commited on
Commit
243f395
·
1 Parent(s): 9e39456

[feat] revert to aws lambda - wip

Browse files
.dockerignore CHANGED
@@ -3,4 +3,5 @@ venv/
3
  __cache__
4
  .idea
5
  tmp/
6
- .env*
 
 
3
  __cache__
4
  .idea
5
  tmp/
6
+ .env*
7
+ __pycache__
Dockerfile CHANGED
@@ -1,38 +1,46 @@
1
- FROM ghcr.io/osgeo/gdal:ubuntu-small-3.7.2
 
2
 
3
- WORKDIR /code
4
- COPY ./requirements.txt /code/requirements.txt
5
- COPY ./requirements_pip.txt /code/requirements_pip.txt
6
 
7
- RUN apt update && apt install -y g++ make cmake unzip libcurl4-openssl-dev python3-pip
 
 
8
 
9
- # avoid segment-geospatial exception caused by missing libGL.so.1 library
10
- RUN apt install -y libgl1 curl
11
- RUN ls -ld /usr/lib/x86_64-linux-gnu/libGL.so* || echo "libGL.so* not found..."
 
 
12
 
13
- RUN which python
14
- RUN python --version
15
- RUN python -m pip install --no-cache-dir --upgrade -r /code/requirements_pip.txt
16
- RUN python -m pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
17
- RUN python -m pip install --no-cache-dir -r /code/requirements.txt
 
18
 
19
- RUN useradd -m -u 1000 user
20
 
21
- USER user
 
 
 
22
 
23
- ENV HOME=/home/user \
24
- PATH=/home/user/.local/bin:$PATH
25
 
26
- WORKDIR $HOME/app
 
27
 
28
- RUN curl -o ${HOME}/sam_vit_h_4b8939.pth https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth
29
- RUN ls -l ${HOME}/
30
- COPY --chown=user . $HOME/app
31
 
32
- RUN echo $HOME/app
33
- RUN echo $HOME/
34
 
35
- RUN ls -l $HOME/app
36
- RUN ls -l $HOME/
 
37
 
38
- CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "7860"]
 
1
+ # inspired by https://dev.to/gaborschulz/running-python-311-on-aws-lambda-1i7p
2
+ FROM ghcr.io/osgeo/gdal:ubuntu-small-3.7.2 as build-image
3
 
4
+ LABEL maintainer="alessandro trinca <alessandro@trinca.tornidor.com>"
 
 
5
 
6
+ # Include global arg in this stage of the build
7
+ ARG LAMBDA_TASK_ROOT="/var/task"
8
+ ARG PYTHONPATH="${LAMBDA_TASK_ROOT}:${PYTHONPATH}:/usr/local/lib/python3/dist-packages"
9
 
10
+ RUN mkdir -p ${LAMBDA_TASK_ROOT}
11
+
12
+ # Install aws-lambda-cpp build dependencies
13
+ RUN apt update && \
14
+ apt install -y g++ make cmake unzip libcurl4-openssl-dev python3-pip
15
 
16
+ # install required packages
17
+ COPY requirements_pip.txt ${LAMBDA_TASK_ROOT}/
18
+ RUN python -m pip install --target ${LAMBDA_TASK_ROOT} --upgrade -r ${LAMBDA_TASK_ROOT}/requirements_pip.txt
19
+ RUN python -m pip install torch torchvision --target ${LAMBDA_TASK_ROOT} --index-url https://download.pytorch.org/whl/cpu
20
+ COPY requirements.txt ${LAMBDA_TASK_ROOT}/
21
+ RUN python -m pip install --target ${LAMBDA_TASK_ROOT} -r ${LAMBDA_TASK_ROOT}/requirements.txt
22
 
23
+ FROM osgeo/gdal:ubuntu-small-3.7.2
24
 
25
+ # Include global arg in this stage of the build
26
+ ARG LAMBDA_TASK_ROOT="/var/task"
27
+ ARG PYTHONPATH="${LAMBDA_TASK_ROOT}:${PYTHONPATH}:/usr/local/lib/python3/dist-packages"
28
+ ARG RIE="https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie"
29
 
30
+ # Set working directory to function root directory
31
+ WORKDIR ${LAMBDA_TASK_ROOT}
32
 
33
+ RUN apt update && apt install -y libgl1 curl
34
+ RUN ls -ld /usr/lib/x86_64-linux-gnu/libGL.so* || echo "libGL.so* not found..."
35
 
36
+ # Copy in the built dependencies
37
+ COPY --from=build-image ${LAMBDA_TASK_ROOT} ${LAMBDA_TASK_ROOT}
 
38
 
39
+ RUN curl -Lo /usr/local/bin/aws-lambda-rie ${RIE}
40
+ RUN chmod +x /usr/local/bin/aws-lambda-rie
41
 
42
+ COPY ./scripts/lambda-entrypoint.sh /lambda-entrypoint.sh
43
+ RUN chmod +x /lambda-entrypoint.sh
44
+ RUN ls -l /lambda-entrypoint.sh
45
 
46
+ ENTRYPOINT ["/lambda-entrypoint.sh"]
dockerfiles/dockerfile-base-webserver CHANGED
@@ -1,22 +1,24 @@
1
- FROM python:3.11
2
 
3
- WORKDIR /code
 
 
 
4
 
5
- RUN which python
6
- RUN python --version
7
- RUN python -m pip install --no-cache-dir fastapi uvicorn loguru
8
 
9
- RUN useradd -m -u 1000 user
 
10
 
11
- USER user
 
 
12
 
13
- ENV HOME=/home/user \
14
- PATH=/home/user/.local/bin:$PATH
15
 
16
- WORKDIR $HOME/app
 
17
 
18
- RUN ls -l ${HOME}/
19
- COPY --chown=user src $HOME/app/src
20
- COPY --chown=user static $HOME/app/static
21
-
22
- CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "7860"]
 
1
+ FROM ghcr.io/osgeo/gdal:ubuntu-small-3.7.2
2
 
3
+ # Include global arg in this stage of the build
4
+ ARG LAMBDA_TASK_ROOT="/var/task"
5
+ ARG PYTHONPATH="${LAMBDA_TASK_ROOT}:${PYTHONPATH}:/usr/local/lib/python3/dist-packages"
6
+ ARG RIE="https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie"
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
13
 
14
+ 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 ./src ${LAMBDA_TASK_ROOT}/src
 
19
 
20
+ RUN apt update && apt install -y python3-pip
21
+ RUN python -m pip install awslambdaric
22
 
23
+ ENTRYPOINT ["/lambda-entrypoint.sh"]
24
+ CMD [ "src.app.lambda_handler" ]
 
 
 
dockerfiles/dockerfile-fastapi-samgeo ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM ghcr.io/osgeo/gdal:ubuntu-small-3.7.2
2
+
3
+ WORKDIR /code
4
+ COPY ./requirements.txt /code/requirements.txt
5
+ COPY ./requirements_pip.txt /code/requirements_pip.txt
6
+
7
+ RUN apt update && apt install -y g++ make cmake unzip libcurl4-openssl-dev python3-pip
8
+
9
+ # avoid segment-geospatial exception caused by missing libGL.so.1 library
10
+ RUN apt install -y libgl1 curl
11
+ RUN ls -ld /usr/lib/x86_64-linux-gnu/libGL.so* || echo "libGL.so* not found..."
12
+
13
+ RUN which python
14
+ RUN python --version
15
+ RUN python -m pip install --no-cache-dir --upgrade -r /code/requirements_pip.txt
16
+ RUN python -m pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
17
+ RUN python -m pip install --no-cache-dir -r /code/requirements.txt
18
+
19
+ RUN useradd -m -u 1000 user
20
+
21
+ USER user
22
+
23
+ ENV HOME=/home/user \
24
+ PATH=/home/user/.local/bin:$PATH
25
+
26
+ WORKDIR $HOME/app
27
+
28
+ RUN curl -o ${HOME}/sam_vit_h_4b8939.pth https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth
29
+ RUN ls -l ${HOME}/
30
+ COPY --chown=user . $HOME/app
31
+
32
+ RUN echo $HOME/app
33
+ RUN echo $HOME/
34
+
35
+ RUN ls -l $HOME/app
36
+ RUN ls -l $HOME/
37
+
38
+ CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "7860"]
dockerfiles/dockerfile-samgeo-api ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM 686901913580.dkr.ecr.eu-west-1.amazonaws.com/lambda-gdal-runner:latest
2
+
3
+ ARG LAMBDA_TASK_ROOT="/var/task"
4
+ ARG PYTHONPATH="${LAMBDA_TASK_ROOT}:${PYTHONPATH}:/usr/local/lib/python3/dist-packages"
5
+
6
+ COPY ./src ${LAMBDA_TASK_ROOT}/src
7
+
8
+ COPY .env ${LAMBDA_TASK_ROOT}
9
+ RUN ls -l ${LAMBDA_TASK_ROOT}/.env
10
+ RUN . ${LAMBDA_TASK_ROOT}/.env
11
+
12
+ RUN ls -l /usr/bin/which
13
+ RUN /usr/bin/which python
14
+ RUN python -v
15
+ RUN echo "PYTHONPATH: ${PYTHONPATH}."
16
+ RUN echo "PATH: ${PATH}."
17
+ RUN echo "LAMBDA_TASK_ROOT: ${LAMBDA_TASK_ROOT}."
18
+ RUN ls -l ${LAMBDA_TASK_ROOT}
19
+ RUN ls -ld ${LAMBDA_TASK_ROOT}
20
+ RUN python -c "import sys; print(sys.path)"
21
+ RUN python -c "import osgeo"
22
+ RUN python -c "import rasterio"
23
+ RUN python -c "import awslambdaric"
24
+ RUN python -m pip list
25
+ RUN python -m pip freeze
26
+ RUN df -h
27
+
28
+ RUN echo "$(date "+%Y%m%d_%H%M%S")" > ${LAMBDA_TASK_ROOT}/buildtime.txt
29
+ RUN python -m pip freeze > ${LAMBDA_TASK_ROOT}/pythonpackages.txt
30
+
31
+ CMD [ "src.surferdtm_prediction_api.app.lambda_handler" ]
requirements.txt CHANGED
@@ -1,6 +1,3 @@
1
- fastapi
2
  bson
3
- loguru
4
  python-dotenv
5
  segment-geospatial
6
- uvicorn[standard]
 
 
1
  bson
 
2
  python-dotenv
3
  segment-geospatial
 
scripts/aws-lambda-rie ADDED
File without changes
scripts/copy_folder_to_host.sh ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+
3
+ echo "options:"
4
+ echo "\$1: container folder we copy from"
5
+ echo "\$2: container folder we copy to (could also be an host folder)"
6
+
7
+ cp -r "$1" "$2"
8
+ echo "copied folder $1 to folder $2!"
9
+ ls -ld "$2"
10
+ ls -l "$2"
11
+
12
+ exit 0
scripts/lambda-entrypoint.sh ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+
3
+ if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
4
+ exec /usr/local/bin/aws-lambda-rie /usr/bin/python -m awslambdaric "$@"
5
+ else
6
+ exec /usr/bin/python -m awslambdaric "$@"
7
+ fi
src/__init__.py CHANGED
@@ -1,4 +0,0 @@
1
- from src.utilities.utilities import setup_logging
2
-
3
-
4
- app_logger = setup_logging(debug=True)
 
 
 
 
 
src/app.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import logging
3
+
4
+
5
+ def lambda_handler(event, context):
6
+ logging.debug(f"event:{event}...")
7
+ logging.debug(f"context:{context}...")
8
+ return {"msg": f"ciao{json.dumps(event)}."}
src/utilities/utilities.py CHANGED
@@ -26,14 +26,12 @@ def setup_logging(debug: bool = False, formatter: str = "{time} - {level} - ({ex
26
  return logger
27
 
28
 
29
- def get_constants(event: dict, root: str = ROOT, dotenv_filename: str = ".env", debug=False) -> dict:
30
  """
31
  Return constants we need to use from event, context and environment variables (both production and test).
32
-
33
  Args:
34
  event: request event
35
- root: path containing the dotenv file
36
- dotenv_filename: dotenv filename
37
  debug: logging debug argument
38
 
39
  Returns:
@@ -41,10 +39,7 @@ def get_constants(event: dict, root: str = ROOT, dotenv_filename: str = ".env",
41
 
42
  """
43
  import json
44
- import os
45
- # from dotenv import dotenv_values
46
 
47
- from src.utilities.constants import SKIP_CONDITIONS_LIST
48
  local_logger = setup_logging(debug)
49
  try:
50
  body = event["body"]
@@ -64,24 +59,10 @@ def get_constants(event: dict, root: str = ROOT, dotenv_filename: str = ".env",
64
  local_logger.debug(f"constants debug:{debug}, log_level:{local_logger.level}, body:{body}.")
65
 
66
  try:
67
- dotenv_file_path = os.path.join(root, dotenv_filename)
68
- local_logger.info(f"root_path:{root}, dotenv file:{dotenv_file_path}.")
69
- # secrets = dotenv_values(dotenv_file_path)
70
-
71
- try:
72
- skip_conditions_list = body["skip_conditions_list"]
73
- local_logger.info(f"found skip_conditions_list, using it: {skip_conditions_list}.")
74
- except KeyError:
75
- skip_conditions_list = SKIP_CONDITIONS_LIST
76
-
77
  return {
78
- "bounding_box": body["bounding_box"],
79
- "zoom": body["zoom"],
80
- "debug": debug,
81
- "slope_cellsize": body["slope_cellsize"],
82
- "model_project_name": body["model_project_name"],
83
- "model_version": body["model_version"],
84
- "skip_conditions_list": skip_conditions_list
85
  }
86
  except KeyError as e_key_constants2:
87
  local_logger.error(f"e_key_constants2:{e_key_constants2}.")
 
26
  return logger
27
 
28
 
29
+ def get_constants(event: dict, debug=False) -> dict:
30
  """
31
  Return constants we need to use from event, context and environment variables (both production and test).
32
+
33
  Args:
34
  event: request event
 
 
35
  debug: logging debug argument
36
 
37
  Returns:
 
39
 
40
  """
41
  import json
 
 
42
 
 
43
  local_logger = setup_logging(debug)
44
  try:
45
  body = event["body"]
 
59
  local_logger.debug(f"constants debug:{debug}, log_level:{local_logger.level}, body:{body}.")
60
 
61
  try:
 
 
 
 
 
 
 
 
 
 
62
  return {
63
+ "bbox": body["bbox"],
64
+ "point": body["point"],
65
+ "debug": debug
 
 
 
 
66
  }
67
  except KeyError as e_key_constants2:
68
  local_logger.error(f"e_key_constants2:{e_key_constants2}.")