An AWS Lambda layer to enable Java 21 support.
- Download java21layer.zip
- Upload to S3 (required to create a layer from a ZIP file greater than 50MB in size)
- In Lambda console, create a layer using this ZIP file using the x86_64architecture and theCustom runtime on Amazon Linux 2runtime
- In Lambda console, create a function with Provide your own bootstrap on Amazon Linux 2and architecturex86_64
- In Lambda function console, add the layer you created, and upload your Java deployment package
- Run
- (Hopefully) celebrate! 🥳
- Download java21layer.zip
- Create a Lambda layer using Code.fromAsset java21layer.zip
- Note you might need to adjust the path for your own project structure
LayerVersion java21layer = new LayerVersion(this, "Java21Layer", LayerVersionProps.builder()
        .layerVersionName("Java21Layer")
        .description("Java 21")
        .compatibleRuntimes(Arrays.asList(Runtime.PROVIDED_AL2))
        .code(Code.fromAsset("java21layer.zip"))
        .build());- Create a function using the PROVIDED_AL2runtime.
- Add this layer to your function.
Function exampleWithLayer = new Function(this, "ExampleWithLayer", FunctionProps.builder()
        .functionName("example-with-layer")
        .description("example-with-layer")
        .handler("example.HelloWorld::handleRequest")
        .runtime(Runtime.PROVIDED_AL2)
        .code(Code.fromAsset("../software/ExampleFunction/target/example.jar"))
        .memorySize(512)
        .logRetention(RetentionDays.ONE_WEEK)
        .layers(singletonList(java21layer))
        .build());A custom JRE is created to reduce final file size. Lambda has a 250MB unzipped file size limit.
Dockerfile describes how the JRE is built.
Obviously, it's early days, and this is a preliminary release. There are a couple of known issues:
- The layer fails CDS (Class Data Sharing) during initialization. This creates some log traffic, and may adversely affect performance.
- It is only (lightly) tested
- Only x86_64 is supported
I'll be working on these as I have time. PRs welcome!
The following JVM settings are added by default.
--add-opens java.base/java.util=ALL-UNNAMED 
-XX:+TieredCompilation -XX:TieredStopAtLevel=1 
-Xshare:auto
Further suggestions welcomed
aws-lambda-java-core-1.2.3.jar
aws-lambda-java-serialization-1.1.2.jar
aws-lambda-java-runtime-interface-client-2.4.1.jar
$LAMBDA_TASK_ROOT
$LAMBDA_TASK_ROOT/*
$LAMBDA_TASK_ROOT/lib/*
- Docker
- Run build-jre.shto build the minimal Java 21 runtime
- Run make-layer.shto package the runtime, dependencies and bootstrap as a zip
Much credit to @msailes, who created the original repo from which this project is forked!