I have created a Lambda function which puts an Item in the DynamoDB table using Node.js 10.x. The Lambda function has been integrated with SQS Queue via trigger. When I put a message in the Queue the Lambda is invoked, but is throwing an error while inserting an Item in the DynamoDB table.
Message -> SQS -> Lambda -> DynamoDB
I am passing the TableName and the Item as the input to the DynamoDB put method. But, the CloudWatch Logs says that TableName and Item parameters are missing. Below are the code snippets and the screenshots.
I did spend a good amount of time, but not able to get around the problem. Could someone help me?
Lambda Code
console.log('Loading function');
var AWS = require('aws-sdk');
var dynamo = new AWS.DynamoDB.DocumentClient();
exports.handler = function(event, context, callback) {
console.log('Received event:', JSON.stringify(event, null, 2));
event.Records.forEach(record => {
const { body } = record;
console.log("Input to DynamoAPI :", body);
dynamo.put(body, callback);
});
return {};
}
Message that goes into the Queue
{
"TableName": "user-transaction",
"Item" : {
"transaction-id": 1
}
}
Error message from the CloudWatch
I did not try this out, but it looks to me like there is a missing JSON.parse()
around the body variable in the dynamo put invocation.
From what I remember, SQS stringifies the body and it is not parsed automatically. The following answer which I found on SO sums this up perfectly: [1].