1
votes

I have enable DynamoDB Streaming of my table and two lambda's are associated with it. Both trigger's whenever there is a change in DynamoDB table through dynamo triggers.

Issue: Both Lambda's insert data accordingly into two RDS tables. Like Lambda-one insert into table-one and Lambda-two insert into table-two. Table-one primary key is the foreign key in Table-two.

So whenever both lambda's triggered the lambda-two complete the execution firstly due to which it show's the foreign key constraint error because the lambda-two try to insert the data into table-two but at that time table-one doesn't have the primary key yet.

So my question is that there is any way to order the triggering of lambda through DynamoDB?

2
you can use sqs to send a notifications from lambda 1 once it update successfully in table. Then only second lambda should update it.AjayLohani
Isn't there any dynamoDB feature to solve this problem or I should move into something like SQS?Abdul Moeez
if i understood correctly - the execution of lamda 2 depends on the excution of lambda 1. Another option is to combine both lambdas in to one - which inserts in both RDS tables. Another option could be - first lambda triggers/calls the 2nd lambdaEncho Solakov
@EnchoSolakov Thanks for the approaches you mentioned, Both seems good to me and I working on that to get the best solution.Abdul Moeez

2 Answers

2
votes

AWS Step Functions is the perfect AWS feature to orchestrate Lambda functions and has specifically been built for these kinds of workflows. It ensures sequential execution of subsequent functions and also provides ways to handle errors, e.g. to rollback and maintain consistency if some functions have failed.

Here's a step-by-step walkthrough: Create a Serverless Workflow with AWS Step Functions and AWS Lambda

Having that said, if you need to maintain referential integrity in your database, it would probably be much simpler to wrap the creation of both records into a transaction using a single Lambda function. In this case the database itself will take care of maintinaing consistency. There is no reason to separate this into two functions if it is supposed to be an atomic write operation.

1
votes

Did implemented something like this internally. But instead of adding complexity via AWS Step Function, we used AWS Lambda Destinations. There are 4 types of Destinations supported

  1. SQS Queue ( can be FIFO in this case)
  2. SNS Topic
  3. Event Bridge
  4. Another Lambda Function itself.

Saves plenty of movement around the services and integration is pretty easy to achieve.