Integrate Your Realm App with Amazon EventBridge
Rate this tutorial
This post was developed with the help of AWS.
makes it easy to develop compelling mobile applications backed by a serverless MongoDB Realm back end and the . You can enrich those applications by integrating with AWS's broad ecosystem of services. In this article, we'll show you how to configure Realm and AWS to turn Atlas database changes into events – all without adding a single line of code. Once in EventBridge, you can route events to other services which can act on them.
This post details how to add a new feature to the RChat application – forwarding messages to a Slack channel.
is a serverless event bus that makes it easier to connect applications together using data from your applications, integrated software as a service (SaaS) applications, and AWS services. It does so by delivering a stream of real-time data from various event sources. You can set up routing rules to send data to targets like and build loosely coupled application architectures that react in near-real time to data sources.
If you want to build and run the app for yourself, this is what you'll need:
If you're not interested in running the mobile app (or don't have access to a Mac), the article includes instructions on manually adding a document that will trigger an event being sent to EventBridge.
This walkthrough shows you how to:
Wait until the Atlas cluster is running.
From the Atlas UI, click on the Realm logo and you will see the RChat app. Open it and make a note of the Realm "App Id":
The Slack app simply allows us to send a message to a Slack channel.
Click on the button to create a new Slack app, name it "RChat," and select one of your Slack workspaces. (If using your company's account, you may want or need to create a new workspace.)
Give your app a short description and then click "Save Changes."
After creating your Slack app, select the "OAuth & Permissions" link. Scroll down to "Bot Token Scopes" and add the
Click on "Install to Workspace" and then "Allow."
Take a note of the new "Bot User OAuth Access Token."
From your Slack client, create a new channel named "rchat-notifications." Invite your Slack app bot to the channel (i.e., send a message from the channel to "@RChat Messenger" or whatever Slack name you gave to your app):
You now need to find its channel ID from a terminal window (substituting in your Slack OAuth access token):
In the results, you'll find an entry for your new "rchat-notifications" channel. Take a note of its
id; it will be stored in AWS Secrets Manager and then used from the Lambda function when calling the Slack SDK:
EventBridge supports MongoDB as a partner event source; this makes it very easy to receive change events from Realm Triggers.
Take a note of your AWS account ID.
Return to the Realm UI navigate to "Triggers" and click "Add a trigger." Configure the trigger as shown here:
Rather than sticking with the default "Function" event type (which is Realm Function, not to be confused with Lambda), select "EventBridge," add your AWS Account ID from the previous section, and click "Save" followed by "REVIEW & DEPLOY":
Return to the AWS "Partner event sources" page, select the new source, and click "Associate with event bus":
On the next screen, leave the "Resource-based policy" empty.
Returning to the "Event buses" page, you'll find the new MongoDB partner bus.
We need a new Lambda function to be invoked on any MongoDB change events added to the event bus. That function will use the Slack API to send messages to our channel. The Lambda function must provide the OAuth token and channel ID to use the Slack SDK. Rather than storing that private information in the function, it's more secure to hold them in AWS Secrets Manager.
Click through the wizard, and apart from assigning a unique name to the secret (and take a note of it as it's needed when configuring the Lambda function), leave the other fields as they are. Take a note of the ARN for the new secret as it's required when configuring the Lambda function.
After creating the Lambda function, navigate to the "Permissions" tab and click on the "Execution role" role name. On the new page, click on the "Policy name" and then "Edit policy."
Click "Add additional permissions" and select the "Secrets Manager" service:
Select the "ListSecrets" action. This permission allows the Lambda function to see what secrets are available, but not to read our specific Slack secret. To remedy that, click "Add additional permissions" again. Once more, select the "Secrets Manager" service, but this time select the "Read" access level and specify your secret's ARN in the resources section:
Review and save the new permissions.
Returning to the Lambda function, select the "Configuration" tab and add an environment variable to set the "secretName" to the name you chose when creating the secret (the function will use this to access Secret Manager):
It can take some time for the function to fetch the secret for the first time, so set the timeout to 30 seconds in the "Basic settings" section.
Finally, we can write the actual Lambda function.
From a terminal, bootstrap the function definition:
In the same lambda directory, create a file called
There are a couple of things to call out in that code.
This is how the Slack credentials are fetched from Secret Manager:
eventis passed in as a parameter, and the function retrieves the original MongoDB document's contents from
blocksis optional, and if omitted, the SDK uses text as the body of the Slack message.
Package up the Lambda function:
From the Lambda console, upload the zip file and then deploy:
The Lambda function is now complete, and the next section will start routing events from the EventBridge partner message bus to it.
The final step to integrate our Realm app with the new Lambda function is to have that function consume the events from the event bus. We do that by adding a new EventBridge rule.
The "Name" can be anything. You should use an "Event pattern," set "Pre-defined pattern by service," search for "Service partner" "MongoDB," and leave the "Event pattern" as is. This rule matches all bus events linked to our AWS account (i.e., it will cover everything sent from our Realm function):
Select the new Lambda function as the target and click "Create":
After creating the back end Realm app, open the RChat iOS app in Xcode:
rchat-xxxxxwith your Realm App Id:
Select your target device (a connected iPhone/iPad or one of the built-in simulators) and build and run the app with
To test a chat app, you need at least two users and two instances of the chat app running.
From Xcode, run (
⌘r) the RChat app in one simulator, and then again in a second simulator after changing the target device. On each device, register a new user. As one user, create a new chat room (inviting the second user). Send messages to the chat room from either user, and observe that message also appearing in Slack:
Suppose you're not interested in using the iOS app or don't have access to a Mac. In that case, you can take a shortcut by manually adding documents to the
ChatMessagecollection within the
RChatdatabase. Do this from the "Collections" tab in the Atlas UI. Click on "INSERT DOCUMENT" and then ensure that you include fields for "author" and "text":
This post stepped through how to get your data changes from MongoDB into your AWS ecosystem with no new code needed. Once your EventBridge bus has received the change events, you can route them to one or more services. Here we took a common approach by sending them to a Lambda function which then has the freedom to import external libraries and work with other AWS or external services.
News & Announcements
Realm Cocoa 5.0 - Multithreading Support with Integration for SwiftUI & Combine
May 16, 2022