There is no better way to explain the function (pun unintended) of complicated cloud services than walking through a step-by-step code example of an easy CS problem. We will be creating an AWS Lambda Function using Java as the service code. This function will be responsible for reversing strings that we pass to it as a payload. We will also be invoking that function using client Java code using the AWS SDK for Java.
- This is partly an AWS tutorial. So make sure you have an AWS account.
- Because we will be making use of AWS on the client side to invoke our function, make sure you have AWS CLI installed on your system. I use CLI v2. Also, make sure you run the post-setup configuration steps, which will involve creating and authenticating with access keys. You can then run “aws configure” for a faster set up.
- I will assume you already have Java installed as well as a reasonable IDE. I used IntelliJ for this one.
- Install Apache Maven. We will need this to package-up our server-side Java code into a JAR file we can then upload to our Lambda.
As mentioned, we will invoke our AWS Lambda function using a Java client. It will be created in the next section, but let’s first create our client. Create a new Java project using your IDE of choice; and make sure you have maven integrated; this means that you should have a pom.xml file where you can declare dependencies.
More installation (sorry): Install the AWS SDK for Java. This adds the necessary components we can use to invoke our function. I used Maven to add this as a dependency. IntelliJ has nice Maven support, btw. Note, as the SDK guide mentions, you don’t need to declare the entire SDK as a dependency. Just use the SDK for Lambda.
Now, we can begin coding.
For simplicity purposes, our client-side code will have only a single class (remember, this is not maintainable, so not recommended when actually building an application).
Following the instructions outlined in the official AWS code examples for invoking a Lambda function using Java, we arrive at something that looks like this:
All we are doing here is accepting a lambda function’s name through the command line (which we create in the next step); create an object capable of invoking the function — this object defines an arbitrary payload as a string constant formatted in JSON; creating and invoking an InvokeRequest and finally printing the result returned by the lambda to the console.
Now comes the part where we actually create our Lambda function using Java. Go the AWS Console and search for Lambda. Create a lambda function and name it whatever you want and use any Java runtime. I used Java 11 for this. Click into your newly created function and note down the ARN number on the top-right. This number uniquely identifies AWS resources such as Lambdas. Also, note down the region you are currently in as resources are specific to regions. Mine is in us-east-2.
To begin the server-side coding, create a new project (yes, we do this twice) and configure Maven again. Lambda functions handle requests. And these requests require specific handlers to be imported via AWS libraries. Therefore following these instructions, configure the aws-lambda-java-core dependency in your pom.xml file. This step is optional, but it is good practice to log information to AWS Cloud Watch — we can use gson library to help us with logging and you can declare this dependency by following this link.
Once again, to keep things simple, we will have only one class — the Handler — that will handle this request. This will reverse the string payload and return the reversed result. Following the official docs that explains how to handle requests, we arrive at the following:
This code sample should also be easy to follow. We define a private method down below that reverses a string. The handle request method does some logging (which is optional, but recommended), gets the payload (which is represented by the “event” parameter), calls the reverse string function; re-constructs the payload and returns the result/response.
With that our server-side code is complete. We now have to package-up this code into a JAR file and upload it to our Lambda within AWS. Make sure you’ve added the final set of Maven dependencies in your pom file, which allow you to build your code into a JAR format. When the dependencies are added:
run “mvn package” in your project directory. If the command executed successfully, you should get “BUILD SUCCESS” in the output. You can find the JAR file in your target folder within your project.
Now that we have our JAR, go to your Lambda function in AWS console and upload the file by going to: Function code -> Actions -> Upload a .zip or .jar file.
After your JAR file has uploaded successfully, set your function handler in Basic Settings -> Edit -> Handler. The first part of your handler will be your package and class name and the second part will be the method that handles the event. If you are following my code example, this was for me: “StringReversalLambdaHandler::handleRequest”.
Testing/Invoking our Lambda
At this point, all coding is complete, and all that is left is to test our awesome creation. There are two ways you can go about testing.
First, go back to your Lambda function within the AWS Console. There you can define Test Events for your Lambda function. I suggest you start with this to make sure your service code works. Define a sample event as follows:
“message1”: “Hello, World!”,
“message2”: “Hi, from Lambda!”
Then hit test and verify the execution results have reversed the two strings that are part of the event payload.
Now, for the more interesting use case. We can use the Java client code we made at the start to send a request to the Lambda service. Simply copy your Lambda’s ARN you saved earlier and supply it as an argument when executing your client Java program — and also make sure that your region is set appropriately in your Lambda client code (my region is in us-east-2).
In this walkthrough you learned how to configure your Java Maven project to use AWS’ Java SDK to invoke Lambdas through client code, and made use of AWS Lamba to set up a serverless string reversing microservice, also based on Java. Here are the two repositories used throughout this article. Feel free to go through the pom.xml in these codebases and use it as a reference if you encounter dependency issues.
Thank you for reading! :)