message: "Internal server error" when try to access aws gateway api

59,411

Solution 1

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

Don't forget to click Deploy API under AWS API Gateway. Without it, change doesn't work.

enter image description here

Solution 3

For accessing dynamodb through lambda function from api gateway it needs:

  1. Create a role in AWS console that have access to dynamodb operations.

  2. Create a lambda function and assign the above created role to your lambda function.

  3. Create a api from API gateway in AWS management console and allow it to access to your lambda function.

  4. 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

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 5

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.

Share:
59,411
Admin
Author by

Admin

Updated on July 08, 2022

Comments

  • Admin
    Admin almost 2 years

    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)

  • visrahane
    visrahane almost 6 years
    check comments - github.com/aws-samples/aws-serverless-workshops/blob/master/‌​… The callback has to have a format
  • ljmocic
    ljmocic almost 6 years
    I forgot to mention that this is for Python. Basically, the same thing is for node.js, you just need to handle the callback.
  • ConorLuddy
    ConorLuddy over 5 years
    I ended up here with the same issue, but from the Node perspective. I needed to JSON.stringify(message) the body before passing it to APIGW.
  • Amax
    Amax about 5 years
    Dude, you saved my day!
  • ljmocic
    ljmocic about 5 years
    It's so awesome to actually hear that it saved time for you! Thank you for the response!
  • stephen mc
    stephen mc about 5 years
    Thanks for this, I had incorrectly set the property to status. Hope this helps someone else.
  • Tobi
    Tobi about 5 years
    thanks a lot this was exactly my problem. Actually I should have stumbled over a how to which checking the documentation but i didn't...
  • GhostBytes
    GhostBytes over 4 years
    Thanks for this answer my dude. It's amazing how unintuitive all of this is, especially when going off of the Amazon guides themselves often lead to errors.
  • ljmocic
    ljmocic over 4 years
    It has been almost a year since I've answered this and the docs still didn't change. I'm glad that this was helpful to you!
  • Casey
    Casey about 4 years
    What is X suppoosed to be?
  • Vaibhav Jain
    Vaibhav Jain almost 4 years
    Works perfectly. Not sure why they didn't included this in any documentation.
  • Justin Ohms
    Justin Ohms over 3 years
    since my method execution tests were working it is frustrating that this was the answer amazon could maybe make it a little more obvious that you need to deploy
  • Diego Duarte
    Diego Duarte over 3 years
    This one solved all my problems, thank you very much :D
  • Gerardo Lima
    Gerardo Lima about 3 years
    I had exactly the same issue: I was confident my lambda was ok because I could run it without errors. It happens, then, the lambdas can run with little restriction, but API Gateway expects an specific shape for the returned object. For what I figured out, it expects the following minimal interface (I used TypeScript to describe it): {statusCode: number, body: string}\
  • Gerardo Lima
    Gerardo Lima about 3 years
    ... by the way I haven't found where theses constraints are declared, so if anyone knows and can share it would be great!
  • Burf2000
    Burf2000 over 2 years
    Legend!! fixed my issue
  • scubbo
    scubbo over 2 years