Ishan mahajan
Ishan mahajan

Reputation: 346

Find whether the value has been updated or inserted in Dynamodb?

I am using updatedItem() function which is either inserting or updating values in Dynamodb. If values are updating I want to fetch those items and invoke a new lambda function. How can I achieve this?

Upvotes: 2

Views: 1493

Answers (2)

Http417
Http417

Reputation: 89

Don't think previously accepted answer works. The return Attributes never returned the partition/sort keys, whether an item was updated or created.

What worked for me was to add ReturnValues: UPDATED_OLD. If the returned Attributes is undefined, then you know that an item was created.

Upvotes: 3

thomasmichaelwallace
thomasmichaelwallace

Reputation: 8484

The most direct approach would be to add ReturnValues: 'UPDATED_NEW' to the params you use for you updateItem() call.

You can then tell if you're inserted a new item because the returned Attributes will include your partition (and sort, if you've used a composite) key.

This is because you cannot change the key of an item, so if all you've done is update an item, then you would not have updated its key. But if you have created a new item, then you would have 'updated' its key.

However, if you want to react to items being updated in a dynamo table, you could alternatively use DynamoDB Streams (docs).

These streams allow you to trigger lambdas on the transactions on the dynamo table. This lambda could then filter the events for updates and react accordingly. The advantage of this architectural approach is it means your 'onUpdate' functionality will trigger if anything updates the table- not just the specific lambda.

Upvotes: 2

Related Questions