FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim 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

# 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.
RUN --mount=type=bind,source=uv.lock,target=uv.lock \
    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    uv export --frozen --no-emit-workspace --no-dev --no-editable -o requirements.txt && \
    uv pip install -r requirements.txt --target build_dir

FROM public.ecr.aws/lambda/python:3.12

ARG version
LABEL Version=$version
ENV build_version=$version

COPY --from=public.ecr.aws/datadog/lambda-extension:58 /opt/extensions/ /opt/extensions

# Copy the runtime dependencies from the builder stage.
COPY --from=builder build_dir ${LAMBDA_TASK_ROOT}

# Copy the application code.
COPY ./app ${LAMBDA_TASK_ROOT}/app
COPY config.py ${LAMBDA_TASK_ROOT}/

# Set the AWS Lambda handler.
ENV DD_LAMBDA_HANDLER=app.main.handler
CMD [ "datadog_lambda.handler.handler" ]
