This repository is deprecated and no longer actively maintained. It contains outdated code examples or practices that do not align with current MongoDB best practices. While the repository remains accessible for reference purposes, we strongly discourage its use in production environments. Users should be aware that this repository will not receive any further updates, bug fixes, or security patches. This code may expose you to security vulnerabilities, compatibility issues with current MongoDB versions, and potential performance problems. Any implementation based on this repository is at the user's own risk. For up-to-date resources, please refer to the MongoDB Developer Center.
This demonstrates how Realm HTTPS Endpoints can be invoked to perform CRUD operations on collections residing in MongoDB Atlas from the EventBridge API Endpoints option.
- clone this repo!
- create a trial MongoDB Atlas account https://www.mongodb.com/cloud/atlas/register
- Follow the steps here to create a new database cluster
- Configure database user, IP Whitelist and copy the connection string. Follow the steps here
Before we can write the code for our webhook, we first need to configure it. The Configure Service HTTPS Endpoints guide in the Realm documentation goes into more detail, but you will need to configure the following options:
*Authentication type must be set to system
*The HTTP method is POST
*Keep rest of the parameters deafult.
For our Webhook we need to write a function which:
- Receives a POST request from EventBridge
- Parses the JSON body from the request
- Iterates over the array
- Writes the object to MongoDB Atlas as a new document
- Returns the correct status code and JSON body to EventBridge in the response
https://github.com/mongodb-developer/EventbridgeAtlas/blob/main/InsertFunction.js does these steps. Copy this into Realm functions logic placeholder as shown in the below screenshot.
Copy the webhook URL in settings. Also copy sample curl command snapshot below:
Execute with below JSON Array in the body:
[{"field":"value1"},{"field":"value2"}]
Eg:
curl
-H "Content-Type: application/json"
-d '[{"field":"value1"},{"field":"value2"}]'
https://webhooks.mongodb-realm.com/api/client/v2.0/app/application-0-rosmq/service/test/incoming_webhook/webhook0
Test whether the data is getting added in the collection.
-
Select the target to invoke when an event matches your event pattern or when schedule is triggered. In this case target would be API Destination.
-
Configure API Destination Details with details of the webhook you already created in the previous step. EventBridge supports Basic, OAuth and API Key Authorizations
-
Name your connection. You will be able select a connection when you create an API destination.
-
Save and start pushing the data. You shoule be able to see it being uploaded in your Atlas cluster.