17

I've set up a lambda function and created some GET and POST methods inside the API Gateway which seem to work fine when testing them inside the web application. I am then trying to call the functions inside an iOS application which is set up using the mobile hub. The functions also work inside the testing facility via the mobile hub perfectly fine, however when I actually test the functions inside the app I get:

"message" : "Internal server error"

I know the error is not much to work from, but I can't figure out a way to get a more detailed error description.

Any ideas?

user3599895
  • 275
  • 1
  • 2
  • 13
  • 1
    That error is API Gateway telling you that the Lambda function didn't respond properly. You need to look into the CloudWatch Logs for the Lambda function to see what the actual error is. – Mark B Jul 15 '17 at 17:03
  • I've fixed the errors that came up in CloudWatch Logs, now there are no errors but I'm still getting the same error. – user3599895 Jul 15 '17 at 18:21
  • When I change the query string parameters to just "?" the message changes to ""message": null" – user3599895 Jul 15 '17 at 18:24
  • 1
    It's really not enough to go on. But have you tried testing it through the API Gateway console? – Mark Jul 15 '17 at 18:56
  • Have you tried using a Proxy integration between API Getway and Lambda? http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html – gkrizek Jul 16 '17 at 06:46
  • Thanks so much, got it to work. The proxy integration did the trick. – user3599895 Jul 16 '17 at 10:57

9 Answers9

33

This may happen because your Lambda function is not set to return a HTTP status code.

Changing from

exports.handler = (event, context, callback) => {
    callback(null, 'Hello from Lambda');
};

to

exports.handler = (event, context, callback) => {
    callback(null, { statusCode: 200, body: 'Hello from Lambda' });
};

Should fix the issue.

deathangel908
  • 8,601
  • 8
  • 47
  • 81
Ricardo Mayerhofer
  • 2,121
  • 20
  • 22
  • Can you explain me what does ' { statusCode: 200, body: 'Hello from Lambda' } ' mean ? What if I replace this part of success message in callback to display some variable like- let a= 10; and then callback(null, a); – Pranjal Gupta Feb 22 '18 at 13:53
  • This part is the Lambda function invocation response. If you replace it by "let a", then "a" will be the function response. – Ricardo Mayerhofer Mar 06 '18 at 06:29
  • This return format only matters when using the API Gateway-Lambda combination in proxy mode. If you do not check the proxy option, then you can return whatever you’d like, the API gateway will forward it on. – zhibirc May 02 '18 at 15:21
5

The JSON.stringify() solved my issue. The response.body needs to be in String format and not as JSON. I hope this helps.

exports.sendRes = (body, status = 200) => {
    var response = {
        statusCode: status,
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(body)
    };
    return response;
};
Nikhil Manapure
  • 3,748
  • 2
  • 30
  • 55
  • 1
    It took half of my day and after seeing your answer, I remembered I had previously had the same issue got resolved by the same solution. Thanks a lot. I was using python btw and str(body) did the work. – Hashir Baig Jul 05 '19 at 10:25
3

I had the same issue with the following code:

exports.handler = async event => {
  console.log("hello world");
  return {
    statusCode: 200,
    body: event
  };
};

To fix all I had to do was JSON.stringify() the body.

exports.handler = async event => {
  console.log("hello world");
  return {
    statusCode: 200,
    body: JSON.stringify(event), // <-- here
  };
};
Karl Taylor
  • 4,839
  • 3
  • 34
  • 62
1

I had this problem until I click in "Deploy API" under the "Actions" button.

Matheus Araujo
  • 5,551
  • 2
  • 22
  • 23
0

The other possible reason could be the payload/request/response limits on API Gateway (10MB) and/or Lambda (6MB)

user2313617
  • 81
  • 1
  • 4
0

None of the above answers worked for me. I was having a permission issue. Below 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 provision api gateway and lambda. I used the example code provided by this blog post.

Diagnosis

In the lambda console I ran a test event on my lambda. As my lambda was super basic I used the hello world test template, named, and saved it. The test return success.

I checked cloudwatch logs, but couldn't find anything of use. I'm new to AWS so wasn't sure if I had to set anything up.

In the api gateway console I ran a test event. I just added Content-Type:application/json to the headers of the event and ran the test. For whatever weird reason the test results returned on the right side of the browser so had to scroll to the right to see them.

I got this result: Execution failed due to configuration error: Invalid permissions on Lambda function

SOLUTION

I checked the basic terraform example for api gateway and lambda integration here and noticed I was missing the aws_lambda_permission resource. This is needed to give permission to api gateway to invoke the lambda function.

For those that aren't using terraform here is a link to the aws docs on how to create the appropriate permissions.

codeinaire
  • 1,682
  • 1
  • 13
  • 26
0

please try to

  1. Give execute lambda permission API Gateway
  2. tick checkbox : Use Lambda Proxy integration
  3. Handle null pointer for query string, headers & body.
Sarang
  • 422
  • 5
  • 11
0

I solved the issue by adding "isBase64Encoded": False/True to my lambda response

results = {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps(res),
"isBase64Encoded": False
}
0

In my case, the issue resolved while adding the integration Response and redeploying API API Integration Response