nav-img
Advanced

Using a Kafka Trigger

This section describes how to create a Kafka trigger and configure a Kafka event to trigger a function.

After a Kafka trigger is used, FunctionGraph periodically polls for new messages in a specific topic in a Kafka instance and passes the messages as input parameters to invoke functions. For details about the DMS for Kafka event source, see Supported Event Sources.

Prerequisites

Before creating a Kafka trigger, make sure you have prepared the following:

Creating a Kafka Trigger

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click the HelloWorld function.
  3. Choose Configuration > Triggers and click Create Trigger.
  4. Set the following parameters:

    • Trigger Type: Select Distributed Message Service for Kafka (Kafka).
    • Instance: Select a Kafka premium instance.
    • Topic: Select a topic of the Kafka premium instance.
    • Batch Size: Set the number of messages to be retrieved from the topic each time.
    • Username: Enter the username of the instance if SSL has been enabled for it.
    • Password: Enter the password of the instance if SSL has been enabled for it.

  5. Click OK.

    Note

    After VPC access is enabled, you need to configure corresponding subnet permissions for the Kafka security group. For details about how to enable VPC access, see Configuring VPC Access.

    Figure 1 Configuring a subnet


Configuring a Kafka Event to Trigger the Function

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click the HelloWorld function.
  3. On the function details page, click the Code tab and select Configure Test Event.
  4. Set the parameters described in Table 1 and click Save.

    Table 1 Test event information

    Parameter

    Description

    Configure Test Event

    You can choose to create a test event or edit an existing one.

    Use the default option Create new test event.

    Event Template

    Select DMS (for Kafka) to use the built-in Kafka event template.

    Event Name

    The event name can contain 1 to 25 characters and must start with a letter and end with a letter or digit. Only letters, digits, underscores (_), and hyphens (-) are allowed. For example, kafka-123test.

    Event data

    The system automatically loads the built-in Kafka event template, which is used in this example without modifications.

    Note

    The event template is as follows:

    {
    "event_version": "v1.0",
    "event_time": 1576737962,
    "trigger_type": "KAFKA",
    "region": "xx-xxxx-1",
    "records": [{
    "messages": [
    "kafka message1",
    "kafka message2",
    "kafka message3",
    "kafka message4",
    "kafka message5"
    ],
    "instance_id": "81335d56-b9fe-4679-ba95-7030949cc76b",
    "topic_id": "topic-test"
    }]
    }

  5. Click Test. The function test result is displayed.