Route events to Function Compute

更新时间:
复制 MD 格式

This topic demonstrates how to use an event rule and a custom event to filter and route events to Function Compute. It covers the prerequisites, procedure, and how to verify the results.

Prerequisites

Step 1: Add a custom event source

  1. Log on to the EventBridge console. In the left navigation pane, click Event Buses.
  2. In the top navigation bar, select a region. On the Event Buses page, click the target custom event bus.
  3. In the left navigation pane, click Event Sources, and then click Add Event Source.
  4. In the Add Custom Event Source panel, enter a Name and Description. For Event Provider, select Event Provider, and then click OK.

Step 2: Create an event rule

Important

The target service and the event rule must be in the same region.

  1. Log on to the EventBridge console. In the left navigation pane, click Event Buses.
  2. In the top navigation bar, select a region. On the Event Buses page, click the target event bus.
  3. In the left navigation pane, click Event Rules, and then click Create Rule.
  4. On the Create Rule page, complete the following steps.

    1. In the Configure Basic Info wizard, enter a name for the rule in the Name text box and a description in the Description text box, and then click Next.

    2. In the Configure Event Pattern step, set Event Source Type to Custom Event Sources. For Event Sources, select the custom event source created in Step 1. Enter an event pattern in the Pattern Content editor, and then click Next.

      For more information, see Event patterns.

    3. In the Configure Targets step, configure the event target, and then click Create.

      Note

      An event rule can have up to five targets.

      Parameter

      Description

      Service Type

      Select Function Compute from the drop-down list.

      Function

      Select the function that you created.

      Event

      The supported transformation types are Complete Event, Partial Event, Fixed Value, and Template. This topic uses the Template type as an example. For more information, see event transformation.

      The following provides examples of Variables and Template.

      Variables:

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

      Template:

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

      Version/Alias

      You can target a specific function version or alias.

      • If you select Specified Version, you must select a specific function version.

      • If you select Specified Alias, you must select a specific function alias.

      Invocation Mode

      Two invocation methods are supported. For more information, see Synchronous invocation and Asynchronous invocation overview.

      • Synchronous: The function processes the event and returns a result immediately.

      • Asynchronous: Function Compute receives and queues the request, then returns a response immediately.

      Delivery Method

      Two event formats are supported:

      • Object: If you select this format, the event is delivered to the downstream function as an object.

      • ObjectList: If you select this format, the event is delivered to the downstream function as an array of objects.

      Note

      If you omit this parameter, events are delivered in Object format by default.

      Retry Policy and Dead-letter Queue

      For setup instructions, see Retry policy and dead-letter queue.

Step 3: Publish an event

  1. Log on to the EventBridge console. In the left navigation pane, click Event Buses.
  2. In the top navigation bar, select a region.
  3. On the Event Buses page, find the target event bus and click Publish Event in the Operations column.

    Note

    Only custom event buses support publishing events from the console.

  4. In the Publish Event to Custom Event Bus {name} panel, select the custom event source that you created from the Custom Event Source drop-down list. Enter the event content in the Event Body editor, and then click OK.

    For information about the parameters in an event, see Event overview.

Note

For EventBridge to retry a failed delivery, your Function Compute function must throw an exception. EventBridge detects this exception and automatically attempts to deliver the event again.

Verify the results

You can verify that the function was invoked by checking its logs in the Function Compute console.

  1. Log on to the Function Compute console.

  2. In the left navigation pane, click Function. In the top navigation bar, select the target region.

  3. On the Function page, click the name of the target function.

  4. On the Function Details page, click the Log tab, and then click Function Logs to view the logs for the target function.

FAQ

Troubleshooting a failed event delivery

If an event delivery fails, view its Event Trace. On the Event Trace page, examine the delivery details and response in the Event Delivery section, and then take action based on the response.

Troubleshooting a "Connection timed out" error

  1. Log on to the Function Compute console, run the target function, and observe its execution time.
  2. If the execution time is longer than 15 seconds, check for network issues. If the execution time is less than 15 seconds, verify that you can access the Function Compute endpoint for the region.
  3. If you cannot access the Function Compute endpoint for the current region, contact support.