2
votes

I was trying to implement a Lambda function based on as a trigger to a MODIFY event on a DynamoDB table. This trigger was to be used to further modify the table based on calculations performed on the new entries. But since this would also be a 'MODIFY' event it would trigger the lambda again, which might start of infinite triggers of lambda functions.

How do stop the lambda from getting triggered infinitely?

var AWS = require('aws-sdk');
// extra
const docClient = new AWS.DynamoDB.DocumentClient();

exports.handler = async (event , context , callback) => {
    // TODO implement
    
    event.Records.forEach((record) => {
        console.log('Stream record: ', JSON.stringify(record, null, 2));
        // logic to find insert type / modify type 
        if(record.eventName == 'MODIFY'){
             // extract student id, and marks from event itself (save I/O)
            console.log("Printing new image values",JSON.stringify(record.dynamodb.NewImage,null,2));
             // modify the same record once new value is calculated
             
             //############# method to modify /update dynamo db  #################
             
             //############# end code ###############
        }
        
    });
    
    console.log(event);
    const response = {
        statusCode: 200,
        body: JSON.stringify(event),
    };
    
    
    
    return response;
};

Here the code i am using in lambda which is triggered by a dynamodb event

1

1 Answers

1
votes

A simple solution is to signify Lambda when not to alter the record anymore

This solution is dependent on your actual use case and if you can save a certain condition that the current record should not be processed anymore

Sometimes it can be a simple property such as

  • Processed = true
  • LastProcessTime = 1602232746

saved directly into the record

Checkout other discussions around this also

https://www.reddit.com/r/aws/comments/7cvqsy/is_it_an_antipattern_to_write_to_dynamodb_from_a/

DynamoDB streams - write data back into table