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?
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.