Getting 502 'internal server error' response even though request succeeds in DynamoDB


#1

Hey all,

So I’ve been implementing these basic REST API requests https://github.com/serverless/examples/tree/master/aws-node-rest-api-with-dynamodb

Things are going well but I am for some reason getting a 502 ‘internal server error’ when performing the UPDATE even though my Item is indeed getting updated just fine in the DynamoDB Table. Has anyone encountered this?


#2

what does the sls logs -f YourFunctionName -t command show for the error logs?


#3
START RequestId: c4b9bf21-0dad-11e7-ada9-89939ea0b366 Version: $LATEST
END RequestId: c4b9bf21-0dad-11e7-ada9-89939ea0b366
REPORT RequestId: c4b9bf21-0dad-11e7-ada9-89939ea0b366	Duration: 38.41 ms	Billed Duration: 100 ms 	Memory Size: 1024 MB	Max Memory Used: 29 MB	

This is what I’m getting which I interpret to be all good?


#4

where are you seeing the 502 error?


#5

Make sure to set CORS headers if you are requesting from client https://github.com/serverless/examples/blob/master/aws-node-rest-api-with-dynamodb/todos/create.js#L38

module.exports.hello = function(event, context, callback) {

    const response = {
      statusCode: 200,
      headers: {
        "Access-Control-Allow-Origin" : "*", // Required for CORS support to work
        "Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS 
      },
      body: JSON.stringify({ "message": "Hello World!" })
    };

    callback(null, response);
};


#6

Just re-deployed the function with the headers, still getting 502 from Postman and API Gateway, item still succeeding in Dynamo


#7

Maybe the response object is not correct? Forgetting to stringify the body? or an invalid statusCode?

Enabling Cloudwatch logs for the API Gateway will most likely reveal the error, it has helped me in the past when troubleshooting issues like this.


#8

Pretty sure @bni is on the right track here - it sounds like the response object.

Look at the code you linked, all the errors have console.error lines before them, so since you’re not seeing those errors (and your DDB operation is working) it pretty much has to be the response object format being wrong. Log out the response object and verify it before you callback(null, response) line to be sure.