Java 14 support for AWS Lambda
AWS is currently the most popular choice for serverless applications, but the Java Runtime is hopelessly outdated. Why wait for Amazon to fix this, when theres’s a much better solution? The frgaal project is ready to give you all of that in a safe and simple way.
It’s been less than a year that Amazon finally updated it’s Java Lambda runtime to Java 11 and nothing happened since then. Apparently only LTS versions will be supported. If the trend continues, we’ll get a Java 15 Lambda runtime sometime in 2022. Until then, you’ll either have to work with an ancient SDK 11, or wait for Amazon to update their runtime before you can use all the nice new features introduced in versions 12,13,14, and 15 of the SDK. Fortunately there’s now a third option…
frgaal — Babel for Java
frgaal is a retrofitting compiler for Java based on the OpenJDK version of javac. frgaal can run on Java 8 itself and can produce compatible bytecode for Java versions as old as Java 8.
Let’s try this with the examples from the AWS Developers Guide. There’s a Github repository with instructions to build and deploy the minimal sample. The sample doesn’t even use the latest Lambda Runtime, but the older Java 8 runtime instead. Make sure you meet the requirements for running the sample. We’ll follow the tutorial, but make some changes before building and deploying the app. Let’s follow the instructions for the setup first:
Clone the repository:
$ git clone https://github.com/awsdocs/aws-lambda-developer-guide.git
$ cd aws-lambda-developer-guide/sample-apps/java-basic
And create a Bucket for the deployment artifacts:
Before we build and deploy we’ll modify the the pom.xml. If you open the pom, you’ll find this section:
Replace it with this to use the frgaal compiler:
We’ll also change the code in Handler.java and replace the response with a multiline text block, which were introduced in Java 13:
Now you can continue with the deployment steps from the original tutorial. make sure you build with Maven:
java-basic$ ./2-deploy.sh mvn
[INFO] Scanning for projects...
[INFO] -----------------------< com.example:java-basic >-----------------------
[INFO] Building java-basic-function 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
The final step is to invoke the test to see if it worked:
To do so run the script
"200 OK\nLook at this multiline text\n..."
The script will continue invoking the lambda, just press
CRTL+C to exit.
That’s it. Just a small change in your Maven pom.xml and you can use the latest Java features in your lambda. There’s no need to roll your own custom runtime or wait for the next release. You can keep your code up to date and you won’t need to catchup with all the new developments when a new version comes out. And the best part is, as soon as the runtime for JDK 15 is out, you can simply update this section of the pom to use the regular compiler again, or continue using frgaal to access even newer features which will have been available by then. For more info on the scope and limitations of the project checkout the frgaal project on Github.