Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use DynamoDBStreamEvent Trigger Lambda when only update certain attribute

I use serverless framework to deploy cloudFormation. I want to trigger lambda to deleteObject on my S3 bucket after I update my DynamoDB Table with certain attribute, eg. my table has account, user, icon, I only want when I update icon, it trigger Lambda to delete my iconObject on S3 bucket.

As I read the documentation on AWS, it seems the eventName of dynamoDB stream Event only have three status, REMOVE, MODIFY, INSERT.

AWS Documentation » Amazon DynamoDB » API Reference » Data Types » Amazon DynamoDB Streams » Record Record

Could I do as below? But how do I know it update icon instead other attribute like account or user?

if (this._record.eventName === 'MODIFY' && this._record.NewImage!== this._record.OldImage ){
        return this._remove(this._record.dynamodb);
    }
like image 358
ckky1213 Avatar asked May 24 '18 22:05

ckky1213


People also ask

Can one lambda function have multiple triggers?

Your function can have multiple triggers. Each trigger acts as a client invoking your function independently. Each event that Lambda passes to your function has data from only one client or trigger. To process items from a stream or queue, you can create an event source mapping.

What is used to trigger Lambda functions for changes in DynamoDB?

With DynamoDB Streams, you can trigger a Lambda function to perform additional work each time a DynamoDB table is updated. Lambda reads records from the stream and invokes your function synchronously with an event that contains stream records.


1 Answers

You are on the right track. If the DynamoDB StreamViewType is set to NEW_AND_OLD_IMAGES then when record.eventName === 'MODIFY', record.dynamodb.NewImage will contain the updated version of the item and record.dynamodb.OldImage will contain what the item was before the update. You could then inspect the 2 objects and look for changes in the fields you are interested in.

like image 120
Brian Winant Avatar answered Nov 18 '22 22:11

Brian Winant