6

I am new to AWS, I am getting {"message": "Internal server error"} while running the Lambda function with API Gateway in Postman.

I have checked the CloudWatchLogs, there is no error showing in the logs. But Postman returning {"message": "Internal server error"} this error.

Deepak Sriram
  • 109
  • 1
  • 1
  • 5
  • You cannot run Lambda in Postman. You run request to specific resources there. Internal server error assumes handling of your request caused system error somewhere. It can be gateway or lambda itself. See logs. – nickolay.laptev Nov 21 '19 at 14:08
  • Thanks for replying. Yea i had checked the logs, it returns the `Status Code 200` and the data also inserted in the Database. Response from postman is `502 Bad gateway`. Does any other ways available to get response of `Status Code 200` from postman? – Deepak Sriram Nov 21 '19 at 14:15
  • @DeepakSriram could you provide a snippet of your code ? without that it would be hard to guess on what is going wrong and where. – sash Nov 21 '19 at 14:38
  • 2
    Hey Deepak upvoting your answer because some mean person downvoted, when asking questions please try and include relevant code snippets - added a comment to bcosta12 answer below! Welcome to Stackoverflow! – Mrk Fldig Nov 21 '19 at 18:40
  • Thanks @MrkFldig for upvoting my post. Since i am new to stackoverflow, i did not know about it and sorry for not posting my full code. – Deepak Sriram Nov 22 '19 at 04:45
  • You're welcome, I reckon bcosta12 has the answer for you but yeah post the code as other people have rightly said it could be a few things. – Mrk Fldig Nov 22 '19 at 09:20
  • hi @Deepak Sriram, if my answer helps you, please accept It. I upvote you too :) – bcosta12 Nov 22 '19 at 14:45
  • 1
    Thanks @bcosta12 for your answer and it has executed correctly – Deepak Sriram Nov 23 '19 at 10:52

4 Answers4

9

It happens when you don't return the correct API Gateway format.

Try to return this in your Lambda:

def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "body": "{'Test': 'Test'}",
        "headers": {
            'Content-Type': 'text/html',
        }
    }
Vinagy
  • 133
  • 2
  • 11
bcosta12
  • 2,364
  • 16
  • 28
2

According to the log message you provide, it looks like the log from your Lambda function. I recommend you to enable logging feature on API Gateway side or you can use the test invoke feature on API Gateway console. They both are able to help you to debug your API.

Here is the common issues which might be able to help you diagnose the issue. 1. It doesn't have a right permission to allow API Gateway invoke your Lambda function. 2. It is set as AWS Service Proxy to your Lambda function, the response from your Lambda function doesn't return the response in the proper format.

Ref: https://forums.aws.amazon.com/thread.jspa?messageID=916452

404
  • 8,022
  • 2
  • 27
  • 47
Hassan Murtaza
  • 963
  • 6
  • 15
0

The error can be caused by incorrect quotation marks.

A. Examples that produce the same error in Postman tests:

 1. def lambda_handler(event, context):
      return {
        'statusCode': 200,
        "body": {"one": 1000}
      }
 2. def lambda_handler(event, context):
      return {
        'statusCode': 200,
        "body": "{"one": 1000}"
      }

B. Examples that do not produce the error:

3. def lambda_handler(event, context):
     return {
       'statusCode': 200,
       "body": "{'one': 1000}"
     }


4. def lambda_handler(event, context):
     return {
       'statusCode': 200,
       "body": '{"one": 1000}'
     }

So, the type of quotation marks used after "body": is the reason for the error in this case. Note that while the Amazon lambda console does not produce an error for example 1., Postman says { "message": "Internal server error" }

Cherif Diallo
  • 311
  • 2
  • 3
0

Resolved it by adding isBase64EncodedFlag to my lambda response

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