Help Center/ EventGrid/ Getting Started/ Sending a Custom Event
Updated on 2023-10-26 GMT+08:00

Sending a Custom Event

This section describes how to send a custom event.

The custom events generated by custom event sources are sent to EG. The EG service filters and converts the custom events based on the filter rule, and triggers the event target (a function in FunctionGraph).

Prerequisites

Step 1: Create a Custom Channel

  1. Log in to the EG console.
  2. In the navigation pane, choose Event Channels.
  3. Click Create Event Channel.
  4. Enter channel in Name, and click OK.

    View the created channel in the Custom area, and record the channel ID.

    Figure 1 Event channel ID

Step 2: Create an Event Source

  1. Log in to the EG console.
  2. In the navigation pane, choose Event Sources.
  3. Click Create Event Source.
  4. Set event source parameters, as shown in Figure 2.

    • Type: Select Existing.
    • Channel: Select the channel created in Step 1.
    • Name: Enter egsdk-source.
    Figure 2 Create a source for the custom event

  5. Click OK.

    View this event source on the Custom tab.

Step 3: Create an Event Target (Create a Function)

  1. Log in to the FunctionGraph console.
  2. Choose Functions > Function List in the navigation pane.
  3. Click Create Function.
  4. Set function parameters, as shown in Figure 3. For details about the function parameters, see Creating a Function.

    • Function Type: Select Event Function.
    • Region: Select the region as required.
    • Function Name: Enter test.
    • Agency: Select Use no agency.
    • Runtime: Select Python 2.7.
    Figure 3 Creating a function

  5. Click Create.
  6. On the Code tab page of the function details page, enter the following code and click Deploy.

    # -*- coding:utf-8 -*-
    import json
    def handler (event, context):
        print(json.dumps(event))
        return {
            "statusCode": 200,
            "isBase64Encoded": False,
            "body": json.dumps(event),
            "headers": {
                "Content-Type": "application/json"
            }
        }

Step 4: Create an Event Subscription

Subscriptions bind event sources, channels, and targets. Events of sources are routed to targets based on specified rules.

  1. Log in to the EG console.
  2. In the navigation pane, choose Event Subscriptions.
  3. Click Create Event Subscription.
  4. Click next to the default subscription name.
  5. Enter CustomEvent in Subscription Name, and click OK.
  6. Configure an event source.

    1. Click Event Source, and set event source parameters as shown in Figure 4.
      • Provider: Select Custom.
      • Channel type: Select Existing for Type.
      • Channel configuration: Select the channel created in Step 1 from Channel.
      • Event source type: Select Existing for Type.
      • Event source configuration: Select egsdk-source (created in Step 2) from Event Source.
      • Filter Rule: Retain the default value and record the value of values, for example, egsdk-source in Figure 4.
      Figure 4 Setting event source parameters
    2. Click OK.

  7. Configure an event target.

    1. Click Event Target, and set event target parameters as shown in Figure 5.
      • Provider: Select Huawei Cloud.
      • Event Target: Select FunctionGraph (function computing).
      • Function: Select test (created in Step 3).
      • Version: Select latest.
      • Transform Type: Select Pass-through.
      Figure 5 Setting event target parameters
    2. Click OK.

  8. Click Save.

Step 6: View Results

  1. Log in to the FunctionGraph console.
  2. Choose Functions > Function List in the navigation pane.
  3. Click the test function to go to the function details page.
  4. On the Metrics tab page, view the number of invocations and running duration.

    Figure 6 Viewing metrics