1
votes

I am implementing an aws lambda function coded in java using the aws-lambda-java-events library. For troubleshooting purpose I am for now just trying to respond back with the same request body

public class LambdaFunctionHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
    @Override
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
        return new APIGatewayProxyResponseEvent().withBody(input.getBody());
    }
}

I have defined the above lambda function and an api gateway but I get the input.getBody as null. The output of the lambda function is an empty json object

{}

I tested this through the AWS lambda test console. The execution succeeds and the output is as above. The api gateway integration request is configured to call the above lambda function on a POST method.

Below are my queries

  1. How do I get the aws test event json as part of the APIGatewayProxyRequestEvent getBody() string?
  2. How do I get the request body passed to the API Gateway POST method in the APIGatewayProxyRequestEvent getBody() string variable? Do I need to define a mapping template? If so how should it look like (I made multiple attempts but with no success)

The request body I want to pass to the API gateway POST method through POSTMAN is

{
    "question":"Hello, how are you doing?"
}

Thanks!

1
How are you testing it? I guess you are testing it from lambda console as a normal event. Is that right? - Rohith Joseph
Yes, I just realized that I need to test it with a gateway proxy event. - Andy Dufresne

1 Answers

4
votes

I tried the same. The code I used was

public class LambdaFunctionHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent,
            Context context) {
        return new APIGatewayProxyResponseEvent().withBody(apiGatewayProxyRequestEvent.getBody());
    }
}

I guess you are testing from the AWS console directly. You cannot test an event-triggered lambda directly.

You have to create the test with "Amazon API Gateway Proxy" event as shown below.

enter image description here

And pass the request body as part of the parameters.

enter image description here

With these inputs, I got the expected output.

enter image description here