diff --git a/Makefile b/Makefile index e02a5159..4cd4d9fc 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,7 @@ check-licenses: build: dist/lambda.zip # Build lambda.zip in dist/ dist/lambda.zip: $(MAKE_DIR)/pyproject.toml $(MAKE_DIR)/poetry.lock $(shell find src -type f) - poetry build-lambda -vv + poetry build-lambda -vv && poetry run clean-lambda deploy: # Deploy the project artefact to the target environment @Pipeline # TODO: Implement the artefact deployment step diff --git a/poetry.lock b/poetry.lock index cb1c064d..7c9413a8 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 2.1.2 and should not be changed by hand. +# This file is automatically @generated by Poetry 2.2.1 and should not be changed by hand. [[package]] name = "aiohappyeyeballs" @@ -1080,14 +1080,14 @@ yaml = ["PyYAML (>=5.4.1)"] [[package]] name = "flask" -version = "3.1.1" +version = "3.1.2" description = "A simple framework for building complex web applications." optional = false python-versions = ">=3.9" groups = ["main"] files = [ - {file = "flask-3.1.1-py3-none-any.whl", hash = "sha256:07aae2bb5eaf77993ef57e357491839f5fd9f4dc281593a81a9e4d79a24f295c"}, - {file = "flask-3.1.1.tar.gz", hash = "sha256:284c7b8f2f58cb737f0cf1c30fd7eaf0ccfcde196099d24ecede3fc2005aa59e"}, + {file = "flask-3.1.2-py3-none-any.whl", hash = "sha256:ca1d8112ec8a6158cc29ea4858963350011b5c846a414cdb7a954aa9e967d03c"}, + {file = "flask-3.1.2.tar.gz", hash = "sha256:bf656c15c80190ed628ad08cdfd3aaa35beb087855e2f494910aa3774cc4fd87"}, ] [package.dependencies] diff --git a/pyproject.toml b/pyproject.toml index b3d442b4..7619832b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,8 +26,6 @@ httpx = "^0.28.1" yarl = "^1.18.3" pydantic = "^2.12.3" asgiref = "^3.9.1" -boto3 = "^1.40.57" -botocore = "^1.40.57" eval-type-backport = "^0.2.2" mangum = "^0.19.0" wireup = "^2.1.0" @@ -35,6 +33,8 @@ python-json-logger = "^3.3.0" fhir-resources = "^8.0.0" python-dateutil = "^2.9.0" pyhamcrest = "^2.1.0" +boto3 = "^1.40.57" +botocore = "^1.40.57" aws-xray-sdk = "2.14.0" [tool.poetry.group.dev.dependencies] @@ -73,6 +73,9 @@ docker-platform = "linux/x86_64" package-artifact-path = "dist/lambda.zip" without = "dev" +[tool.poetry.scripts] +clean-lambda = "scripts.lambda.clean_lambda:main" + [tool.ruff] line-length = 120 exclude = ["docs/", "scripts/"] diff --git a/scripts/lambda/__init__.py b/scripts/lambda/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/scripts/lambda/clean_lambda.py b/scripts/lambda/clean_lambda.py new file mode 100644 index 00000000..88564196 --- /dev/null +++ b/scripts/lambda/clean_lambda.py @@ -0,0 +1,58 @@ +import zipfile +import os +import shutil +import tempfile +import logging + +ZIP_PATH = "dist/lambda.zip" + +# Configure logging +logging.basicConfig( + level=logging.INFO, + format="%(asctime)s [%(levelname)s] %(message)s" +) +logger = logging.getLogger(__name__) + +# Folders to prune (only safe unused folders) +BOTOCORE_SAFE_PRUNE = [ + "botocore/data/s3/tests", + "botocore/data/glacier/tests", +] + +BOTO3_SAFE_PRUNE = [ + "boto3/examples" +] + +def main(): + if not os.path.exists(ZIP_PATH): + logger.warning("ZIP file %s does not exist. Skipping cleanup.", ZIP_PATH) + return + + tmp_dir = tempfile.mkdtemp(prefix="lambda_clean_") + logger.info("Unzipping %s...", ZIP_PATH) + with zipfile.ZipFile(ZIP_PATH, "r") as z: + z.extractall(tmp_dir) + + # Remove only safe folders + for folder_list in [BOTOCORE_SAFE_PRUNE, BOTO3_SAFE_PRUNE]: + for rel in folder_list: + target = os.path.join(tmp_dir, rel) + if os.path.exists(target): + logger.info("Removing %s", target) + shutil.rmtree(target) + + # Re-create the cleaned zip + logger.info("Re-zipping cleaned Lambda package...") + with zipfile.ZipFile(ZIP_PATH, "w", zipfile.ZIP_DEFLATED) as z: + for root, _, files in os.walk(tmp_dir): + for f in files: + abs_path = os.path.join(root, f) + rel_path = os.path.relpath(abs_path, tmp_dir) + z.write(abs_path, rel_path) + + shutil.rmtree(tmp_dir) + logger.info("Cleaned Lambda saved at %s", ZIP_PATH) + +if __name__ == "__main__": + logger.info("Running clean_lambda.py...") + main()