ARG FUNCTION_DIR="/var/task"
ARG VENV_DIR="/venv"

FROM 483193324480.dkr.ecr.us-east-1.amazonaws.com/python:3.9-slim-builder as builder

FROM builder as build-venv

ARG FUNCTION_DIR
ARG VENV_DIR

WORKDIR ${FUNCTION_DIR}

COPY ./requirements/ ./requirements/

RUN python -m venv ${VENV_DIR} \
    && . ${VENV_DIR}/bin/activate \
    && pip --no-cache-dir install --upgrade pip setuptools wheel \
    && pip --no-cache-dir wheel --wheel-dir=/tmp/wheelhouse -r requirements/base.pip \
    && pip --no-cache-dir wheel --wheel-dir=/tmp/wheelhouse -r requirements/dev.pip \
    && pip --no-cache-dir wheel --wheel-dir=/tmp/wheelhouse -r requirements/test.pip

FROM build-venv as build-base

RUN . ${VENV_DIR}/bin/activate \
    && pip --no-cache-dir install --trusted-host pypi.python.org --no-index --find-links=/tmp/wheelhouse -r requirements/base.pip \
    && pip --no-cache-dir install awslambdaric

FROM build-base as build-dev

RUN . ${VENV_DIR}/bin/activate \
    && pip --no-cache-dir install --trusted-host pypi.python.org --no-index --find-links=/tmp/wheelhouse -r requirements/dev.pip

FROM build-dev as build-test

RUN . ${VENV_DIR}/bin/activate \
    && pip --no-cache-dir install --trusted-host pypi.python.org --no-index --find-links=/tmp/wheelhouse -r requirements/test.pip

FROM 483193324480.dkr.ecr.us-east-1.amazonaws.com/python:3.9-slim-runner-lambda as main

ARG VERSION
ARG REVISION
ARG BUILDTIME

LABEL REVISION=${REVISION}
LABEL BUILDTIME=${BUILDTIME}

ENV SENTRY_PROJECT=juno-email-messages

COPY --from=build-base ${VENV_DIR} ${VENV_DIR}
COPY lambda-entrypoint.sh /lambda-entrypoint.sh

USER ${APP_USER}

COPY juno_email_messages ${FUNCTION_DIR}/juno_email_messages

WORKDIR ${FUNCTION_DIR}

EXPOSE 8000

ENTRYPOINT [ "/lambda-entrypoint.sh" ]

CMD ["juno_email_messages.lambda_handler"]

FROM build-test as test

COPY . ${FUNCTION_DIR}

WORKDIR ${FUNCTION_DIR}
