I’ve added a new custom base image for Java 21 on Lambda to complement the community base images already available for Java 17, Java 18, Java 19, and Java 20. You can find the images on the ECR Public Gallery and DockerHub and the source code on GitHub. Java 21 is an LTS release with some of the most exciting new features Java has seen in a long time, so everyone should be looking to upgrade ASAP!
I’ve also released a custom Lambda runtime for Java 21, if that’s more your speed.
These should be plenty to get you started on your AWS Lambda + Java 21 adventure!
Why Java 21?
You can read the full list of Java 21 changes here, but the new (or newly stable) features I’m particularly excited about are:
- Virtual threads — Perhaps the single biggest change to the language runtime since OS threads were released in Java 1.2, the culmination of Project Loom is a transformative improvement to the language. Most programs will now get async-shaped scalability with synchronous program logic. It’s hard to overstate how big a deal this is.
- Pattern matching for switch — This is another major step forward in Java’s journey towards functional programming. This feature enables the usage of algebraic types, which is enormously useful for complex data models.
There are also several new features in preview, like structured concurrency, unnamed patterns and variables, unnamed classes and instance main methods, and string templates.
There’s a lot to love in Java 21!
I strongly recommend anyone looking for more information check out the Road to 21 playlist on the official Java YouTube channel. The team did an outstanding job of covering the upgrade journey from Java 17.
Where Can I Find the Base Images?
The images are publicly available on the ECR Public Gallery and DockerHub. I will also make them available on the other major Docker registries, but I’ll make sure that at least the current images stay available on that repository going forward.
How Do I Use Them?
Using these custom base images is just like using stock AWS base images. You can find an example on GitHub at aleph0io/example-java-lambda-function. For convenience, find the Dockerfile
below. Note the FROM
clause:
FROM public.ecr.aws/aleph0io/lambda/java:21-al2
COPY target/hello-lambda.jar "${LAMBDA_TASK_ROOT}/lib/"
CMD [ "com.sigpwned.lambda.hello.HelloLambda::handleRequest" ]
How Mature Are They?
The Java 21 images are all tested live on Lambda on deployment, but I have not built any production services with them, though I plan to soon. (And Java 21 itself is brand spanking new, so it could have bugs of its own!) Also, they are not (well) optimized for size, cold start time, or performance, so please consider them experimental, at least for now.
Can I see the Source Code?
Of course! The source code is released under the Apache 2.0 license. If you’re curious, you can find the source code for the base image on GitHub at aleph0io/aws-lambda-java-base-images. All feedback and PRs welcome! Happy hacking!