- Overview
- Prerequisites
- General Implementation Steps
- Known Issues and Solutions
- Reference Implementation
This documentation provides guidance for adding GraalVM support for AWS Lambda Powertools Java modules and using the modules in Lambda functions.
- GraalVM 21+ installation
- Maven 3.x
GraalVM native image compilation requires complete knowledge of an application's dynamic features at build time. The GraalVM reachability metadata (GRM) JSON files are essential because Java applications often use features that are determined at runtime, such as reflection, dynamic proxy classes, resource loading, and JNI (Java Native Interface). The metadata files tell GraalVM which classes need reflection access, which resources need to be included in the native image, and which proxy classes need to be generated.
In order to generate the metadata reachability files for Powertools for Lambda, follow these general steps.
-
Add Maven Profiles
-
Generate Reachability Metadata
- Set the
JAVA_HOME
environment variable to use GraalVM - Run tests with
-Pgenerate-graalvm-files
profile.
- Set the
mvn -Pgenerate-graalvm-files clean test
- Validate Native Image Tests
- Set the
JAVA_HOME
environment variable to use GraalVM - Run tests with
-Pgraalvm-native
profile. This will build a GraalVM native image and run the JUnit tests.
- Set the
mvn -Pgraalvm-native clean test
- Clean Up Metadata
- GraalVM metadata reachability files generated in Step 2 contains references to the test scoped dependencies as well.
- Remove the references in generated metadata files for the following (and any other references to test scoped resources and classes):
- JUnit
- Mockito
- ByteBuddy
-
Mockito Compatibility
- Powertools uses Mockito 5.x which uses “inline mock maker” as the default. This mock maker does not play well with GraalVM. Mockito recommends using subclass mock maker with GraalVM. Therefore
generate-graalvm-files
profile uses subclass mock maker instead of inline mock maker. - Subclass mock maker does not support testing static methods. Tests have therefore been modified to use JUnit Pioneer to inject the environment variables in the scope of the test's execution.
- Powertools uses Mockito 5.x which uses “inline mock maker” as the default. This mock maker does not play well with GraalVM. Mockito recommends using subclass mock maker with GraalVM. Therefore
-
Log4j Compatibility
- Version 2.22.1 fails with this error
java.lang.InternalError: com.oracle.svm.core.jdk.UnsupportedFeatureError: Defining hidden classes at runtime is not supported.
- This has been fixed in Log4j 2.24.x. PT has been updated to use this version of Log4j
Working example is available in the examples.