I have created a simple lambda function in AWS that returns list from DynamoDB. I have also created API Gateway to trigger the lambda function. The function works fine when I test in AWS console. But I always get 502 bad gateway below error when I test this function in Postman. ({ "message": "Internal server error" }
Below is the function in node.js:
const doc = require('dynamodb-doc');
const dynamo = new doc.DynamoDB();
/**
* Provide an event that contains the following keys:
*
* - operation: one of the operations in the switch statement below
* - tableName: required for operations that interact with DynamoDB
* - payload: a parameter to pass to the operation being performed
*/
exports.handler = async (event) => {
const operation = event.operation;
const payload = event.payload;
if (event.tableName) {
payload.TableName = event.tableName;
}
switch (operation) {
case 'create':
return await dynamo.putItem(payload).promise();
case 'read':
return await dynamo.getItem(payload).promise();
case 'update':
return await dynamo.updateItem(payload).promise();
case 'delete':
return await dynamo.deleteItem(payload).promise();
case 'list':
return await dynamo.scan(payload).promise();
case 'echo':
return payload;
case 'ping':
return 'pong';
default:
throw new Error(`Unrecognized operation "${operation}"`);
}
};
Below is generated API Gateway Endpoint details.
API endpoint: https://iabzqisam7.execute-api.us-east-1.amazonaws.com/test/moneyAppDynamoDBOperationsLambda
Authorization: NONE
Method: ANY
Resource path: /moneyAppDynamoDBOperationsLambda
Stage: test
Here is how I am trying to test API using Postman:
Postman URL(Get) : https://iabzqisam7.execute-api.us-east-1.amazonaws.com/test/moneyAppDynamoDBOperationsLambda
Headers: Key: content-type, Value: application/json
Body (raw) :
{
"operation": "list",
"tableName": "Advertiser",
"payload": {
"TableName": "Advertiser"
}
}
It works perfectly fine within AWS console.
Any idea why I am keep getting 502 bad gateway error while calling API Gateway from Postman ?