# Dockerfile-big-lambda-Alpine
# 2023-12-10 | CR
# Used by: scrips/aws_big_lambda/big_lambdas_manager.sh

# Define global args
ARG FUNCTION_DIR="/home/app/"
ARG RUNTIME_VERSION="3.12"
ARG DISTRO_VERSION="3.16"

# Stage 1 - bundle base image + runtime
# Grab a fresh copy of the image and install GCC
FROM python:${RUNTIME_VERSION}-alpine${DISTRO_VERSION} AS python-alpine
# Install GCC (Alpine uses musl but we compile and link dependencies with GCC)
RUN apk add --no-cache \
    libstdc++

# Stage 2 - build function and dependencies
FROM python-alpine AS build-image

# To solve any eventual docker vulnerabilies reported...
RUN apk upgrade

# Install aws-lambda-cpp build dependencies
RUN apk add --no-cache \
    build-base \
    libtool \
    autoconf \
    automake \
    libexecinfo-dev \
    make \
    cmake \
    libcurl \
    git

# Include global args in this stage of the build
ARG FUNCTION_DIR
ARG RUNTIME_VERSION

# Copy all repo content (files and directories) to handler function
RUN mkdir -p ${FUNCTION_DIR}

# Copy all repo content to handler function
COPY . ${LAMBDA_TASK_ROOT}

# RUN cd ${LAMBDA_TASK_ROOT} && \
#     mkdir -p chalicelib && \
#     mv config chalicelib/ && \
#     mv config_dbdef chalicelib/ && \
#     mv constants chalicelib/ && \
#     mv models chalicelib/ && \
#     mv util chalicelib/

# Optional – Install the function's dependencies

# Error: Please make sure the libxml2 and libxslt development packages are installed.
    # google: alpine install libxml2-dev libxslt-dev python-dev
    # https://stackoverflow.com/questions/5178416/libxml-install-error-using-pip
    # google: alpine linux can't find Rust compiler
    # https://github.com/docker/compose/issues/8105
    # https://github.com/pyca/cryptography/blob/main/docs/installation.rst#alpine
RUN apk add --no-cache \
    libxml2-dev libxslt-dev python3-dev gcc \
    musl-dev libffi-dev openssl-dev cargo pkgconfig

# RUN python${RUNTIME_VERSION} -m pip install -r ${FUNCTION_DIR}/requirements.txt --target ${FUNCTION_DIR}
RUN python -m pip install -r ${FUNCTION_DIR}requirements.txt --target ${FUNCTION_DIR}

# Install Lambda Runtime Interface Client for Python
# RUN python${RUNTIME_VERSION} -m pip install awslambdaric --target ${FUNCTION_DIR}
RUN python -m pip install awslambdaric --target ${FUNCTION_DIR}

# Stage 3 - final runtime image
# Grab a fresh copy of the Python image
FROM python-alpine
# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}
# Copy in the built dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}
# (Optional) Add Lambda Runtime Interface Emulator and use a script in the ENTRYPOINT for simpler local runs
ADD https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie /usr/bin/aws-lambda-rie
COPY ./entry-Alpine.sh /
RUN chmod 755 /usr/bin/aws-lambda-rie /entry-Alpine.sh
ENTRYPOINT [ "/entry-Alpine.sh" ]
CMD [ "app.app" ]
# CMD [ "app.handler" ]
