Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use AWS API gateway + lambda function + RequestHandler

If I use AWS API gateway and integration type set to lambda function, in the Java implementation of the lambda function, is it possible to override RequestHandler (instead of RequestStreamHandler)? So that the input to my lambda function will be a POJO I defined in Java code and output of it would be another POJO and the client should just need to send the JSON serialization of that request POJO and receives a JSON serialization of the response POJO. I've tried to do that but whatever my client sends it will just receive an empty 200 response.

Not sure what's going wrong here, can anyone help? enter image description hereenter image description hereenter image description here

like image 650
Jiechao Wang Avatar asked Apr 01 '18 18:04

Jiechao Wang


People also ask

Can we call API gateway from Lambda?

And yes, you can call this API (Lambda proxy) as any Rest API.

Can you invoke a cross account Lambda function from an API gateway integration?

You can now use an AWS Lambda function from a different AWS account as your API integration backend. Each account can be in any region where Amazon API Gateway is available. This makes it easy to centrally manage and share Lambda backend functions across multiple APIs.


1 Answers

So I create a sample app

LambdaFunctionHandler.java

package com.amazonaws.lambda.demo;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaFunctionHandler implements RequestHandler<RequestClass, ResponseClass> {

    public ResponseClass handleRequest(RequestClass input, Context context) {
        context.getLogger().log("Input: " + input);

        // TODO: implement your handler
        ResponseClass myVal = new ResponseClass();
        myVal.setGreetings("Hello from POJO - " + input.getName() );
        return myVal;
    }

}

ResponseClass.java

package com.amazonaws.lambda.demo;

public class ResponseClass {
    String greetings;

    public String getGreetings() {
        return greetings;
    }

    public void setGreetings(String greetings) {
        this.greetings = greetings;
    }

    public ResponseClass(String greetings) {
        this.greetings = greetings;
    }

    public ResponseClass() {
    }

}

RequestClass.java

package com.amazonaws.lambda.demo;

public class RequestClass {
    String name;

    public String getName() {
        return name;
    }

    public void setName(String firstName) {
        this.name = firstName;
    }

    public RequestClass(String name) {
        this.name = name;
    }

    public RequestClass() {
    }
}

After uploading the same to AWS, I created a API Gateway for the same

Config

And the tests on the same

Testing

As you can see POJO works in Lambda + API Gateway combination

like image 80
Tarun Lalwani Avatar answered Sep 21 '22 13:09

Tarun Lalwani