Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 301
user1671308 Avatar asked Dec 06 '17 10:12

user1671308


People also ask

What is API gateway error?

Handling API Gateway 502 Error: Bad Gateway A 502 error code is related to the service your API Gateway integrates with. It means that API Gateway couldn't understand the response. For example, when you throw an error in a Lambda function or the resolved value has an invalid structure, it can lead to a 502 error.

How do I handle if API gateway is down?

If you want your micro-service Up all the time, you need to make a replica of your micro-service and place load-balancer against it. If one instance is down, the load balancer will automatically route the request to the instance which is alive.

How do I clear my API gateway cache?

To flush the API stage cache, you can choose the Flush entire cache button under the Cache Settings section in the Settings tab in a stage editor of the API Gateway console. The cache-flushing operation takes a couple of minutes, after which the cache status is AVAILABLE immediately after flushing.


3 Answers

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'}
});
like image 53
ljmocic Avatar answered Oct 12 '22 15:10

ljmocic


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

enter image description here

like image 29
ssuperczynski Avatar answered Oct 12 '22 14:10

ssuperczynski


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)
}
like image 9
Shubham Pandey Avatar answered Oct 12 '22 14:10

Shubham Pandey