Handling Request in AWS Lambda using Java

Reading Time: 3 minutes

Do you know!! to create your own request handler using Java for AWS Lambda is quite easy. Instead of implementing AWS Lambda’s predefined interfaces and override their abstract methods, you can write your own custom handler with user-defined parameters and use them accordingly. In this blog, we’ll get to know how to create request handler in Java and deploy handler’s jar to AWS Lambda.

Lambda supports two approaches for creating a handler:
1. Loading the handler method directly without having to implement an interface. This blog describes this approach.
2.Implementing standard interfaces provided as part of aws-lambda-java-core library (interface approach).

Before proceeding further, make sure that we’ve all the necessary components or tools installed to our system which will help us to create lambda function and deploy its jar file to AWS Lambda. The prerequisites are:
1. IDE(Here we are using IntelliJ)
2. AWS Toolkit (Plugin)
3. Install AWS SAM(Serverless Application Model)

To create request handler, we’ve to create a new AWS Serverless Application project by using File->New->Project->AWS Serverless Application

Step: 1 Creating a New AWS Serverless Application Project

After creating a new project, the next step is to add the plugin to the pom.xml file.

Step: 2 Add plugin to the pom.xml

 <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
       </execution>
     </executions>
   <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <mainClass>packageName.ClassName</mainClass>
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
   </configuration>
</plugin>

This plugin will help us to create a Jar file with dependencies.

Step: 3 Create a Java class with Request Handler

The general syntax for the handler is as follows:

outputType handler-name(inputType input, Context context) {
   ...
}

inputType – The first handler parameter is the input to the request handler, which can be event data or custom input that you provide such as a string or any custom data object.
outputType – If you plan to invoke the Lambda function synchronously (using the RequestResponse invocation type), you can return the output of your function using any of the supported data types.

The following Java class shows a handler myHandler that uses Map< String , String> for input and String type as output.

import com.amazonaws.services.lambda.runtime.Context;
import java.util.Map;

public class MyRequestHandler {
   public String myHandler(Map<String, String> map, Context context)            
   {
     String value  = map.get("key");
      return value;
   }
}

In the above code, we’ve created a java class MyRequestHandler which contains a public method myHandler() with two parameters (Map<> and Context which is of AWS lambda runtime context).
So, basically, here we create one method which handles the request data in a key-value pair, myHandler() function extracts the value of the data with the help of its key and send the response of the

Step: 4 Create a Jar file of request handler

Our java code is ready to deploy to the AWS Lambda, so we have to create a jar file which will be deployed.
To make a Jar file we’ve to run a command in our terminal which is :
mvn compile package
Jar file will be created in the target package of the project.

Step: 5 Deploy Jar to AWS Lambda

NOTE: We’ve to specify our handler in the AWS Lambda which is packageName.MyRequest::myHandler to use the functionality of the Lambda function.

Step: 6 Test myHandler in AWS Lambda

Run the test case:

Handler successfully gives the response by returning the value if the key-value passed in the test case.

blog-footer


Written by 

Pawan Singh Bisht is a Software Consultant at Knoldus Software LLP, having a strong experience of more than two years in the technology field. He has been well versed in the core implementation of Rust and Java. He loves to contribute to the community which he attained from the community.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading