message: "Internal server error" when try to access aws gateway api
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'}
});
Don't forget to click Deploy API under AWS API Gateway. Without it, change doesn't work.
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)
}
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.
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;
};