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

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?

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

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?

where are you seeing the 502 error?

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);
};

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

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.

2 Likes

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.

Hello ALL,
Has anyone found a solution ?
I happen to be facing the exact same issue.

If so please share. Thank you in advance.

Cheers,
Hamlet

I was facing similar issue.
In my case, Lambda was timing out in 6 seconds. Lambda was making ‘DDB Update’ request but because of 3 ‘Provision Write Capacity Units’, ‘DDB update operation’ was taking more time and during this time lambda was getting timed out.
Solutions: I increased ‘Provision Write Capacity Units’ of DDB to required.
‘DDB Update’ operation was taking more time because in my case I was making number of ‘DDB Update’ operations concurrently so.