You can route events to Function Compute by using event rules. This topic describes the prerequisites, procedure, and result verification of routing custom events to Function Compute.

Prerequisites

The following operations are performed:

Step 1: Create a custom event bus

To create a custom event bus to receive events from your applications, perform the following steps:

  1. Log on to the EventBridge console.
  2. In the top navigation bar, select a region.
  3. In the left-side navigation pane, click Custom Event Buses.
  4. On the Custom Event Buses page, click Create Event Bus.
  5. In the Create Event Bus panel, specify Name and Description, for example, set Name to MyEventBus and enter Used for Receiving Custom Application Events in the Description field. Then, click OK.

Step 2: Publish an event

  1. Log on to the EventBridge console.
  2. In the top navigation bar, select a region.
  3. In the left-side navigation pane, click Custom Event Buses.
  4. On the Custom Event Buses page, find the required event bus 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, enter the event content in the code editor and click OK.
    For more information about the event parameters, see Overview.

Step 3: Create an event rule

  1. Log on to the EventBridge console.
  2. In the top navigation bar, select a region.
  3. In the left-side navigation pane, click Custom Event Buses.
  4. On the Custom Event Buses page, find the required event bus and click Rules in the Operations column.
  5. On the Rules page, click Create Rule.
  6. On the Create Rule page, perform the following steps:
    1. 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 Step.
    2. In the Configure Event Pattern step, select Customized Pattern for Event Pattern Type, specify the event pattern in the Event Pattern Content section, and then click Next Step.

      For more information, see Event patterns.

    3. In the Configure Targets step, configure an event target.
      • Service type: Click Function Compute.
      • Service: Select the service that you created.
      • Function: Select the function that you created.
      • Event: Select the type of event transformer.
        • Complete Event: The complete data structure is delivered without conversion. The data structure is defined in the CloudEvents 1.0 protocol.
        • Partial Event: JSONPath is used to extract the content that needs to be delivered to the event target.
        • Constant: The event serves as a trigger. Only constants are delivered regardless of the event content.
        • Template: Events are routed based on a custom template. You can customize variables in the template.

          The following scripts shows examples of variables and a template.

          Variables:

          {
            "source":"$.source",
            "type":"$.type"
          }

          Template:

          The event comes from ${source},event type is ${type}.

        For more information, see Transform events.

      • Service Version and Alias: Select a service version and alias.
      Notice Make sure that the event target and the event rule are in the same region.
    4. Click Create.

Verify the result

To verify the result, you can view logs in the Function Compute console.

  1. Log on to the Function Compute console.
  2. In the top navigation bar, select a region.
  3. In the left-side navigation pane, click Service/Function.
  4. On the Service/Function page, click the service. Then, click the function name in the Function Name column on the Functions tab.
  5. On the page that appears, click the Log tab to view logs.
    FC Invoke Start RequestId: c2be67a7-fh1a-9619-ei4c-3c04gcf6****
    2020-11-19T11:11:34.161Z c2be67a7-fh1a-9619-ei4c-3c04gcf6c**** [verbose] Receive Event v2 ==> The event comes from aliyun.ui,event type is ui:Created:PostObject.
    2020-11-19T11:11:34.167Z c2be67a7-fh1a-9619-ei4c-3c04gcf6c**** 
    FC Invoke End RequestId: c2be67a7-fh1a-9619-ei4c-3c04gcf6c****