FROM amazon/aws-sam-cli-emulation-image-java11

# We need yum to install some of the packages to help building sam applications in container.
# Some of the emulation images are based on AL1 (eg: Java8) which already have yum installed so we
# can simply use `yum install` to install necessary packages. Some emulation images which are based
# on AL2 (eg: Java11) decided to trim down the image size by removing unneeded packages, so these
# emulation images do not have yum. So we perform a trick where we:
# 1. Copy emulation root to AL2 image (which have yum)
# 2. Then install all needed packages to copied emulation root by using yum in AL2.
# 3. Then copy the updated emulation image root back to emulation image.

# Copying root from runtimes image to AL2
FROM amazonlinux:2
COPY --from=0 / /rootfs

# Installing by yum at copied location
RUN yum groupinstall -y development --installroot=/rootfs && \
  yum install -d1 --installroot=/rootfs -y \
  yum \
  tar \
  gzip \
  unzip \
  python3 \
  jq \
  grep \
  curl \
  make \
  binutils \
  gcc-c++ \
  procps \
  libgmp3-dev \
  zlib1g-dev \
  libmpc-devel \
  && yum clean all

# Copying root from AL2 to runtimes image
FROM amazon/aws-sam-cli-emulation-image-java11
COPY --from=1 /rootfs /

# Install AWS CLI
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && unzip awscliv2.zip && ./aws/install && rm awscliv2.zip && rm -rf ./aws

# Install SAM CLI in a dedicated Python virtualenv
ARG SAM_CLI_VERSION
RUN curl -L "https://github.com/awslabs/aws-sam-cli/archive/v$SAM_CLI_VERSION.zip" -o "samcli.zip" && \
  unzip samcli.zip && python3 -m venv /usr/local/opt/sam-cli && \
  /usr/local/opt/sam-cli/bin/pip3 --no-cache-dir install -r ./aws-sam-cli-$SAM_CLI_VERSION/requirements/reproducible-linux.txt && \
  /usr/local/opt/sam-cli/bin/pip3 --no-cache-dir install ./aws-sam-cli-$SAM_CLI_VERSION && \
  rm samcli.zip && rm -rf aws-sam-cli-$SAM_CLI_VERSION

ENV PATH=$PATH:/usr/local/opt/sam-cli/bin

ENV LANG=en_US.UTF-8

# Wheel is required by SAM CLI to build libraries like cryptography. It needs to be installed in the system
# Python for it to be picked up during `sam build`
RUN pip3 install wheel

# Setup Java Home

ENV JAVA_HOME="/var/lang"

# Install Java build tools

RUN mkdir /usr/local/gradle && curl -L -o gradle.zip https://downloads.gradle-dn.com/distributions/gradle-6.2.2-bin.zip && \
  unzip -d /usr/local/gradle gradle.zip && rm gradle.zip && mkdir /usr/local/maven && \
  curl -L http://mirror.cc.columbia.edu/pub/software/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz | \
  tar -zx -C /usr/local/maven

ENV PATH="/usr/local/gradle/gradle-6.2.2/bin:/usr/local/maven/apache-maven-3.6.3/bin:${PATH}"

COPY ATTRIBUTION.txt /