This topic describes how to use SDK for Python to create one or more event targets for an event rule.

Sample code:

# -*- coding: utf-8 -*-

from alibabacloud_eventbridge.client import Client as EventBridgeClient
from alibabacloud_eventbridge import models as event_bridge_models
from alibabacloud_tea_console.client import Client as ConsoleClient
from alibabacloud_tea_util.client import Client as UtilClient


class create_targets_sample(object):
    def __init__(self):
        pass

    @staticmethod
    def create_client():
        """
        Uses the create_client() function to initialize common request parameters.
        """
        config = event_bridge_models.Config(

        )
        # Your AccessKey ID.
        config.access_key_id = "<accessKeyId>"
        # Your AccessKey secret.
        config.access_key_secret = "<accessKeySecret>"
        # Your endpoint.
        config.endpoint = "<endpoint>"
        return EventBridgeClient(config)

    @staticmethod
    def create_targets_sample(client):
        try:
            create_targets_request = event_bridge_models.CreateTargetsRequest(

            )
            create_targets_request.event_bus_name = "demo-bus"
            create_targets_request.rule_name = "myRule"
            target_entry = event_bridge_models.TargetEntry(

            )
            target_entry.id = "1234"
            target_entry.endpoint = "http://www.baidu.com"
            list = [
                target_entry
            ]
            create_targets_request.targets = list
            response = client.create_targets(create_targets_request)
            ConsoleClient.log("--------------------create targets success--------------------")
            ConsoleClient.log(UtilClient.to_jsonstring(response.to_map()))
        except Exception as error:
            ConsoleClient.log(error.message)

    @staticmethod
    def main(args):
        client = create_targets_sample.create_client()
        create_targets_sample.create_targets_sample(client)

create_targets_sample.main("")