You can use event rules to filter events and route them to an ApsaraDB RDS for MySQL instance or a self-managed MySQL database. This topic uses a custom event as an example to describe the prerequisites, procedure, and verification steps.
Region limitations
Routing events to a database is supported only in the China (Zhangjiakou), Germany (Frankfurt), and Singapore regions.
Prerequisites
-
EventBridge is activated. For more information, see Activate EventBridge and grant permissions.
-
If you want to route events to an ApsaraDB RDS for MySQL instance, complete the following:
-
(Deprecated, redirected to "Step 1") Create a database and an account.
-
Create a database table. For information about common SQL statements, see Common SQL statements.
-
Configure a whitelist for the RDS instance. For more information, see (Deprecated, redirected to "Step 2") Connect to an ApsaraDB RDS for MySQL instance by using a client or the CLI.
NoteMake sure that you are the owner (creator) of the ApsaraDB RDS for MySQL instance.
-
If you want to route events to a self-managed MySQL database, ensure the database can communicate with EventBridge over the network.
Step 1: Add a custom event source
-
Log on to the EventBridge console. In the left-side navigation pane, click Event Buses.
-
In the top navigation bar, select a region. On the Event Buses page, click the name of the target custom event bus.
-
In the left-side navigation pane, click Event Sources and then click Add Event Source.
-
In the Add Custom Event Source panel, enter a Name and Description, select Custom Application for Event Provider, and then click OK.
Step 2: Create an event rule
The event target and the event rule must be in the same region.
Log on to the EventBridge console.
-
In the left-side navigation pane, click Event Buses.
In the top navigation bar, select a region.
-
On the Event Buses page, click the name of the target event bus.
-
In the left-side navigation pane, click Event Rules.
-
On the Event Rules page, click Create Rule.
-
On the Create Rule page, complete the following steps.
-
In the Configure Basic Info step, enter a rule name in the Name field and a rule description in the Description field, and then click Next.
-
In the Configure Event Pattern step, set Event Source Type to Custom Event Sources and select the custom Event Sources you added in Step 1. Then, enter an event pattern in the Event Pattern Content code editor and click Next.
For more information, see Event patterns.
-
In the Configure Targets step, select Database for Service Type, configure the following parameters, and then click Create.
NoteYou can add up to five targets to a single event rule.
Category
Parameter
Description
Database Configuration
Instance ID
The ID of the target ApsaraDB RDS for MySQL instance. This parameter is required only when Database Configuration is set to RDS for MySQL.
The endpoint of the database.
The endpoint of the target self-managed MySQL database. The endpoint consists of the host's IP address or domain name and the port number, which is 3306. This parameter is required only when Database Configuration is set to Self-managed MySQL Database.
The name of the database.
The name of the target database.
-
If Database Configuration is set to RDS for MySQL, select a database name from the drop-down list.
-
If Database Configuration is set to Self-managed MySQL Database, enter the database name.
The username used to log on to the database.
The account used to access the target database.
The password used to log on to the database.
The password for the specified database account.
Concurrency Configuration (Database Connection Pool)
The maximum number of concurrent database connections. Default: 10.
Network Configuration
Internet
Route events to the database over the public network. This option is available only when Database Configuration is set to Self-managed MySQL Database.
VPC
The VPC information used to route events.
-
VPC: The VPC used to route events.
-
vSwitches: The vSwitch used to route events.
-
Security Group: The security group that allows EventBridge to access the database.
Database Import Configuration
Quick Configuration
Provides a simple way to configure data operations on the database table.
-
Table Name: The name of an existing data table in the database. The routed event content is written to this table.
-
Operation Method: The Data Manipulation Language (DML) operation to be performed on the database table. The following operations are supported:
-
INSERT: Inserts event content into the database table.
-
UPDATE: Updates content in the database table based on the specified key-value pair.
-
DELETE: Deletes content from the database table based on the specified key-value pair.
-
-
Column, Value: EventBridge routes the event content to the key-value pairs of the database table.
-
Column: A field in the database table. You can enter a constant or a variable from the event.
-
Value: The value for a table field (key). You can enter a constant or a variable from the event. Maximum length: 1,024 characters.
You can add more key-value pairs as needed by clicking Add .
NoteQuick configuration generates the SQL statements that are executed.
-
Custom SQL Statement
The SQL statement to write data to the database table.
-
Variables: Defines variables that can be used in the SQL statement.
-
SQL Statement: The SQL statement to execute. Maximum length: 10,240 characters.
For more information, see Event transformation.
-
-
Step 3: Publish an event
-
Log on to the EventBridge console. In the left-side navigation pane, click Event Buses.
- In the top navigation bar, select a region.
-
On the Event Buses page, find the target bus and click Publish Event in the Operations column.
NoteYou can publish events from the console only for custom event buses.
-
In the Publish Event to Custom Event Bus {name} panel, select a custom event source from the Custom Event Source drop-down list, enter the event content in the Event Body code editor, and then click OK.
For information about event parameters, see Event structure.
Verify the results
You can view the routed event content in the database. This example uses an ApsaraDB RDS for MySQL instance as the event target and inserts data with an id of 123 to verify successful delivery.
-
Go to the RDS Instances page. In the top navigation bar, select a region and then click the ID of the target instance.
-
On the Basic Information page of the instance, click Log On to Database in the upper-right corner.
-
In the Log On to Instance dialog box, enter the database account and password, and then click Login.
-
View the data in the database table.
The inserted data is visible in the user table. The row with
id=123has NULL values in thenameandAAAcolumns. The other rows (id=33,id=22, andid=11) contain non-NULL values.You can view the inserted content in the destination database table.