Send Event Notifications from MongoDB Trigger to AWS Eventbridge
Tags: aws, mongodb, cloud, sns
Published: 2023-01-12 06:02:56

**Introduction**

What if you want to record any action performed in your database and send an SMS notification to your Smart device? You'll see this great approach that I've performed just for fun :).

**MongoDB Side**

**Create MongoDB Trigger**

Go to your MongoDB Atlas Cluster and Add a new Trigger

![MongoDBAddTrigger](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qqtaczc7dghuqdub2m8r.png)

Select **Database** as Trigger Type, enter a name for your trigger and link a data source to itP:

![MongoDBTrigger01](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idxu6ughzt9xnk3hq05z.png)

Select your Cluster, Database, Collection Names and the operations you want to fire up the trigger:

![MongoDBTrigger02](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ydf41bdlv6qur7hm7b12.png)

Select **EventBridge** as Event Type, Enter your AWS Account ID and Region:

![MongoDBTrigger03](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wj9ap7rb6nfhkts9ch6t.png)

**AWS Side**

Let's go to Amazon EventBridge>Partner event sources and you'll see the Event Source added after creating the trigger within MongoDB, now we have to associate this event

![EventBridge001](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ush95m5uycynsojwru00.png)

You can use the default options while associating this event source:

![EventBridge002](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/so7p2prt4n9vfs6a9m7l.png)

You'll see the event in **Active** state after this process:

![EventBridge007](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ospcqm324dzbdy26k8v.png)

Now, let's go to **Rules**, Select the Event Bus already created from the drown dop list and create a rule:

![EventBridge008](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bqvhqbn5lgk9cjttcen1.png)

Let's put a name to this rule and use the default options in section 1:

![EventBridge009](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ebaluu77t72y8238oasb.png)

Go to Event Pattern in Section 2 and select the options from the image below:

![EventBridge010](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69n00i307o7z6v5dl43u.png)

In section 3, use **AWS Service** as target type, in target you can use any option you want but, in this example, I'll be using SNS Topic:

![EventBridge011](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5uob9yb1u6md7862j2b.png)

Open a new tab from your browser for your AWS Console and got to SNS (Simple Notification Service):

![SNS001](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j9r00p4c9pslhwjryqhl.png)

Enter a display for this SNS and leave the other options as default:

![SNS002](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7i6gro11hjwl19m9c62u.png)

Now, let's create a subscription:

![SNS003](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53d71zpkc7woi3iw1b7i.png)

Select your topic ARN already created SMS as protocol and select your phone number from the list.

![SNS004](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njwauwizv95zkg8jnl89.png)

After selecting your target and SNS Topic, you can click on Next and Create your Rule:

![EventBridge012](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tt18o6n96hcu009lbgbj.png)

After creating your rule, we can go back to our MongoDB Atlas Cluster and perform a test by removing a document:

![MongoDB001](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c9us2pzb2l94w4zwc45a.png)

I've got my notification via SMS that a document was deleted, this is working!

![SMS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtghv1tmfop4f8kkpt97.jpg)

I hope you find this information helpful, if you have any issues, comments or feedback shoot me a comment.

Have a good one community!