First Time User of AWS Lambda

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

simple-lambda-java-create

Invoke the function

simple-lambda-java-console

Log in the AWS Cloud Watch

simple-lambda-java-cloud-watch

Function List

simple-lambda-java-function-list

What is next?

Maybe try out invoking the function for the AWS Events.

Advertisements
First Time User of AWS Lambda

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s