Following the announcement that AWS Lambda now supports running Java code, I took the chance to try it out. It was pretty straightforward to follow the documentation and see it running.
According to AWS Lambda’s document, there are essentially two ways of creating lambda function handler. One is to implement one of the pre-defined interface classes which requires you to implement “handleRequest” method, the other is to designate your own custom method as the handler.
For the sake of simplicity or my own laziness, I chose implementing AWS Lambda’s interface.
com.amazonaws.services.lambda.runtime.RequestHandler
All it takes is just
public class CurrentTime implements RequestHandler<Request, Response> { public Response handleRequest(Request request, Context context) { LambdaLogger logger = context.getLogger(); logger.log(String.format("name:%s, requestId:%s, function:%s, groupName:%s, logStreamName:%s", request.getName(), context.getAwsRequestId(), context.getFunctionName(), context.getLogGroupName(), context.getLogStreamName())); Response response = new Response(); response.setMessage((new Date()).toString()); return response; } }
Request and Response classes are custom classes for the input and output. This function simply logs the request (with information from request object as well as context provided by AWS) and return the current timestamp in the response.
The entire project can be found on github.
The instruction to prepare the deployable jar file is also easy to follow. Once you have that, the instruction for deploying the jar file through AWS Lambda console can be found at here.
To create a new Lambda Function
Invoke the function
Log in the AWS Cloud Watch
Function List
What is next?
Maybe try out invoking the function for the AWS Events.