You can use event rules to filter events and route events to an ApsaraDB RDS for MySQL database or a self-managed MySQL database. This topic describes the prerequisites and procedure for routing custom events to a database. This topic also describes how to verify the results.

Supported regions

The feature for routing events to a database is supported only in the China (Zhangjiakou), Germany (Frankfurt), and Singapore regions.

Prerequisites

Step 1: Create a custom event source

  1. Log on to the EventBridge console. In the left-side navigation pane, click Event Buses.
  2. In the top navigation bar, select a region. On the Event Buses page, click the name of the custom event bus that you want to manage.
  3. In the left-side navigation pane, click Event Sources, and then click Add Event Source.
  4. In the Add Custom Event Source panel, set the Name and Description parameters, select Custom Application from the Event Provider drop-down list, and then click OK.

Step 2: Create an event rule

Important The event targets that you want to configure for an event rule must reside in the same region as the event rule.
  1. Log on to the EventBridge console.
  2. In the left-side navigation pane, click Event Buses.
  3. In the top navigation bar, select the region.
  4. On the Event Buses page, click the name of the event bus that you want to manage.
  5. In the left-side navigation pane, click Event Rules.
  6. On the Event Rules page, click Create Rule.
  7. Complete the Create Rule wizard.
    1. In the Configure Basic Info step, enter a rule name in the Name field and a rule description in the Description field, and click Next Step.
    2. In the Configure Event Pattern step, set the Event Source Type parameter to Custom Event Source, select the custom event source that is created in Step 1 from the Event Source drop-down list, specify an event pattern in the Event Pattern Content code editor, and then click Next Step.

      For more information, see Event patterns.

    3. In the Configure Targets step, set the Service Type parameter to Database, set the parameters that are described in the following table, and then click Create.
      Note You can configure up to five event targets for an event rule.
      Category Parameter Description
      Database Configuration Instance ID The ID of the ApsaraDB RDS for MySQL instance in which you want to configure a database as the event target. This parameter is required only if the Database Configuration parameter is set to RDS For MySql.
      Database Endpoint The endpoint of the self-managed MySQL database that you want to configure as the event target. The endpoint consists of the IP address or domain name of the database host and the port number. The port number is 3306. This parameter is required only if the Database Configuration parameter is set to Self-managed MySQL Database.
      Database Name The name of the database that you want to configure as the event target.
      • If the Database Configuration parameter is set to RDS For MySql, select the database from the drop-down list.
      • If the Database Configuration parameter is set to Self-managed MySQL Database, enter the database name.
      Database Account The account that is used to log on to the database.
      Database Password The password that is used to log on to the database.
      Concurrency Configuration (Database Connection Pool) The maximum number of connections that can be established to the database. Default value: 10.
      Network Configuration Internet Select this option to route events to the database over the Internet. You can route events to the database over the Internet only if the Database Configuration parameter is set to Self-managed MySQL Database.
      Virtual Private Cloud Select this option to route events to the database over a virtual private cloud (VPC).
      • Virtual Private Cloud: the name of the VPC that is used to route events.
      • vSwitch: the vSwitch that is used to route events.
      • Security Group: the security group that is used to route events in the VPC.
      Database Import Configuration Quick Configuration The quick configurations for writing event content to the database. Only simple operations are supported.
      • Table Name: the name of the table that has been created in the database. The event content is written to this table.
      • Operation Method: the mode in which the event content is written to the table. Valid values:
        • INSERT: The event content is inserted into the table.
        • UPDATE: Data is updated in the table based on the key-value pairs that you specify.
        • DELETE: Data is deleted from the table based on the key-value pairs that you specify.
      • Column and Value: the key-value pair that you specify for EventBridge to write event content to the table.
        • Column: a field in the table. You can enter a constant or a variable for partial event transformation.
        • Value: the value of the field specified by the Column parameter. You can specify a field in the table or a field in the event content to be processed by SQL statements. You can enter a constant or a variable for partial event transformation. The value of this parameter cannot exceed 1,024 characters in length.

        To add more key-value pairs, click Add below the Column field.

        Note SQL statements are generated based on the quick configurations that you set. The SQL statements are executed when the rule takes effect.
      Custom SQL Statement The SQL statements that are used to write event content to the table.
      • Parameters: the definitions of the variables that are referenced in the SQL statements to be executed.
      • SQL Statement: the SQL statements to be executed. The SQL statements cannot exceed 10,240 characters in length.

      For more information, see Event transformation.

Step 3: Publish an event

  1. Log on to the EventBridge console.
  2. In the left-side navigation pane, click Event Buses.
  3. In the top navigation bar, select a region.
  4. On the Event Buses page, find the event bus to which you want to publish an event and click Publish Event in the Operations column.
    Note You can publish events only to custom event buses in the EventBridge console.
  5. In the Publish Event to Custom Event Bus 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 more information about the event parameters, see Overview.

Verify the results

You can view the content of the routed event in the database. For example, an event is routed to an ApsaraDB RDS for MySQL database to insert a value of 123 into the id column.

  1. Go to the Instances page on the ApsaraDB RDS console. In the top navigation bar, select the region in which the instance resides, find the instance, and then click the ID of the instance.
  2. In the upper-right corner of the Basic Information page, click Log On to Database.
  3. In the RDS database logon dialog box, enter the database account and password and click Login.
  4. View the table data in the database table.
    ebToMysql

    You can view the inserted content in the database table.