Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws apigateway lambda always return 502

I have created aws apigateway lambda integration for my proxy server. When i am making get request to the gateway, the request is successfully going through. The lambda function also executes successfully and writes response in outputstream with statusCode as 200. But apigateway always returns 502.

Snippet of handleRequest():

 BufferedReader reader = new BufferedReader(new 
        InputStreamReader(inputStream));
        JSONObject event = (JSONObject) parser.parse(reader);
        request = Input.builder().setEvent(event).build();

    Response response = requestManager.handleRequest(request);
    logger.log(String.format("Response [%s]", response.toString()));

    JSONObject responseJson = new JSONObject();
    responseJson.put("statusCode", response.getStatusCode());
    responseJson.put("headers", response.getHeaders());
    JSONObject jsonBody = (JSONObject) parser.parse(response.getBody());
    responseJson.put("body", jsonBody);
    OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
    logger.log("response recieved");
    logger.log(String.format("responseJson [%s]", responseJson));
    writer.write(responseJson.toJSONString());
    writer.close();
    logger.log(String.format("output stream [%s]", outputStream));

Am i missing anything ?

like image 411
kunal shrivastava Avatar asked Mar 27 '17 19:03

kunal shrivastava


People also ask

Does API gateway scale automatically?

Amazon API Gateway acts as a proxy to the backend operations that you have configured. Amazon API Gateway will automatically scale to handle the amount of traffic your API receives.


1 Answers

502 errors with Lambda usuaully indicate that you are using the Lambda proxy method and not generating the proper JSON response. Make sure your response adheres to the appropriate format.

If you are still having problems, please share a sample JSON generated by your Lambda function.

like image 71
Bob Kinney Avatar answered Oct 07 '22 02:10

Bob Kinney