"message": Error "Internal Server Error" with Lambda / API Gateway and iOS
I set up a lambda function and created some GET and POST methods inside the API gateway that seem to work fine when testing them inside a web app. Then I am trying to call functions inside an iOS app that is configured with a mobile hub. The functions also work inside the test object via the mobile hub is perfectly fine, however when I actually test the functions inside the application I get:
"message" : "Internal server error"
I know this is not how the error works, but I cannot figure out how to get a more detailed description of the error.
Any ideas?
source to share
This can happen because your Lambda function is not configured to return an HTTP status code.
Change
exports.handler = (event, context, callback) => {
callback(null, 'Hello from Lambda');
};
to
exports.handler = (event, context, callback) => {
callback(null, { statusCode: 200, body: 'Hello from Lambda' });
};
You need to fix the problem.
source to share
JSON.stringify()
solved my problem. response.body
should be in format String
, not how JSON
. Hope this helps.
exports.sendRes = (body, status = 200) => {
var response = {
statusCode: status,
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(body)
};
return response;
};
source to share
None of the above answers worked for me. I had a permission issue. This is how I solved it.
Context
This is my lambda function:
exports.handler = function(event, context, callback) {
callback(null, {
statusCode: '200',
body: JSON.stringify({ 'message': 'hello world' }),
headers: {
'Content-Type': 'application/json',
},
});
};
I used terraform to provide an API gateway and lambda. I used the example code provided by this blog post .
Diagnosis
In the lambda console, I have fired a test event on my lambda. Since my lambda was very simple, I used the hello world test template, named it and saved it. The test will return successfully.
I checked cloudwatch logs but couldn't find anything useful. I'm new to AWS so I wasn't sure if I needed to configure anything.
In the API gateway console, I fired a test event. i just added Content-Type:application/json
to the headers the events and ran the test. For some strange reason, the test results were returning on the right side of the browser, so I had to scroll to the right to see them.
I got this result: Execution failed due to configuration error: Invalid permissions on Lambda function
DECISION
I checked a basic terraform example for API gateway and lambda integration here and noticed I was missing a resource aws_lambda_permission
. This is required to give permission to the API gateway to call the lambda function.
For those not using terraform , here's a link to aws documentation on how to create the appropriate permissions.
source to share