aws lambda handlers options

February 24, 2021 | No comments

Aws lambda handlers

While implementing AWS lambda functions in java we need to implement a Handler interface . The lambda environment will invoke the implementation of the Handler interface.
The Handler interface is a functional interface It has just one method that needs to be implemented. Broadly speaking AWS supports two different types of handlers. Following are the two interfaces that can be chosen to implement the handler.
The aws-lambda-java-core library defines two interfaces for handler methods.

Choosing the right handler

Choosing the right Handler depends on the use cases. If we are working with simple Objects as input like String, Integer, Map, etc then RequestHandler is a good choice. If we want to work with more complicated input/output objects or we want to control the way the input and output get converted to String then RequestStreamHandler is the option.
This interface is a generic interface. The handleRequest method takes two objects as input and also returns an object. The java runtime in the lambda environment deserializes and serializes input event and returned object from the method
public interface RequestHandler {
     * Handles a Lambda Function request
     * @param input The Lambda Function input
     * @param context The Lambda execution environment context object.
     * @return The Lambda Function output
    public O handleRequest(I input, Context context);
This handler works with InputStream and OutputStream. It gives more control over how we want to perform the serialization/deserialization etc.
public interface RequestStreamHandler {
     * Handles a Lambda Function request
     * @param input The Lambda Function input stream
     * @param output The Lambda function output stream
     * @param context The Lambda execution environment context object.
     * @throws IOException
    public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException;
  • RequestStreamHandler : If we need to customize the serialization and deserialization process then RequestStreamHandler is the choice.
  • RequestHandler: It is the most suitable handler in most cases.


No comments :

Post a Comment

Please leave your message queries or suggetions.