Skip to content

Commit

Permalink
new java 21 graalvm version images
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Oct 4, 2023
1 parent ad7c444 commit 5121910
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
29 changes: 29 additions & 0 deletions amazonlinux-java-graal-community-lambda-runtime/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
19 changes: 19 additions & 0 deletions amazonlinux-java-graal-community-lambda-runtime/README.md
Original file line number Diff line number Diff line change
@@ -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/<UBERJAR_PATH>
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 <UBERJAR_PATH> <OUTPUT_ZIP_PATH?>
```

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)
3 changes: 3 additions & 0 deletions amazonlinux-java-graal-community-lambda-runtime/bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
set -euo pipefail
./function -Xmx"$AWS_LAMBDA_FUNCTION_MEMORY_SIZE"m -Djava.library.path=$(pwd)
12 changes: 12 additions & 0 deletions amazonlinux-java-graal-community-lambda-runtime/build-and-push.sh
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions amazonlinux-java-graal-community-lambda-runtime/packageZip
Original file line number Diff line number Diff line change
@@ -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!

0 comments on commit 5121910

Please sign in to comment.