0
votes

I am using Lambda proxy integration to execute lambda from API Gateway. Following is my code for the same in Java. My lambda is executing properly from Eclipse and from AWS console. I assume this should be with the configuration of my API gateway. I have created a POST method and have defined a model as well.

But when I am trying the "Test" option from API Gateway, I am getting "Internal Error": {"errorMessage":"Input value must not be null","errorType":"java.lang.IllegalArgumentException"} and with "Execution failed due to configuration error: Malformed Lambda proxy response".

public class SavePersonHandler implements RequestHandler<PersonRequest, JSONObject> {

    private DynamoDB dynamoDb;
    JSONParser parser = new JSONParser();
    private String DYNAMODB_TABLE_NAME = "XXXX";
    private Regions REGION = Regions.US_WEST_2;

    public JSONObject handleRequest(UserRequest userRequest, Context context) {
        this.initDynamoDbClient();
        LambdaLogger logger = context.getLogger();
        String responseCode = "200";

        JSONObject responseJson = new JSONObject();

        saveData(personRequest); // inserting data into DynamoDB
        UserResponse userResponse = new UserResponse();
        try {

            //Getting the recently inserted data back from DynamoDB
            Item item = this.dynamoDb.getTable(DYNAMODB_TABLE_NAME).getItem("id", personRequest.getId(), "id, firstName, lastName, age, address", null);

            personResponse.setMessage("Saved Successfully-"+item.toJSON());
            JSONObject responseBody = new JSONObject();
            responseBody.put("message", item.toJSON());

            JSONObject headerJson = new JSONObject();
            headerJson.put("x-custom-header", "custom header value");
            responseJson.put("isBase64Encoded", false);
            responseJson.put("statusCode", responseCode);
            responseJson.put("headers", headerJson);
            responseJson.put("body", responseBody.toString()); 

        }
        catch (Exception e) {
            System.err.println("GetItem failed.");
            System.err.println(e.getMessage());
        }

        return responseJson;
     }
1

1 Answers

0
votes

Unable to integrate API gateway with aws lambda helped solve the issue.

The response had to be sent back as a POJO object directly rather than serializing the POJO and sending it back as a String. This is how I got it to work.