> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!--
Tab options on this page. Append to the .md URL to filter:
  ?tabs=<id,...>   select specific tabs (e.g. ?tabs=nodejs,shell)
  ?allTabs=true    include every tab
  (no param)       default: one tab per tabset

Available tabs:
  other tabs: ui, cli, api
-->

# Send Trigger Events to AWS EventBridge

MongoDB offers an [AWS Eventbridge](https://aws.amazon.com/eventbridge/) partner event source that lets you send Atlas Trigger events to an event bus instead of calling an Atlas Function. You can configure any Trigger type to send events to EventBridge. Database Triggers also support custom error handling, to reduce Trigger suspensions due to non-critical errors.

All you need to send Trigger events to EventBridge is an AWS account ID. This guide walks you through finding your account ID, configuring the Trigger, associating the Trigger event source with an event bus, and setting up custom error handling.

**Note: Official AWS Partner Event Source Guide**

This guide is based on Amazon's [Receiving Events from a SaaS Partner](https://docs.aws.amazon.com//eventbridge/latest/userguide/create-partner-event-bus.html) documentation.

## Procedure

**Note:**

Individual entries for an EventBridge Trigger event must be smaller than 1 MB.

Learn how to reduce the size of your `PutEvents` entry in the [Performance Optimization](https://www.mongodb.com/docs/atlas/atlas-ui/triggers/aws-eventbridge.md#std-label-atlas-send-aws-eventbridge-performance-optimization) section.

1. Set Up the MongoDB Partner Event Source

   To send Trigger events to AWS EventBridge, you need the AWS
   account ID of the account that should receive the events.

   Open the [Amazon EventBridge console](https://console.aws.amazon.com/events/) and click Partner event sources in the navigation menu.

   Search for the MongoDB partner event source and then click Set up.

   From the MongoDB partner event source page, click Copy to copy your AWS account ID to the clipboard.

2. Configure the Trigger

   After you have the AWS account ID, you can configure a [Database Trigger](https://www.mongodb.com/docs/atlas/atlas-ui/triggers/database-triggers.md#std-label-atlas-database-trigger) or [Scheduled Trigger](https://www.mongodb.com/docs/atlas/atlas-ui/triggers/scheduled-triggers.md#std-label-atlas-scheduled-triggers) to send events to EventBridge.

   You can configure the Trigger in the Atlas UI or using the App Services CLI.

   ### Atlas UI

   In the Atlas UI, create and configure a new [Database Trigger](https://www.mongodb.com/docs/atlas/atlas-ui/triggers/database-triggers.md#std-label-atlas-database-trigger) or [Scheduled Trigger](https://www.mongodb.com/docs/atlas/atlas-ui/triggers/scheduled-triggers.md#std-label-atlas-scheduled-triggers) with the following settings:

   Select EventBridge as the event type.

   Paste the AWS Account ID that you copied from EventBridge.

   Select an AWS Region to send the Trigger events to.

   **Note: Supported AWS Regions**

   For a full list of supported AWS regions, refer to Amazon's [Receiving Events from a SaaS Partner](https://docs.aws.amazon.com//eventbridge/latest/userguide/create-partner-event-bus.html) guide.

   *(Optional for Database Triggers Only)* Configure a Function to handle Trigger errors.

   For more details, refer to the [Custom Error Handling](https://www.mongodb.com/docs/atlas/atlas-ui/triggers/aws-eventbridge.md#std-label-atlas-eventbridge-error-handling) section on this page.

   ![The EventBridge input boxes in the Trigger configuration.](/images/triggers/eventbridge-trigger-config.png)

   To enable Extended JSON, toggle the Enable Extended JSON setting in the Advanced (Optional) section.

   By default, Triggers convert the BSON types in event objects into standard JSON types.

   Enabling Extended JSON preserves BSON type information by serializing event objects into [Extended JSON format](https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/) instead. This preserves type information at the expense of readability and interoperability.

3. Associate the Trigger Event Source with an Event Bus

   Return to the EventBridge console.

   Select Partner event sources in the navigation pane.

   In the Partner event sources table, find and select the Pending Trigger source, then click Associate
   with event bus.

   On the Associate with event bus screen, define any required access permissions for other accounts and organizations, then click Associate.

   After the association is confirmed, the status of the Trigger event source changes from Pending to Active, and the name of the event bus updates to match the event source name. You can now create rules that trigger on events from that partner event source.

   For more information, see [Creating a Rule that Triggers on a SaaS Partner Event.](https://docs.aws.amazon.com//eventbridge/latest/userguide/create-rule-partner-events.html)

## Custom Error Handling

**Note: Only Database Triggers Support Custom Error Handlers**

Currently, only Database Triggers support custom error handling. Authentication Triggers and Scheduled Triggers do not support custom error handling at this time.

You can create an error handler to execute on a Trigger failure when retry does not succeed. Custom error handling allows you to determine whether an error from AWS EventBridge is critical enough to suspend the Trigger, or if it is acceptable to ignore the error and continue processing other events.

For more information on suspended Database Triggers, refer to [Suspended Triggers.](https://www.mongodb.com/docs/atlas/atlas-ui/triggers/database-triggers.md#std-label-atlas-suspended_triggers)

### Create a New Custom Error Handler

You can create a new error handler in the Atlas UI, using the App Services CLI, or through the App Services Admin API.

### Atlas UI

This procedure walks you through how to create the new Function directly in the Create a Trigger page.

You can also create the Function from the Functions page. For more information on how to define Functions in Atlas, refer to [Define a Function.](https://www.mongodb.com/docs/atlas/atlas-ui/triggers/functions.md#std-label-atlas-define-a-function)

![The EventBridge custom error handling configuration in the UI.](/images/triggers/eventbridge-custom-function.png)

1. Create a New Error Handler

   From the Configure Error Function section of the Create a Trigger page, select + NewFunction.

   You can also select an existing Function, if one is already defined, from the dropdown.

2. Name the New Function

   Enter a unique identifying name for the Function in the Name field. This name must be distinct from all other Functions in the application.

3. Write the Function Code

   In the Function section, write the JavaScript code directly in the Function editor. The Function editor contains a default Function that you can edit as needed. For more information on creating Functions, refer to the [Functions](https://www.mongodb.com/docs/atlas/atlas-ui/triggers/functions.md#std-label-atlas-functions) documentation.

4. Test the Function

   In the Testing Console tab beneath the Function editor, you can test the Function by passing in example values to the `error` and `changeEvent` parameters, as shown in the comments of the testing console.

   For more information on these parameters, refer to the [Error Handler Parameters](https://www.mongodb.com/docs/atlas/atlas-ui/triggers/aws-eventbridge.md#std-label-atlas-eventbridge-error-handler-parameters) section on this page.

   Click Run to run the test.

5. Save the Function

   Once you are satisfied with the custom error handler, click Save.

### Error Handler Parameters

The default error handler has two parameters: `error` and `changeEvent`.

#### `error`

Has the following two attributes:

- `code`: The code for the errored EventBridge put request. For a list of error codes used by the error handler, refer to the [Error Codes](https://www.mongodb.com/docs/atlas/atlas-ui/triggers/aws-eventbridge.md#std-label-atlas-eventbridge-error-codes) section on this page.

- `message`: The unfiltered error message from an errored EventBridge put request.

#### `changeEvent`

The requested change to your data made by EventBridge. For more information on types of change events and their configurations, see [Change Event Types.](https://www.mongodb.com/docs/atlas/atlas-ui/triggers/database-triggers.md#std-label-atlas-database-events)

### Error Codes

If an error was received from EventBridge, the event processor will parse the error as either `DOCUMENT_TOO_LARGE` or `OTHER`. This parsed error is passed to the error handler Function through the `error` parameter.

#### `DOCUMENT_TOO_LARGE`

If the entry for an EventBridge Trigger event is larger than 1 MB, EventBridge will throw an error. The error will contain either:

- [status code: 400](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400) and `total size of the entries in the request is over the limit`.

- [status code: 413](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/413), which indicates a too large payload.

For more information on reducing entry size, see [Performance Optimization.](https://www.mongodb.com/docs/atlas/atlas-ui/triggers/aws-eventbridge.md#std-label-atlas-send-aws-eventbridge-performance-optimization)

#### `OTHER`

The default bucket for all other errors.

**Tip: Optimize Error Handling for Errors with OTHER Code**

You can make special error handling cases for your most common error messages to optimize your error handling for errors with an `OTHER` code. To determine which errors need special cases, we recommended keeping track of the most common error messages you receive in `error.message`.

### Error Handler Logs

You can view [Trigger Error Handler logs](https://www.mongodb.com/docs/atlas/atlas-ui/triggers/logs.md#std-label-atlas-logs-trigger-error-handler) for your EventBridge Trigger error handler in the application logs.

### Atlas UI

From the `Triggers` page of the Atlas UI, select the Logs tab.

All logs display by default. To view only error handler logs, click the Show errors only toggle.

To learn more about viewing application logs, see [View Application Logs.](https://www.mongodb.com/docs/atlas/app-services/activity/view-logs.md#std-label-view-logs)

## Example Event

The following object configures a Trigger to send events to AWS EventBridge and handle errors:

```json
"event_processors": {
   "AWS_EVENTBRIDGE": {
      "config": {
         "account_id": "012345678901",
         "region": "us-east-1"
      }
   }
},
 "error_handler": {
   "config": {
      "enabled": true,
      "function_name": "myErrorHandler.js"
   }
}
```

## Performance Optimization

Individual entries for an EventBridge Trigger event must be smaller than 1 MB.

For more information, see the [AWS Documentation to calculate Amazon PutEvents event entry size](http://docs.aws.amazon.com/eventbridge/latest/userguide/eb-putevents.html#eb-putevent-size). When using Database Triggers, the Project Expression can include only specified fields, reducing document size before sending messages to EventBridge. For more details about Project Expression, refer to the [Database Trigger Project Expression documentation.](https://www.mongodb.com/docs/atlas/atlas-ui/triggers/database-triggers.md#std-label-atlas-database-triggers-project-expression)
