In the previous post, we looked at a simple Lambda handler using the AWS Java SDK. In this post, we're going to implement Rest API with Lambda (using Lambda Proxy Integration).

What is Lambda Proxy Integration

When a client submits an API request, API Gateway passes the request to the integrated Lambda function as-is, except that the order of the request parameters is not preserved. This request data includes the request headers, query string parameters, URL path variables, payload, and API configuration data.

The configuration data can include current deployment stage name, stage variables, user identity, or authorization context (if any). The backend Lambda function parses the incoming request data. The API gateway simply acts as a postman to pass requests and responses. One advantage is that you can change the implementation of the Lambda without impacting the API.

Let's implement using AWS Java SDK

Create an empty maven project and update the pom.xml with the following contents.

<project xmlns=""
    <description>Demonstrates API Request &amp; Response Using Lambda and Java SDK for AWS</description>
        <!-- -->

We are adding 2 main dependencies here:

  1. aws-lambda-java-core - This provides RequestHandler
  2. aws-lambda-java-events - This provides APIGatewayProxyRequestEvent and APIGatewayProxyResponseEvent

Added 2 plugins:

  1. Compiler plugin - determines java version for the compiler.
  2. maven-shade-plugin - to generate fat jar aka Uber jar (with all dependencies)

Create a class LambdaHandler, with the following contents.

package net.rajanpanchal.handlers;


public class LambdaHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent requestEvent, Context context) {
        APIGatewayProxyResponseEvent responseEvent = new APIGatewayProxyResponseEvent();
        try {
            LambdaLogger logger = context.getLogger();
            logger.log("requestEventString:" + requestEvent.toString());
            logger.log("query string param:" + requestEvent.getQueryStringParameters());
            logger.log("headers:" + requestEvent.getHeaders());
            // fetching the value send in the request body
            String message = requestEvent.getBody();
            // Request request = gson.fromJson(message, Request.class);
            logger.log("message body:" + message);

            // setting up the response message
            responseEvent.setBody("Hello from Lambda!");

            return responseEvent;

        } catch (Exception ex) {
            responseEvent.setBody("Invalid Response");
            return responseEvent;

The above code is simple. requestEvent object is of type APIGatewayProxyRequestEvent. You will get headers, body, query-string, etc. from this object. responseEvent object is of type APIGatewayProxyResponseEvent. You can set response headers, body, status, etc in this object.

Now execute maven command mvn package to generate a jar file in the target folder. Login to AWS console, create Lambda function with Java 8 runtime, let it create execution role, and upload the jar file under Function Code section. In Basic Settings, set the handler name in format :


Now go to API Gateway, create a REST API AWS Java SDK Rest API using Lambda Proxy Integration Go to actions and create a new method of type GET. Select the following configuration: AWS Java SDK Rest API using Lambda Proxy Integration To enable proxy integration, make sure to check the checkbox. Save to create a method.

Go to actions and click deploy API and give the following information and click deploy. Lambda proxy integration using java sdk Use the URL generated to access the API AWS Java SDK Rest APIk


Hit the URL in the browser and you should see the below response: AWS Java SDK Rest API using Lambda Proxy Integration

Go to Cloud Watch, access Log Groups from the left-hand navigation, and access the logs corresponding to your lambda function. You should see the log statements from the lambda handler. AWS Java SDK Rest API using Lambda Proxy Integration

Source Code:

AWS Java SDK Rest API using Lambda Proxy Integration Here we complete our REST API with Lambda!

If you like the post, feel free to share and follow me on Twitter for updates!