Search code examples
aws-lambdaamazon-dynamodbamazon-dynamodb-streams

When should I use a DynamoDB trigger over calling the Lambda with another?


I currently have one AWS Lambda function that is updating a DynamoDB table, and I need another Lambda function that needs to run after the data is updated. Is there any benefit to using a DynamoDB trigger in this case instead of invoking the second Lambda using the first one?

It looks like the programmatic invocation would give me more control over when the Lambda is called (ie. I could wait for several updates to occur before calling), and reading from a DynamoDB Stream costs money while simply invoking the Lambda does not.

So, is there a benefit to using a trigger here? Or would I be better off invoking the Lambda myself?


Solution

  • DynamoDB Stream seems to be the better practice because:

    • you delegate the responsibility of invoking the post-processor function from your writer-Lambda. Makes writer more simple (aka faster),
    • you simplify connecting new external writers to the same Table, otherwise you have to implement the logic to call post-processors in all of them as well,
    • you guarantee that all data is post-processed (even if somebody added a new item in the web-interface of DynamoDB. :)
    • moneywise, the execution time you will spend to send invoke() operation from writer Lambda will likely cover the costs of a stream.
    • unless you use DynamoDB transactions your data may still be not yet available for post-processor if you call him from writer too soon. If your business logic doesn't need transactions then using them just to cover this problem = extra time/cost.

    P.S. You can batch from the DynamoDB stream of course out of the box with simple setting. You are not obliged to invoke post-processor for every write operation.