message: "Internal server error" when try to access aws gateway api
Solution 1
You need to pass the statusCode
after executing the Lambda function. If you don't pass it the API Gateway will trigger an error 502 Bad Gateway
by default.
message = {
'message': 'Execution started successfully!'
}
return {
'statusCode': 200,
'headers': {'Content-Type': 'application/json'},
'body': json.dumps(message)
}
EDIT: This sample is for Python. For node.js you just need to handle callback, the message is basically the same.
callback(null, {
statusCode: 200,
body: JSON.stringify(message),
headers: {'Content-Type': 'application/json'}
});
Solution 2
Don't forget to click Deploy API under AWS API Gateway. Without it, change doesn't work.
Solution 3
For accessing dynamodb through lambda function from api gateway it needs:
Create a role in AWS console that have access to dynamodb operations.
Create a lambda function and assign the above created role to your lambda function.
Create a api from API gateway in AWS management console and allow it to access to your lambda function.
In order for your api to show a proper response the return type of lambda function should be a specific format i.e :
return {
"statusCode": 200,
"body": json.dumps(your response)
}
Solution 4
It's already explained above, but my problem was this worked for me with just calling the lambda:
exports.handler = async (event) => {
return "gugus"
};
So all the tests in lambda were fine. The logs looked fine too. Just the API response was not ok.
To call it with the API gateway it needs something like this:
exports.handler = async (event) => {
...
var res ={
"statusCode": 200,
"headers": {
"Content-Type": "*/*"
}
};
res.body = "gugus";
return res;
};
Solution 5
I had this problem using API Gateway + Lambda. In my case the problem was simply a permission issue. I was using stages in my API.
I had to execute
aws lambda add-permission --function-name X --source-arn "X" --principal apigateway.amazonaws.com --statement-id X --action lambda:InvokeFunction
Hope this helps.
Admin
Updated on July 08, 2022Comments
-
Admin almost 2 years
Created Lambda Hello world function using Node.js and created API GateWay trigger for Get call, tried the URL to access Lambda function, getting below error.
message: "Internal server error".
(very new to AWS)
-
visrahane almost 6 yearscheck comments - github.com/aws-samples/aws-serverless-workshops/blob/master/… The callback has to have a format
-
ljmocic almost 6 yearsI forgot to mention that this is for Python. Basically, the same thing is for node.js, you just need to handle the callback.
-
ConorLuddy over 5 yearsI ended up here with the same issue, but from the Node perspective. I needed to
JSON.stringify(message)
the body before passing it to APIGW. -
Amax about 5 yearsDude, you saved my day!
-
ljmocic about 5 yearsIt's so awesome to actually hear that it saved time for you! Thank you for the response!
-
stephen mc about 5 yearsThanks for this, I had incorrectly set the property to
status
. Hope this helps someone else. -
Tobi about 5 yearsthanks a lot this was exactly my problem. Actually I should have stumbled over a how to which checking the documentation but i didn't...
-
GhostBytes over 4 yearsThanks for this answer my dude. It's amazing how unintuitive all of this is, especially when going off of the Amazon guides themselves often lead to errors.
-
ljmocic over 4 yearsIt has been almost a year since I've answered this and the docs still didn't change. I'm glad that this was helpful to you!
-
Casey about 4 yearsWhat is X suppoosed to be?
-
Vaibhav Jain almost 4 yearsWorks perfectly. Not sure why they didn't included this in any documentation.
-
Justin Ohms over 3 yearssince my method execution tests were working it is frustrating that this was the answer amazon could maybe make it a little more obvious that you need to deploy
-
Diego Duarte over 3 yearsThis one solved all my problems, thank you very much :D
-
Gerardo Lima about 3 yearsI had exactly the same issue: I was confident my lambda was ok because I could run it without errors. It happens, then, the lambdas can run with little restriction, but API Gateway expects an specific shape for the returned object. For what I figured out, it expects the following minimal interface (I used TypeScript to describe it):
{statusCode: number, body: string}
\ -
Gerardo Lima about 3 years... by the way I haven't found where theses constraints are declared, so if anyone knows and can share it would be great!
-
Burf2000 over 2 yearsLegend!! fixed my issue
-
scubbo over 2 yearsIt's not easy to discover, but docs.aws.amazon.com/apigateway/latest/developerguide/…