DEV Community


Creating an AWS Lambda Function in Java

Java Developer
・2 min read

To create an AWS Lambda function in Java, first, we need to create a Handler class that implements the RequestHandler interface from the lambda package of the AWS SDK (see RequestHandler interface. Then, we need to override the handleRequest method from that interface. That method receives a generic object and a context (from the lambda package of the AWS SDK too) and returns another generic object (or void) that is serialized by the runtime into a JSON document. If it is a type that wraps a primitive value, the runtime returns the representation of that value.

In the next example, we are using a Map and a String to replace the generic objects. The map object will have all the information coming from the event (from the trigger, e.g. API Gateway, as a JSON formatted string), and the context is the information about the invocation (AWS request ID, lambda function version, lambda function ARN, execution environment details, and Cognito identity information).

If you need to take advantage of the execution context for reuse (if the lambda function is on hold waiting for another usage), you can create some global variables (for example to create database connections, loggers, etc)

public class Handler implements RequestHandler<Map<String,String>, String>{
  private static final Logger logger = LoggerFactory.getLogger(Handler.class);

  public String handleRequest(Map<String,String> event, Context context)
    String response = new String("200 OK");
    return response;
Enter fullscreen mode Exit fullscreen mode

Read more about Java Lambda Functions at:

Discussion (0)