diff --git a/amazonlinux-java-graal-community-lambda-runtime/Dockerfile b/amazonlinux-java-graal-community-lambda-runtime/Dockerfile new file mode 100644 index 0000000..544d22e --- /dev/null +++ b/amazonlinux-java-graal-community-lambda-runtime/Dockerfile @@ -0,0 +1,29 @@ +# install base linux + tools +ARG AWS_LINUX_VERSION=2.0.20230119.1 +FROM amazonlinux:$AWS_LINUX_VERSION + +RUN yum install -y gcc gcc-c++ libc6-dev binutils zlib1g-dev curl bash zlib zlib-devel tar zip gzip + +# get graal... +ARG JAVA_VERSION=jdk-21.0.0 +ENV LANG=en_US.UTF-8 +ENV GRAAL_FILENAME=graalvm-community-${JAVA_VERSION}_linux-x64_bin.tar.gz +ENV GRAAL_URL=https://github.com/graalvm/graalvm-ce-builds/releases/download/${JAVA_VERSION}/${GRAAL_FILENAME} +RUN echo $GRAAL_URL +RUN curl -4 -L $GRAAL_URL -o /tmp/${GRAAL_FILENAME} + +RUN tar -zxvf /tmp/${GRAAL_FILENAME} -C /tmp && mv /tmp/${GRAAL_FILENAME} /usr/lib/graalvm +RUN rm -rf /tmp/* + +# add lambda runtime bootstrap script +COPY bootstrap . +RUN chmod 777 bootstrap + +# add script to create native executable and zip it +COPY packageZip . +RUN chmod 777 packageZip + +# set entry point script - this takes 2 args: +# 1. the input UberJar +# 2. the path to the output ZIP (optional, defaults to function.zip) +ENTRYPOINT ["./packageZip"] \ No newline at end of file diff --git a/amazonlinux-java-graal-community-lambda-runtime/README.md b/amazonlinux-java-graal-community-lambda-runtime/README.md new file mode 100644 index 0000000..1e0ceed --- /dev/null +++ b/amazonlinux-java-graal-community-lambda-runtime/README.md @@ -0,0 +1,19 @@ +# http4k/amazonlinux-java-graal-ce-lambda-runtime + +This image builds a runtime ZIP file containing a Java GraalVM binary for Amazon Linux. It is designed to work with a ShadowJar containing an http4k AWS Lambda function and, given a tasteful selection of technologies, should not require any customisation of the GraalVM configuration. + +Basic usage for an example project. Arguments to the docker run command: +1. UBERJAR_PATH = path the UberJar. This is loaded from inside the container at source/ +2. OUTPUT_ZIP_PATH (optional) = path to the output Zipfile. Defaults to source/function.zip + +```shell +./gradlew shadowJar +docker run -v $(pwd):/source \ +http4k/amazonlinux-java-graal-ce-lambda-runtime:latest +``` + +The Docker image itself includes 2 script files loaded from this repo: +- **bootstrap** - the standard bootstrap file used by the Aws Lambda runner. Detects and passes the lambda memory settings to the JVM. +- **packageZip** - takes the input ShadowJar (renamed to function.jar), uses `native-image` to create a native binary, and creates the function.zip package. + +Images are hosted in [DockerHub](https://hub.docker.com/r/http4k) diff --git a/amazonlinux-java-graal-community-lambda-runtime/bootstrap b/amazonlinux-java-graal-community-lambda-runtime/bootstrap new file mode 100644 index 0000000..bd2af59 --- /dev/null +++ b/amazonlinux-java-graal-community-lambda-runtime/bootstrap @@ -0,0 +1,3 @@ +#!/bin/sh +set -euo pipefail +./function -Xmx"$AWS_LAMBDA_FUNCTION_MEMORY_SIZE"m -Djava.library.path=$(pwd) diff --git a/amazonlinux-java-graal-community-lambda-runtime/build-and-push.sh b/amazonlinux-java-graal-community-lambda-runtime/build-and-push.sh new file mode 100755 index 0000000..e3494c1 --- /dev/null +++ b/amazonlinux-java-graal-community-lambda-runtime/build-and-push.sh @@ -0,0 +1,12 @@ +#!/bin/sh +set -eu +export AWS_LINUX_VERSION=2.0.20230119.1 +export JAVA_VERSION=$1 + +docker build --build-arg AWS_LINUX_VERSION=$AWS_LINUX_VERSION \ +--build-arg JAVA_VERSION="$JAVA_VERSION" \ +-t http4k/amazonlinux-java-graal-community-lambda-runtime \ +-t http4k/amazonlinux-java-graal-community-lambda-runtime:latest \ +-t http4k/amazonlinux-java-graal-community-lambda-runtime:amazonlinux$AWS_LINUX_VERSION-"$JAVA_VERSION" . + +docker push -a http4k/amazonlinux-java-graal-community-lambda-runtime diff --git a/amazonlinux-java-graal-community-lambda-runtime/packageZip b/amazonlinux-java-graal-community-lambda-runtime/packageZip new file mode 100644 index 0000000..56df6ce --- /dev/null +++ b/amazonlinux-java-graal-community-lambda-runtime/packageZip @@ -0,0 +1,15 @@ +#!/bin/sh +set -euo pipefail + +cp source/"$1" ./function.jar +FUNCTION_ZIP="${2:-function.zip}" + +echo Creating native-image... +/usr/lib/graalvm/bin/native-image --enable-url-protocols=https -H:+ReportExceptionStackTraces --no-server -jar function.jar function +chmod 777 function + +echo Building ZIP... +mkdir -p $(dirname source/"$FUNCTION_ZIP") +zip -j source/"$FUNCTION_ZIP" bootstrap function + +echo Done! \ No newline at end of file