angel7170
asked on
AWS DynamoDB Streams
Hello,
I have a table named contactDetails that basically stores phonenumber for each contact. I have also enabled streams and have created a trigger using an lambda function to send an email whenever a new record is created in the table.
I do receive an email but I get them twice. It looks like there are more than one time the same record is modified for different attributes so it generates different stream events.
Lamdbafunction.txt
'use strict';
var AWS = require("aws-sdk");
var sns = new AWS.SNS();
exports.handler = (event, context, callback) => {
event.Records.forEach((rec ord) => {
console.log('Stream record: ', JSON.stringify(record, null, 2));
// console.log(event);
if (record.eventName == 'MODIFY') {
// console.log(event);
let tabledetails = JSON.parse(JSON.stringify( event.Reco rds[0].dyn amodb));
// console.log(tabledetails.N ewImage.ad dress.S);
let customerPhoneNumber = tabledetails.NewImage.cust omerPhoneN umber.S;
var params = {
Subject: 'A new voicemail received from' + customerPhoneNumber,
Message: 'A new voicemail received from Phone Number ' + customerPhoneNumber,
TopicArn: 'arn:aws:sns:xxxxxx:xxxxxx x:xxxxxxxs nstopic'
};
}
sns.publish(params, function(err, data) {
if (err) {
console.error("Unable to send message. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Results from sending message: ", JSON.stringify(data, null, 2));
}
});
});
callback(null, `Successfully processed ${event.Records.length} records.`);
};
What I need is even though there are multiple events generated for the same record, I only need one email to be send out based on the primary key(contactID) in the table.
Attached is the sample lambda function.
I have a table named contactDetails that basically stores phonenumber for each contact. I have also enabled streams and have created a trigger using an lambda function to send an email whenever a new record is created in the table.
I do receive an email but I get them twice. It looks like there are more than one time the same record is modified for different attributes so it generates different stream events.
Lamdbafunction.txt
'use strict';
var AWS = require("aws-sdk");
var sns = new AWS.SNS();
exports.handler = (event, context, callback) => {
event.Records.forEach((rec
console.log('Stream record: ', JSON.stringify(record, null, 2));
// console.log(event);
if (record.eventName == 'MODIFY') {
// console.log(event);
let tabledetails = JSON.parse(JSON.stringify(
// console.log(tabledetails.N
let customerPhoneNumber = tabledetails.NewImage.cust
var params = {
Subject: 'A new voicemail received from' + customerPhoneNumber,
Message: 'A new voicemail received from Phone Number ' + customerPhoneNumber,
TopicArn: 'arn:aws:sns:xxxxxx:xxxxxx
};
}
sns.publish(params, function(err, data) {
if (err) {
console.error("Unable to send message. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Results from sending message: ", JSON.stringify(data, null, 2));
}
});
});
callback(null, `Successfully processed ${event.Records.length} records.`);
};
What I need is even though there are multiple events generated for the same record, I only need one email to be send out based on the primary key(contactID) in the table.
Attached is the sample lambda function.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.