|
| 1 | +######################################################## |
| 2 | +# Stage 1: Install dependencies with uv and copy to /opt/python |
| 3 | +######################################################## |
| 4 | +FROM python:3.12-slim AS deps |
| 5 | + |
| 6 | +# Avoid prompts & keep images small |
| 7 | +ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \ |
| 8 | + PIP_NO_CACHE_DIR=1 |
| 9 | + |
| 10 | +ARG APP_ROOT="/app" |
| 11 | +WORKDIR ${APP_ROOT} |
| 12 | + |
| 13 | +# Bring in lockfiles first for better caching |
| 14 | +COPY pyproject.toml ${APP_ROOT}/ |
| 15 | +COPY uv.lock ${APP_ROOT}/ |
| 16 | + |
| 17 | +# Install uv and create a local venv, then sync (no dev deps) |
| 18 | +RUN pip install --no-cache-dir uv |
| 19 | +RUN uv sync --no-group dev --frozen |
| 20 | + |
| 21 | +# Materialize a Lambda-style "layer" dir with only runtime packages |
| 22 | +# (Lambda adds /opt/python to sys.path automatically) |
| 23 | +RUN mkdir -p /opt/python |
| 24 | +RUN cp -a ${APP_ROOT}/.venv/lib/python3.12/site-packages/. /opt/python/ |
| 25 | + |
| 26 | + |
| 27 | +######################################################## |
| 28 | +# Stage 2: Create final AWS Lambda image |
| 29 | +######################################################## |
| 30 | +#FROM public.ecr.aws/lambda/python:3.12-arm64 |
| 31 | +FROM public.ecr.aws/lambda/python:3.12 |
| 32 | + |
| 33 | +ARG APP_NAME |
| 34 | +ARG APP_VERSION |
| 35 | +ARG COMMIT_SHA |
| 36 | +ARG BRANCH |
| 37 | +ARG BUILD_DATE |
| 38 | + |
| 39 | +ENV APP_NAME="${APP_NAME}" |
| 40 | +ENV APP_VERSION="${APP_VERSION}" |
| 41 | +ENV COMMIT_SHA="${COMMIT_SHA}" |
| 42 | +ENV BRANCH="${BRANCH}" |
| 43 | +ENV BUILD_DATE="${BUILD_DATE}" |
| 44 | + |
| 45 | +LABEL org.opencontainers.image.name="${APP_NAME}" \ |
| 46 | + org.opencontainers.image.version="${APP_VERSION}" \ |
| 47 | + org.opencontainers.image.revision="${COMMIT_SHA}" \ |
| 48 | + org.opencontainers.image.ref.branch="${BRANCH}" \ |
| 49 | + org.opencontainers.image.created="${BUILD_DATE}" |
| 50 | + |
| 51 | +# This image automatically adds packages in /opt/python to sys.path |
| 52 | +COPY --from=deps /opt/python /opt/python |
| 53 | +COPY src/ ${LAMBDA_TASK_ROOT}/ |
| 54 | + |
| 55 | +ENV _HANDLER="main.handler" |
| 56 | +CMD ["main.handler"] |
0 commit comments