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)
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.
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.
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.
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'}
});
Don't forget to click Deploy API under AWS API Gateway. Without it, change doesn't work.
For accessing dynamodb through lambda function from api gateway it needs:
Create a role in AWS console that have access to dynamodb operations.
Create a lambda function and assign the above created role to your lambda function.
Create a api from API gateway in AWS management console and allow it to access to your lambda function.
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With