Help Center/ EventGrid/ Getting Started/ Sending a Custom Event
Updated on 2025-09-05 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 Bus > Event Sources.
  3. Click Create Event Source in the upper right corner.
  4. Set event source parameters, as shown in Figure 2.

    • Type: Select a value from the drop-down list.
    • Name: Enter egsdk-source.
    • Description: Enter a description.
    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.
      Table 1 Custom event source parameters

      Parameter

      Description

      Channel

      Select an existing custom event channel.

      Event Source

      Enter or select a custom event source that has been associated with the selected custom event channel.

      Filter Rule

      Enter an event filter rule.

      Only events that match these filter rules will be routed to the associated targets.

      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 Cloud services.
      • Event Target: Select FunctionGraph (function computing).
      • Function: Select test (created in Step 3).
      • Version: Select latest.
      • Agency: Select the created agency.
      • Transform Type: Select Pass-through.
      Figure 5 Setting event target parameters
    2. Click OK.

  8. Click Save.

Step 5: Send a Custom Event

  1. Configure a custom event. For details, see CloudEvents SDK.

    Modify the following parameters in the sample code for publishing an event:

    • NAME: IAM username.
    • PASSWORD: IAM user password.
    • DOMAIN_NAME: Account name.
    • IAM_ENDPOINT: IAM endpoint. For details, see Regions and Endpoints.
    • PROJECT_ID: The project ID. Obtain it by referring to API Credentials.
    • CHANNEL_ID: Change the value to the channel ID recorded in Step 1.
    • ENDPOINT: EG endpoint.
    • SOURCE: Event source name. Change it to the value of values in Filter recorded in Step 4: Create an Event Subscription.

    • TIME: Time when the event is generated.
    • DATA: Event content that complies with the CloudEvents 1.0 specifications. Modify this parameter based on service requirements.

  2. Run the main function to publish the event.

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