Link to home
Start Free TrialLog in
Avatar of angel7170
angel7170Flag for United States of America

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((record) => {
     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.Records[0].dynamodb));
            //    console.log(tabledetails.NewImage.address.S);
                let customerPhoneNumber = tabledetails.NewImage.customerPhoneNumber.S;
                   
             
           var params = {
                Subject: 'A new voicemail received from' + customerPhoneNumber,
                Message: 'A new voicemail received from Phone Number ' + customerPhoneNumber,
                TopicArn: 'arn:aws:sns:xxxxxx:xxxxxxx:xxxxxxxsnstopic'
        };
           
            }
                   
          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 TRIAL
Members 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.