Creating a Lambda Function with Quarkus and GraalVM

Creating a Lambda Function with Quarkus and GraalVM

Quarkus is known as a “Supersonic Subatomic Java Framework” and is designed to work well with GraalVM for creating native executables. AWS Lambda can run native executables, allowing for reduced cold start times and potentially lower resource consumption. Here’s a step-by-step tutorial on how to create a Lambda function using Quarkus with GraalVM.

Prerequisites

  • AWS Account
  • Apache Maven (3.6.3+)
  • Java SDK (Amazon Corretto 11 or newer)
  • Docker for running the native image build

1. Set up the Quarkus project

You can generate a new Quarkus project using Maven:

mvn io.quarkus:quarkus-maven-plugin:2.11.3.Final:create \
    -DprojectGroupId=com.codevup \
    -DprojectArtifactId=quarkus-native-lambda \
    -Dextensions="amazon-lambda,container-image-docker"

Running this in the terminal will generate a project with a simple GreetingLambda class.

2. Setup GraalVM

Save the following dockerfile as Dockerfile.graalbase under src/main/docker folder

FROM ghcr.io/graalvm/graalvm-ce:latest AS build
RUN gu install native-image
WORKDIR /project
VOLUME ["/project"]
ENTRYPOINT ["native-image"]

Run the following once to create a GraalVM image, this will be used later to build the native executable

docker build -f src/main/docker/Dockerfile.graalbase -t graalbase .

3. Build

Run this to build the project with graalVM

./mvnw package -Pnative -Dquarkus.native.container-build=true -Dquarkus.container-image.build=true -Dquarkus.native.builder-image=graalbase

At this point you should see the native build in target/function.zip

4. Deploy

Go to AWS Lambda and click on Create Function Set the following configuration:

  • Runtime: Provide your own bootstrap on Amazon Linux 2
  • Architecture: Set based on the architecture of the machine that you used to build

After the function is created click on Upload from > .zip file and upload the function.zip file.

Now click on Test, add try testing with following Event JSON

{
  "name": "John"
}

Check the output, you should see Hello John there if the test was successful. You should also see the duration time, so you’ll be able to compare it to the same function deployed in a non-native way.

Conclusion

You have successfully created a native executable Lambda function using Quarkus and GraalVM. This allows you to leverage the performance benefits of native images on AWS Lambda and reduce cold start time.

Remember, while the tutorial gives you the basic steps, building a production-ready application requires careful consideration regarding error handling, monitoring, security, and configuration management.

For the latest guidance and more detailed configurations, consult the Quarkus AWS Lambda guide and AWS’s documentation on deploying native functions.

Roberto
Roberto Founder and Author of Codevup