message: "Internal server error" when try to access aws gateway api
Aws LambdaAws Api-GatewayAws Lambda Problem Overview
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)
Aws Lambda Solutions
Solution 1 - Aws Lambda
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 - Aws Lambda
Solution 3 - Aws Lambda
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 - Aws Lambda
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.
Solution 5 - Aws Lambda
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 6 - Aws Lambda
Be sure to pass the body across as a string in the response. If it's an object it will fail and give you the error you see. See here for more - http://www.awslessons.com/2017/lambda-api-gateway-internal-server-error/
Solution 7 - Aws Lambda
I had this problem, but in my case I was using API Gateway and java lambda function. When looking in cloudwatch there was no error, every things look fine. The problem happen when API Gateway is attempting to render the response. In my case, I had in my response object, the statusCode which was not an int while API gateway need it to be a int.
This
private final ResponseCode statusCode;
private final String body;
private final Map<String, String> headers;
private final boolean isBase64Encoded;
After I change statusCode to int
private final int statusCode;
This works
Solution 8 - Aws Lambda
In my case the problem was that I created API Gateway from Lambda. In this case no method was created on the API Gateway side of REST API.
Fixed by going to API Gateway, create method manually (GET), attach Lambda function, and (!) Deploy changes.
After this link started to work.
Solution 9 - Aws Lambda
This error indicates that there was a problem with the configuration of the API. If you enable CloudWatch Logs you can see more information: https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-cloudwatch-logs/
Solution 10 - Aws Lambda
In my case spelling of headers is wrong, it was 'header' in place of 'headers' so make sure you have the right JSON response body.
Solution 11 - Aws Lambda
callback(null, {
statusCode: 200,
body: JSON.stringify(message),
headers: {'Content-Type': 'application/json'}
});
This worked for me perfectly