Skip to content

Commit

Permalink
Switch to using uv's built-in project and dependency management; upda…
Browse files Browse the repository at this point in the history
…te Dockerfile to use uv; use arm64 Docker image
  • Loading branch information
malessi committed Jan 22, 2025
1 parent c8c60fd commit fdbc160
Show file tree
Hide file tree
Showing 9 changed files with 624 additions and 584 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
FROM public.ecr.aws/lambda/python:3.13 as base
FROM ghcr.io/astral-sh/uv:0.5.22 AS uv
# First, bundle the dependencies into the task root.
FROM public.ecr.aws/lambda/python:3.13-arm64 AS builder
# Enable bytecode compilation, to improve cold-start performance.
ENV UV_COMPILE_BYTECODE=1
# Disable installer metadata, to create a deterministic layer.
ENV UV_NO_INSTALLER_METADATA=1
# Enable copy mode to support bind mount caching.
ENV UV_LINK_MODE=copy
# Bundle the dependencies into the Lambda task root via `uv pip install --target`.
#
# Omit any local packages (`--no-emit-workspace`) and development dependencies (`--no-dev`).
# This ensures that the Docker layer cache is only invalidated when the `pyproject.toml` or `uv.lock`
# files change, but remains robust to changes in the application code.
COPY pyproject.toml .
COPY uv.lock .
RUN --mount=from=uv,source=/uv,target=/bin/uv \
--mount=type=cache,target=/root/.cache/uv \
uv export --frozen --no-emit-workspace --no-dev --no-editable -o requirements.txt && \
uv pip install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"

# Build required python packages
FROM base as python
COPY requirements.txt .
RUN pip3 install -r requirements.txt

# Copy function code
FROM base as lambda
COPY --from=python /var/lang/lib/python3.13/site-packages /var/lang/lib/python3.13/site-packages
COPY --from=python /var/lang/bin /var/lang/bin
COPY . "${LAMBDA_TASK_ROOT}/"
ENV PATH="/var/lang/lib/python3.13/site-packages:${PATH}"
CMD [ "ccw_manifests_verifier.handler" ]
FROM public.ecr.aws/lambda/python:3.13-arm64
# Copy the runtime dependencies from the builder stage.
COPY --from=builder ${LAMBDA_TASK_ROOT} ${LAMBDA_TASK_ROOT}
# Copy the application code.
COPY . ${LAMBDA_TASK_ROOT}/app
CMD [ "app.ccw_manifests_verifier.handler" ]

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*
!requirements.txt
!pyproject.toml
!uv.lock
!ccw_manifests_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,11 @@ It is assumed you are using `pyright`/`pylance` for type-checking, `uv` for virt
2. Setup Python 3.13 virtual environment:

```bash
uv venv --python 3.13
source .venv/bin/activate
uv sync
```

3. Install all dependencies:

```bash
uv pip sync requirements.txt dev-requirements.txt
```

4. Your virtual environment is now setup!
3. Your virtual environment is now setup! By default, it is available under `.venv`; using VS Code, this Virtual Environment can be chosen using the `Python: Select Interpreter` command

## Updating/managing dependencies

1. Add the dependency (with version constraint, if necessary) to `dev-requirements.in` if it is a development-only tool/library (like for testing, linting, etc.) or `requirements.in` if it is a runtime dependency
2. Source the virtual environment (`source .venv/bin/activate`)
3. Run:

```bash
uv pip compile --upgrade --output-file requirements.txt --generate-hashes requirements.in
uv pip compile --upgrade --constraint requirements.txt --output-file dev-requirements.txt --generate-hashes dev-requirements.in
```

4. Install requirements:

```bash
uv pip sync requirements.txt dev-requirements.txt
```
See [`Managing dependencies`](https://docs.astral.sh/uv/concepts/projects/dependencies/) and [`Locking and syncing`](https://docs.astral.sh/uv/concepts/projects/sync/) in the `uv` docs for more information.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,55 @@
requires-python = ">=3.13"
name = "ccw-manifests-verifier"
version = "1.0.0"
dependencies = [
"aws-lambda-powertools[all]>=3.4.1",
"boto3>=1.36.3",
"psycopg[binary]>=3.2.4",
"pydantic>=2.10.5",
]

[dependency-groups]
dev = [
"boto3-stubs[rds,s3,sns]>=1.36.3",
"ruff>=0.9.2",
"uv>=0.5.22",
]

[tool.ruff]
# Set the maximum line length to 100.
line-length = 100

[tool.ruff.lint]
select = [
"D", # pydocstyle
"E501",
# pycodestyle
"E",
# Pyflakes
"F",
# pyupgrade
"UP",
# flake8-bugbear
"B",
# flake8-simplify
"SIM",
# isort
"I",
"ANN",
"LOG",
"G",
"PT",
"RSE",
"PIE",
"RET",
"SLF",
"ARG",
"PTH",
"PLE",
"PLW",
"PERF",
"FURB",
"RUF",
"D", # pydocstyle
"E501",
# pycodestyle
"E",
# Pyflakes
"F",
# pyupgrade
"UP",
# flake8-bugbear
"B",
# flake8-simplify
"SIM",
# isort
"I",
"ANN",
"LOG",
"G",
"PT",
"RSE",
"PIE",
"RET",
"SLF",
"ARG",
"PTH",
"PLE",
"PLW",
"PERF",
"FURB",
"RUF",
]

[tool.ruff.lint.pydocstyle]
Expand All @@ -62,3 +75,4 @@ reportUnknownMemberType = false
reportUnknownVariableType = false
reportUnknownArgumentType = false
reportUnknownParameterType = false

This file was deleted.

Loading

0 comments on commit fdbc160

Please sign in to comment.