aws apigateway lambda always return 502

断了今生、忘了曾经 提交于 2021-01-03 05:38:43

问题


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 ?


回答1:


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.




回答2:


I had the same issue where all of my logs were saying the lambda executed successfully but API Gateway was still returning 502s on every request. Turns out I forgot to configure the response status codes in the API Gateway Console so it was throwing errors because the response was "incorrect" even with proper formatting. Just add in a status code 200, 400, 403, etc. to the route on your gateway and that might solve your problem.




回答3:


Make sure you're hitting your callback with the response before calling context.succeed(event) or some other call to context.

This was my problem and putting my callback first fixed the persistent 502 res.



来源:https://stackoverflow.com/questions/43054833/aws-apigateway-lambda-always-return-502

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!