Part 2: Java
In our previous installment of Going Serverless, we got familiar with AWS Lambda functions and made one of our own, using Node.js. For this installment, we are going to look at how you can build Lambda functions in Java. We’ll take an existing Trajectory Modeler utility and make it available on Lambda. No need to go into the modeler code here, but it takes a Well Known Text (WKT) geometry and some information about the planned departure time, speed and altitude of a flight. It then calculates a trajectory of positions for that flight, including latitude, longitude, altitude, and time. With that goal in mind, this article will review how to make it happen.
When it comes to Java, we typically use maven and often eclipse. Fortunately, AWS provides an eclipse toolkit plugin for building and deploying maven based projects. We’ll provide a quick overview, but you can find more details here:
First, you can install the toolkit as you would most eclipse plugins, through its software management screens.
As seen in the screenshot above, there are a number of tools included in the bundle, most of which we won’t need to make use of here – but will be handy for future enhancements.
The AWS documentation also includes a tutorial that walks you through the process of developing a basic lambda function with the toolkit: https://docs.aws.amazon.com/toolkit-for-eclipse/v1/user-guide/lambda-tutorial.html
Creating a new Lambda project leads you to a starter RequestHandler implementation that looks like this:
As you can see, the logic is pretty simple. It takes an input object and returns a string. In most cases, these are likely to use JSON. Let’s extend that to do something a little more interesting. The following code makes use of our Trajectory Modeler library to get a 4D trajectory and return it, in its response.
After including the trajectory modeler as a maven dependency in my pom, I was able to write this implementation of the lambda RequestHandler interface (note: I’ve left out the input validation and error handling code to keep this clear and simple):
When I was ready to push the function code to AWS, the eclipse toolkit helped again. To do this, simply right-click on your development window and find the AWS Lambda sub-menu. Choose “Upload function to AWS Lambda…”. These options are shown below.
This will lead you through the setting of options for the deployment. The following screenshot shows what this looks like.
When you have finished with the upload, you can then use the eclipse toolkit to invoke your function, as well. In the same context menu, select “Run function on AWS Lambda…” This provides you with the following dialog.
Here you can enter your input value, or load it from a file. Remember that when invoking the function, that although you may be using JSON, the eclipse tool is expecting a string. This means that the entire JSON text, must be surrounded by quotes and each quote in the JSON must be escaped (\”).
The trajectory output is too verbose to show here in its entirety, but this screen capture, of the console log, shows a sample of the input and output of the function.
At this point you can configure any external access, you wish, as we did in the previous article.
A nice feature of the AWS Lambda framework, is its flexibility. Over two articles we’ve now seen how it can be used with Node.js, using third party tools; and now with Java, using AWS tools. In both cases we were able to build, test, and publish functions that met our needs. These functions are now available to be run at a reasonable price, when – and only when – we need them.
Thank for reading our Java walk-through. Be sure to watch for our next installment, when we’ll get creative with Python – both on AWS and a Raspberry Pi client. See you then!