aletrn commited on
Commit
c76cb5f
1 Parent(s): c8c2cad

[refactor] use poetry also to build the aws docker images

Browse files
Pipfile DELETED
@@ -1,31 +0,0 @@
1
- [[source]]
2
- url = "https://pypi.org/simple"
3
- verify_ssl = true
4
- name = "pypi"
5
-
6
- [packages]
7
- sphinx = "*"
8
- sphinx-autodoc-typehints = "*"
9
- myst-parser = "*"
10
-
11
- [dev-packages]
12
- aws-lambda-powertools = "*"
13
- awslambdaric = "*"
14
- bson = "*"
15
- geopandas = "*"
16
- jmespath = "*"
17
- matplotlib = "*"
18
- numpy = "*"
19
- onnxruntime = "*"
20
- opencv-python-headless = "*"
21
- pillow = "*"
22
- pydantic = ">=2.0.3"
23
- python-dotenv = "*"
24
- rasterio = "*"
25
- requests = "*"
26
- pytest = "*"
27
- pytest-cov = "*"
28
-
29
- [requires]
30
- python_version = "3.11"
31
- python_full_version = "3.11.6"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Pipfile.lock DELETED
The diff for this file is too large to render. See raw diff
 
dockerfiles/dockerfile-lambda-fastsam-api CHANGED
@@ -3,10 +3,13 @@ FROM 686901913580.dkr.ecr.eu-west-1.amazonaws.com/lambda-gdal-runner:latest
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
 
7
  # Set working directory to function root directory
8
  WORKDIR ${LAMBDA_TASK_ROOT}
9
  COPY ./samgis ${LAMBDA_TASK_ROOT}/samgis
 
10
  COPY ./machine_learning_models ${LAMBDA_TASK_ROOT}/machine_learning_models
11
 
12
  RUN ls -l /usr/bin/which
@@ -19,16 +22,15 @@ RUN ls -l ${LAMBDA_TASK_ROOT}
19
  RUN ls -ld ${LAMBDA_TASK_ROOT}
20
  RUN ls -l ${LAMBDA_TASK_ROOT}/machine_learning_models
21
  RUN python -c "import sys; print(sys.path)"
22
- RUN python -c "import cv2"
23
  RUN python -c "import geopandas"
24
  RUN python -c "import onnxruntime"
25
  RUN python -c "import rasterio"
26
  RUN python -c "import awslambdaric"
27
- RUN python -m pip list
28
- RUN python -m pip freeze
29
  RUN df -h
30
  RUN ls -l /lambda-entrypoint.sh
31
  RUN ls -l ${LAMBDA_TASK_ROOT}/samgis/
 
32
 
33
  # ENTRYPOINT ["/lambda-entrypoint.sh"]
34
- CMD [ "samgis.io.lambda_wrapper.lambda_handler" ]
 
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
+ ENV VIRTUAL_ENV=${LAMBDA_TASK_ROOT}/.venv \
7
+ PATH="${LAMBDA_TASK_ROOT}/.venv/bin:$PATH"
8
 
9
  # Set working directory to function root directory
10
  WORKDIR ${LAMBDA_TASK_ROOT}
11
  COPY ./samgis ${LAMBDA_TASK_ROOT}/samgis
12
+ COPY ./src ${LAMBDA_TASK_ROOT}/src
13
  COPY ./machine_learning_models ${LAMBDA_TASK_ROOT}/machine_learning_models
14
 
15
  RUN ls -l /usr/bin/which
 
22
  RUN ls -ld ${LAMBDA_TASK_ROOT}
23
  RUN ls -l ${LAMBDA_TASK_ROOT}/machine_learning_models
24
  RUN python -c "import sys; print(sys.path)"
 
25
  RUN python -c "import geopandas"
26
  RUN python -c "import onnxruntime"
27
  RUN python -c "import rasterio"
28
  RUN python -c "import awslambdaric"
29
+ RUN python -c "import cv2"
 
30
  RUN df -h
31
  RUN ls -l /lambda-entrypoint.sh
32
  RUN ls -l ${LAMBDA_TASK_ROOT}/samgis/
33
+ RUN ls -l ${LAMBDA_TASK_ROOT}/src/
34
 
35
  # ENTRYPOINT ["/lambda-entrypoint.sh"]
36
+ CMD [ "src.lambda_wrapper.lambda_handler" ]
dockerfiles/dockerfile-lambda-gdal-runner CHANGED
@@ -1,28 +1,76 @@
1
- FROM python:3.11-slim-bookworm
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
- RUN echo "ENV RIE: $RIE ..."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # Set working directory to function root directory
11
  WORKDIR ${LAMBDA_TASK_ROOT}
12
- COPY requirements_pip.txt ${LAMBDA_TASK_ROOT}/requirements_pip.txt
13
- COPY requirements_dockerfile.txt ${LAMBDA_TASK_ROOT}/requirements_dockerfile.txt
14
 
15
  # avoid segment-geospatial exception caused by missing libGL.so.1 library
16
  RUN apt update && apt install -y libgl1 curl python3-pip && apt clean
17
- RUN ls -ld /usr/lib/*linux-gnu/libGL.so* || echo "libGL.so* not found..."
 
 
18
  RUN which python
19
  RUN python --version
20
- RUN python -m pip install -r ${LAMBDA_TASK_ROOT}/requirements_pip.txt --upgrade --target ${LAMBDA_TASK_ROOT}
21
- RUN python -m pip install -r ${LAMBDA_TASK_ROOT}/requirements_dockerfile.txt --target ${LAMBDA_TASK_ROOT}
22
- RUN python -c "import sys;print(sys.path)"
23
- # RUN python -m pip install pillow awslambdaric aws-lambda-powertools httpx jmespath --target ${LAMBDA_TASK_ROOT}
 
 
 
 
24
 
25
  RUN curl -Lo /usr/local/bin/aws-lambda-rie ${RIE}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  RUN chmod +x /usr/local/bin/aws-lambda-rie
27
 
28
  COPY ./scripts/lambda-entrypoint.sh /lambda-entrypoint.sh
 
 
 
1
  # Include global arg in this stage of the build
2
+ ARG ARCH="x86_64"
3
  ARG LAMBDA_TASK_ROOT="/var/task"
4
  ARG PYTHONPATH="${LAMBDA_TASK_ROOT}:${PYTHONPATH}:/usr/local/lib/python3/dist-packages"
5
+ ARG POETRY_NO_INTERACTION=1
6
+ ARG POETRY_VIRTUALENVS_IN_PROJECT=1
7
+ ARG POETRY_VIRTUALENVS_CREATE=1
8
+ ARG POETRY_CACHE_DIR=/tmp/poetry_cache
9
  ARG RIE="https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie"
10
 
11
+
12
+ FROM python:3.11-bookworm as builder
13
+
14
+ ARG ARCH
15
+ ARG LAMBDA_TASK_ROOT
16
+ ARG PYTHONPATH
17
+ ARG POETRY_NO_INTERACTION
18
+ ARG POETRY_VIRTUALENVS_IN_PROJECT
19
+ ARG POETRY_VIRTUALENVS_CREATE
20
+ ARG POETRY_CACHE_DIR
21
+ ARG RIE
22
+
23
+ RUN echo "ARCH: $ARCH ..."
24
+
25
+ RUN echo "ARG RIE: $RIE ..."
26
+ RUN echo "ARG POETRY_CACHE_DIR: ${POETRY_CACHE_DIR} ..."
27
+ RUN echo "ARG PYTHONPATH: $PYTHONPATH ..."
28
 
29
  # Set working directory to function root directory
30
  WORKDIR ${LAMBDA_TASK_ROOT}
31
+ COPY requirements_poetry.txt pyproject.toml poetry.lock README.md ${LAMBDA_TASK_ROOT}/
 
32
 
33
  # avoid segment-geospatial exception caused by missing libGL.so.1 library
34
  RUN apt update && apt install -y libgl1 curl python3-pip && apt clean
35
+ RUN echo "check for libGL.so ..."
36
+ RUN ls -ld /usr/lib/*-linux-gnu/libGL.so* || echo "libGL.so* not found/1..."
37
+ RUN ls -ld /usr/lib/${ARCH}-linux-gnu/libGL.so* || echo "libGL.so* not found/2..."
38
  RUN which python
39
  RUN python --version
40
+
41
+ # poetry installation path is NOT within ${LAMBDA_TASK_ROOT}: not needed for runtime docker image
42
+ RUN python -m pip install -r ${LAMBDA_TASK_ROOT}/requirements_poetry.txt
43
+
44
+ RUN which poetry && poetry --version && poetry config --list
45
+ RUN poetry config virtualenvs.path ${LAMBDA_TASK_ROOT}
46
+ RUN echo "# poetry config --list #" && poetry config --list
47
+ RUN poetry install --with aws_lambda --no-root
48
 
49
  RUN curl -Lo /usr/local/bin/aws-lambda-rie ${RIE}
50
+
51
+ RUN echo "check for libGL.so"
52
+ RUN ls -ld /usr/lib/*-linux-gnu/libGL.so* || echo "libGL.so* not found..."
53
+ RUN ls -ld ${LAMBDA_TASK_ROOT}/.venv
54
+ RUN ls -l ${LAMBDA_TASK_ROOT}/.venv
55
+
56
+
57
+ FROM python:3.11-slim-bookworm as runtime
58
+
59
+ ARG ARCH
60
+ ARG LAMBDA_TASK_ROOT
61
+
62
+ ENV VIRTUAL_ENV=${LAMBDA_TASK_ROOT}/.venv \
63
+ PATH="${LAMBDA_TASK_ROOT}/.venv/bin:$PATH"
64
+
65
+ COPY --from=builder /usr/local/bin/aws-lambda-rie /usr/local/bin/aws-lambda-rie
66
+ RUN echo "COPY --from=builder /usr/lib/${ARCH}-linux-gnu/libGL.so* /usr/lib/${ARCH}-linux-gnu/"
67
+ COPY --from=builder /usr/lib/${ARCH}-linux-gnu/libGL.so* /usr/lib/${ARCH}-linux-gnu/
68
+ COPY --from=builder ${LAMBDA_TASK_ROOT}/.venv ${LAMBDA_TASK_ROOT}/.venv
69
+
70
+ RUN echo "new LAMBDA_TASK_ROOT after hidden venv copy => ${LAMBDA_TASK_ROOT}"
71
+ RUN ls -ld ${LAMBDA_TASK_ROOT}/
72
+ RUN ls -lA ${LAMBDA_TASK_ROOT}/
73
+
74
  RUN chmod +x /usr/local/bin/aws-lambda-rie
75
 
76
  COPY ./scripts/lambda-entrypoint.sh /lambda-entrypoint.sh
poetry.lock ADDED
The diff for this file is too large to render. See raw diff
 
pyproject.toml CHANGED
@@ -1,30 +1,50 @@
1
- [build-system]
2
- requires = ["hatchling", "hatch-requirements-txt"]
3
- build-backend = "hatchling.build"
4
-
5
- [project]
6
  name = "samgis"
7
  version = "1.0.0"
8
- authors = [
9
- { name="Alessandro Trinca Tornidor", email="alessandro@trinca.tornidor.com" },
10
- ]
11
  description = "A backend for machine learning instance segmentation on geospatial data even without dedicated graphics cards."
 
 
12
  readme = "README.md"
13
- requires-python = ">=3.11"
14
- classifiers = [
15
- "Programming Language :: Python :: 3",
16
- "License :: OSI Approved :: MIT License",
17
- "Operating System :: OS Independent",
18
- ]
19
- dynamic = ["dependencies", "optional-dependencies"]
20
-
21
- [project.urls]
22
- Homepage = "https://github.com/trincadev/samgis-be"
23
- Issues = "https://github.com/trincadev/samgis-be/issues"
24
-
25
- [tool.hatch.metadata.hooks.requirements_txt]
26
- files = ["requirements_dockerfile.txt"]
27
-
28
- [tool.hatch.metadata.hooks.requirements_txt.optional-dependencies]
29
- dev = ["requirements_dev.txt"]
30
- docs = ["requirements.txt"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [tool.poetry]
 
 
 
 
2
  name = "samgis"
3
  version = "1.0.0"
 
 
 
4
  description = "A backend for machine learning instance segmentation on geospatial data even without dedicated graphics cards."
5
+ authors = ["alessandro trinca tornidor <alessandro@trinca.tornidor.com>"]
6
+ license = "MIT license"
7
  readme = "README.md"
8
+
9
+ [tool.poetry.dependencies]
10
+ bson = "^0.5.10"
11
+ contextily = "^1.4.0"
12
+ geopandas = "^0.14.1"
13
+ numpy = "^1.26.2"
14
+ onnxruntime = "^1.16.3"
15
+ opencv-python-headless = "^4.8.1.78"
16
+ python = "^3.11"
17
+ python-dotenv = "^1.0.0"
18
+ rasterio = "^1.3.9"
19
+ requests = "^2.31.0"
20
+ pillow = "^10.1.0"
21
+
22
+ [tool.poetry.group.aws_lambda]
23
+ optional = true
24
+
25
+ [tool.poetry.group.aws_lambda.dependencies]
26
+ aws-lambda-powertools = "^2.30.2"
27
+ awslambdaric = "^2.0.8"
28
+ jmespath = "^1.0.1"
29
+ pydantic = ">=2.0.3"
30
+
31
+ [tool.poetry.group.test]
32
+ optional = true
33
+
34
+ [tool.poetry.group.test.dependencies]
35
+ pytest = "^7.4.3"
36
+ pytest-cov = "^4.1.0"
37
+ python-dotenv = "^1.0.0"
38
+
39
+ [tool.poetry.group.docs]
40
+ optional = true
41
+
42
+ [tool.poetry.group.docs.dependencies]
43
+ sphinx = "^7.2.6"
44
+ sphinx-autodoc-typehints = "^1.25.2"
45
+ sphinxcontrib-openapi = "^0.8.3"
46
+ myst-parser = "^2.0.0"
47
+
48
+ [build-system]
49
+ requires = ["poetry-core"]
50
+ build-backend = "poetry.core.masonry.api"
requirements_pip.txt DELETED
@@ -1,3 +0,0 @@
1
- pip
2
- wheel
3
- setuptools
 
 
 
 
requirements_poetry.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ poetry==1.7.1
samgis/__version__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ __version__ = "1.0.0"
scripts/lambda-entrypoint.sh CHANGED
@@ -1,7 +1,17 @@
1
  #!/bin/sh
2
 
 
 
 
 
 
 
 
 
 
 
3
  if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
4
- exec /usr/local/bin/aws-lambda-rie /usr/local/bin/python -m awslambdaric "$@"
5
  else
6
- exec /usr/local/bin/python -m awslambdaric "$@"
7
  fi
 
1
  #!/bin/sh
2
 
3
+ # default value for PYTHONBIN=/usr/local/bin/python
4
+ # but it change because of poetry that it uses a virtualenv installed within the LAMBDA_TASK_ROOT=/var/task
5
+
6
+ echo "python installation path and version:"
7
+ which python
8
+ python --version
9
+
10
+ echo "lambda-entrypoint.sh: PYTHONPATH ${PYTHONPATH} ..."
11
+ echo "lambda-entrypoint.sh: PATH ${PATH} ..."
12
+
13
  if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
14
+ exec /usr/local/bin/aws-lambda-rie python -m awslambdaric "$@"
15
  else
16
+ exec python -m awslambdaric "$@"
17
  fi
src/__init__.py ADDED
File without changes
{samgis/io → src}/lambda_wrapper.py RENAMED
File without changes
tests/test_app.py CHANGED
@@ -5,7 +5,8 @@ from unittest.mock import patch
5
 
6
  from awslambdaric.lambda_context import LambdaContext
7
 
8
- from samgis.io import lambda_helpers, lambda_wrapper
 
9
  from tests.local_tiles_http_server import LocalTilesHttpServer
10
 
11
 
@@ -21,7 +22,7 @@ class TestAppFailures(unittest.TestCase):
21
  samexporter_predict_mocked,
22
  time_mocked
23
  ):
24
- from samgis.io.lambda_wrapper import lambda_handler
25
 
26
  time_mocked.return_value = 0
27
  get_parsed_request_body_mocked.value = {}
@@ -44,7 +45,7 @@ class TestAppFailures(unittest.TestCase):
44
  @patch.object(time, "time")
45
  @patch.object(lambda_wrapper, "get_parsed_request_body")
46
  def test_lambda_handler_400(self, get_parsed_request_body_mocked, time_mocked):
47
- from samgis.io.lambda_wrapper import lambda_handler
48
 
49
  time_mocked.return_value = 0
50
  get_parsed_request_body_mocked.return_value = {}
@@ -64,7 +65,7 @@ class TestAppFailures(unittest.TestCase):
64
 
65
  @patch.object(time, "time")
66
  def test_lambda_handler_422(self, time_mocked):
67
- from samgis.io.lambda_wrapper import lambda_handler
68
 
69
  time_mocked.return_value = 0
70
  event = {"body": {}, "version": 1.0}
@@ -95,7 +96,7 @@ class TestAppFailures(unittest.TestCase):
95
  samexporter_predict_mocked,
96
  time_mocked
97
  ):
98
- from samgis.io.lambda_wrapper import lambda_handler
99
  from tests import TEST_EVENTS_FOLDER
100
 
101
  time_mocked.return_value = 0
@@ -145,7 +146,7 @@ class TestAppFailures(unittest.TestCase):
145
  import xyzservices
146
  import shapely
147
 
148
- from samgis.io.lambda_wrapper import lambda_handler
149
  from tests import LOCAL_URL_TILE, TEST_EVENTS_FOLDER
150
 
151
  local_tile_provider = xyzservices.TileProvider(name="local_tile_provider", url=LOCAL_URL_TILE, attribution="")
@@ -190,7 +191,7 @@ class TestAppFailures(unittest.TestCase):
190
  assert len(output_geojson.geoms) == expected_response_body["n_shapes_geojson"]
191
 
192
  def test_debug(self):
193
- from samgis.io.lambda_wrapper import lambda_handler
194
 
195
  input_event = {
196
  'bbox': {
 
5
 
6
  from awslambdaric.lambda_context import LambdaContext
7
 
8
+ from samgis.io import lambda_helpers
9
+ from src import lambda_wrapper
10
  from tests.local_tiles_http_server import LocalTilesHttpServer
11
 
12
 
 
22
  samexporter_predict_mocked,
23
  time_mocked
24
  ):
25
+ from src.lambda_wrapper import lambda_handler
26
 
27
  time_mocked.return_value = 0
28
  get_parsed_request_body_mocked.value = {}
 
45
  @patch.object(time, "time")
46
  @patch.object(lambda_wrapper, "get_parsed_request_body")
47
  def test_lambda_handler_400(self, get_parsed_request_body_mocked, time_mocked):
48
+ from src.lambda_wrapper import lambda_handler
49
 
50
  time_mocked.return_value = 0
51
  get_parsed_request_body_mocked.return_value = {}
 
65
 
66
  @patch.object(time, "time")
67
  def test_lambda_handler_422(self, time_mocked):
68
+ from src.lambda_wrapper import lambda_handler
69
 
70
  time_mocked.return_value = 0
71
  event = {"body": {}, "version": 1.0}
 
96
  samexporter_predict_mocked,
97
  time_mocked
98
  ):
99
+ from src.lambda_wrapper import lambda_handler
100
  from tests import TEST_EVENTS_FOLDER
101
 
102
  time_mocked.return_value = 0
 
146
  import xyzservices
147
  import shapely
148
 
149
+ from src.lambda_wrapper import lambda_handler
150
  from tests import LOCAL_URL_TILE, TEST_EVENTS_FOLDER
151
 
152
  local_tile_provider = xyzservices.TileProvider(name="local_tile_provider", url=LOCAL_URL_TILE, attribution="")
 
191
  assert len(output_geojson.geoms) == expected_response_body["n_shapes_geojson"]
192
 
193
  def test_debug(self):
194
+ from src.lambda_wrapper import lambda_handler
195
 
196
  input_event = {
197
  'bbox': {