Demystifying Java AWS Lambda handlers for API Gateway

George Mao
2 min readDec 16, 2020

I recently did a deep dive into writing Java based AWS Lambda functions for Amazon API Gateway payloads. Special thanks to Heeki Park for helping figure this out! Check out his Medium for awesome AWS tips. Here’s the things you should know and will save you time and headache:

You have two options for writing handlers written in Java. The related AWS docs are here. I found it really confusing so I’ll try to summarize. First, your class should implement one of these two interfaces:

[Option 1] com.amazonaws.services.lambda.runtime.RequestHandler

[Option 2] com.amazonaws.services.lambda.runtime.RequestStreamHandler

Option 1: RequestHandler

This is the simplest way to write your function. With this option, your handler class should implement RequestHandler and your handler method should have two parameters: the event and the context . For the event, use any POJO, Custom class, or an AWS provided class from the events library. The type for the event should match the input you expect your Lambda function to receive. It could be a string, JSON payload, integer/number, or Base64 encoded string.

API Gateway will always deliver a JSON payload to your function. This means you should use either a custom object or one of the provided classes from the events library.

If you are using REST APIs you should use the APIGatewayProxyRequestEvent type. This class is written…

--

--

George Mao

Distinguished Engineer @ Capital One leading all things Serverless | Ex -AWS WW Serverless Tech Lead.