Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to invoke lambda only on item expiry from Dynamo

I have set up Dynamo table and have enabled stream and also enabled TTL (timetolive) on one of the columns. I also have one lambda which will pull entry from Dynamo Stream.

Now either I add, delete, or edit, Or TTL gets expired - all this will cause the lambda invocation.

I am not interested in add or edit event, I only want stream to receive the deleted, TTL expired entries, is this possible?

Also, I can definitely put a check in my lambda code and process only when event type of "delete", but still lambda invocation for add, edit will take place regardless. Kindly guide

like image 976
Unbreakable Avatar asked Sep 01 '25 03:09

Unbreakable


2 Answers

You can't control DynamoDB stream, it will always post events for any changes happened to your table, however you can control the lambda invocation by adding property "FilterCriteria" in your "EventSourceMapping"

https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html

FilterCriteria: {"Filters": [{"Pattern": "{ \"userIdentity\": { \"type\": [ \"Service\" ] ,\"principalId\": [\"dynamodb.amazonaws.com\"] }}"}]}

using above filter your lambda will be only invoked if TTL expiry event posted in the DymnamoDB stream.

like image 98
m4c_4rthur Avatar answered Sep 02 '25 17:09

m4c_4rthur


Sadly, you can't make DynamoDB stream, to stream only deletion or expiration of items. Everything is streamed, and its up to your lambda function to filter the events of interests.

For TTL expired items, your function needs to check:


        "userIdentity":{
            "type":"Service",
            "principalId":"dynamodb.amazonaws.com"
        }

An alternative way, is to have second table, only with TTL markers. This could be useful, if your primary table experiences a lot of updates and modifications. This way, the stream on your second table would only invoke your function twice for each item, i.e. creation and TTL expiration, rather then for all the updates you are not interested in.

like image 22
Marcin Avatar answered Sep 02 '25 17:09

Marcin