If you are a beginner in development then have you ever thought that what will you do if you want to run a service when any change happens in your MongoDB database?
Here what you want is that you want to trigger an event if any changes occur in your database. So to achieve this requirement you will have to use MongoDB triggers along with an AWS service named Eventbridge.
Mongodb Triggers
It allows you to execute a server-side logic whenever either a new document is added or an older document is updated or deleted from the linked MongoDB cluster.
AWS Eventbridge
Amazon EventBridge is a serverless event bus service that makes it easy to connect your applications with data from a variety of sources. EventBridge delivers a stream of real-time data from your applications, software-as-a-service (SaaS) applications, and AWS services and routes that data to targets such as AWS Lambda. You can set up routing rules to determine where to send your data to build application architectures that react in real-time to all of your data sources. EventBridge enables you to build event-driven architectures that are loosely coupled and distributed. There are a lot of SaaS providers available and MongoDB is just one of them.
To make a connection, we need to follow some steps on both MongoDB and Eventbridge.
Mongodb atlas
1. Go to the MongoDB atlas console and select triggers from the left-hand side menu.
2. Click on Add Trigger and this will land you on a page that will show you the options to create the trigger. Then select database trigger as the trigger type and fill rest of the details. When you select your cluster in the link data source section, it will create a realm behind the scene. After that, you will have to select the database and the collection on which you want to execute the server-side logic and what type of operation.
3. In the function section, we need to select Eventbridge and we will be asked to enter our AWS account id and the region in which our account is.
4. Now an optional part will come which is match expression and projection. We will only use it if we would like to be notified in a specific scenario and specific fields. i.e.: We want to be get notified only if the user’s mark is less than 40.
5. Now, Click on save. It will create a new trigger and On the AWS, a new event bus will be created with the same name as of partner event source “aws.partner/mongodb.com/stitch.trigger/{triggerId}” where triggerId is returned from MongoDB.
Now go to the AWS Eventbridge console
1. Click on Partner event sources.
2. Select the record and click on Associate with event bus.
3. Once you click on associate, it will create a new custom bus for you. Click on Event Bus in the left-hand section to verify it under the custom event bus section.
4. Next click on Rules and select the event bus created above from the drop-down and click Create Rule.
5. Select the Pre-defined pattern by service inside the Event matching pattern and select service partners as the service providers and MongoDB as the service name.
6. Select the lambda function as the target and select the lambda function name from the dropdown menu.
7. Click Create.
And you are all done. Congratulation!!!