すべてのプロダクト
Search
ドキュメントセンター

EventBridge:Terraform を使用したイベントバスの管理

最終更新日:Jan 23, 2026

Terraform は、クラウドインフラストラクチャとリソースを安全かつ効率的にプレビュー、設定、管理するために使用できるオープンソースツールです。このトピックでは、Terraform を使用してイベントバス、イベントソース、接続、API 宛先、イベントルールを作成する方法など、イベントバスを管理する方法について説明します。

説明

このトピックのサンプルコードは、数回のクリックで実行できます。サンプルコードを実行するには、こちらをクリックしてください。

事前準備

  • 次のサービスがアクティブ化されていることを確認します。

  • このトピックの操作を実行するには、最小限の権限を持つ RAM ユーザーを使用することを推奨します。これにより、Alibaba Cloud アカウントの AccessKey ペアが漏洩するリスクを軽減できます。詳細については、「RAM ユーザーの作成」および「RAM ユーザーへの権限付与」をご参照ください。このトピックでは、次のポリシーが RAM ユーザーにアタッチされています。

    {
      "Version": "1",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": "mns:*",
          "Resource": "*"
        },
        {
          "Effect": "Allow",
          "Action": [
            "eventbridge:GetEventBus",
            "eventbridge:ListEventBuses",
            "eventbridge:CreateEventBus",
            "eventbridge:DeleteEventBus",
            "eventbridge:UpdateEventBus",
            "eventbridge:ListApiDestinations",
            "eventbridge:ListUserDefinedEventSources",
            "eventbridge:CreateEventSource",
            "eventbridge:DeleteEventSource",
            "eventbridge:UpdateEventSource",
            "eventbridge:ListPartnerEventSources",
            "eventbridge:GetRule",
            "eventbridge:ListRules",
            "eventbridge:CreateRule",
            "eventbridge:DeleteRule",
            "eventbridge:EnableRule",
            "eventbridge:UpdateRule",
            "eventbridge:ListConnections",
            "eventbridge:CreateConnection",
            "eventbridge:DeleteConnection",
            "eventbridge:UpdateConnection",
            "eventbridge:GetConnection",
            "eventbridge:GetApiDestination",
            "eventbridge:CreateApiDestination",
            "eventbridge:DeleteApiDestination",
            "eventbridge:UpdateApiDestination"
          ],
          "Resource": "*"
        }
      ]
    }
  • Terraform 環境を準備します。次のいずれかの方法で Terraform を実行できます。

    • Terraform Explorer で Terraform を使用する:Alibaba Cloud は Terraform を実行するためのオンライン環境を提供します。Terraform をインストールする必要はありません。ログインしてオンラインで Terraform を使用し、試すことができます。この方法は、無料で迅速かつ便利に Terraform を試してデバッグしたいシナリオに適しています。

    • Cloud Shell:Alibaba Cloud の Cloud Shell には、Terraform コンポーネントがプリインストールされ、認証情報が設定されています。Cloud Shell で直接 Terraform コマンドを実行できます。この方法は、低コストで迅速かつ便利に Terraform にアクセスして使用したいシナリオに適しています。

    • ローカルマシンに Terraform をインストールして設定する:この方法は、ネットワーク接続が悪い場合や、カスタム開発環境が必要なシナリオに適しています。

リソース

説明

このトピックで必要な特定のリソースには料金が発生します。リソースが不要になった場合は、できるだけ早くリソースをリリースまたはサブスクライブ解除してください。

ステップ 1:イベントバスとイベントソースの作成

  1. main.tf という名前の設定ファイルを含む作業ディレクトリを作成します。次に、次のコードを main.tf 設定ファイルにコピーします。このコードは、イベントバスと SMQ キューを作成し、SMQ キューをイベントソースとして指定するために使用されます。

    variable "region" {
      default = "cn-shanghai"
    }
    
    provider "alicloud" {
      region = var.region
    }
    
    variable "api_key_name"{
      # 認証に使用する API キーのユーザー名
      default = "<The username of the API key that you use for authentication>"
    }
    
    
    variable "api_key_value"{
      # 認証に使用する API キーの値
      default = "<The value of the API key that you use for authentication>"
    }
    
    # API 宛先の URL
    variable "endpoint"{
      default = "http://xxxx:8080/putEventsByAPiKey"
    }
    
    data "alicloud_account" "default" {
    }
    
    resource "random_integer" "default" {
      min = 10000
      max = 99999
    }
    
    # カスタムイベントバス
    resource "alicloud_event_bridge_event_bus" "example" {
      event_bus_name = "event_bus_name_${random_integer.default.result}"
    }
    
    # SMQ キュー。このキューはイベントソースとして使用されます。
    resource "alicloud_mns_queue" "source" {
      name = "queue-name-source-${random_integer.default.result}"
    }
    
    # カスタムイベントソース
    resource "alicloud_event_bridge_event_source" "default" {
      event_bus_name         = alicloud_event_bridge_event_bus.example.event_bus_name
      event_source_name      = "event_source_name_${random_integer.default.result}"
      description            = "description_${random_integer.default.result}"
      linked_external_source = true
      external_source_type   = "MNS"
      external_source_config = {
        QueueName = alicloud_mns_queue.source.name
      }
    }
  2. 次のコマンドを実行して、Terraform ランタイム環境を初期化します:

    terraform init

    次の情報が返された場合、Terraform は初期化されています。

    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. 次のコマンドを実行して、コードの実行を開始します:

    terraform apply

    コードの実行中に、プロンプトが表示されたら yes と入力し、Enter キーを押します。コマンドが完了するまで待ちます。次の情報が返された場合、コードは正常に実行されています。

    You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
    
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    
    
    Apply complete!  Resources: 4 added, 0 changed, 0 destroyed.
  4. 結果の確認

    terraform show コマンドの使用

    作業ディレクトリで次のコマンドを実行して、Terraform を使用して作成したリソースの詳細をクエリします:

    terraform show

    image

    EventBridge コンソールの使用

    1. EventBridge コンソールにログインし、[イベントバス] ページに移動します。上部のナビゲーションバーで、作成したカスタムイベントバスが存在するリージョンを選択します。この例では、[中国 (上海)] が選択されています。次に、イベントバスリストで、作成したカスタムイベントバスを表示します。

      image

    2. [イベントバス] ページで、作成したカスタムイベントバスの名前をクリックします。イベントバスの詳細ページの左側のナビゲーションウィンドウで、 をクリックして、作成したイベントソースを表示します。

      image

ステップ 2:接続と API 宛先の作成

  1. main.tf ファイルに、次の内容を追加します:

    # 接続
    resource "alicloud_event_bridge_connection" "defaultConnection" {
      connection_name = "connection_name_${random_integer.default.result}"
      description     = "description_alicloud_event_bridge_connection_${random_integer.default.result}"
      network_parameters {
        network_type      = "PublicNetwork"
      }
      auth_parameters {
        authorization_type = "API_KEY_AUTH"
        api_key_auth_parameters {
          api_key_name  = var.api_key_name
          api_key_value = var.api_key_value
        }
      }
    }
    
    # API 宛先
    resource "alicloud_event_bridge_api_destination" "default" {
      description = "description_alicloud_event_bridge_api_destinationn_${random_integer.default.result}"
      http_api_parameters {
        endpoint = var.endpoint
        method   = "POST"
      }
      api_destination_name = "api_destination_name_${random_integer.default.result}"
      connection_name = alicloud_event_bridge_connection.defaultConnection.connection_name
    }
  2. 実行計画を作成し、変更をプレビューします。

    terraform plan
  3. 次のコマンドを実行して、コードの実行を開始します:

    terraform apply

    コードの実行中に、プロンプトが表示されたら yes と入力し、Enter キーを押します。コマンドが完了するまで待ちます。次の情報が返された場合、コードは正常に実行されています。

    Apply complete!  Resources: 2 added, 0 changed, 0 destroyed.
  4. 結果の確認

    terraform show コマンドの使用

    作業ディレクトリで次のコマンドを実行して、Terraform を使用して作成したリソースの詳細をクエリします:

    terraform show

    image

    EventBridge コンソールの使用

    1. EventBridge コンソールにログインします。左側のナビゲーションウィンドウで、[統合センター] > [API 宛先] を選択します。上部のナビゲーションバーで、作成した接続が存在するリージョンを選択します。この例では、[中国 (上海)] が選択されています。次に、[接続] タブをクリックして、作成した接続を表示します。

      image

    2. [API 宛先] タブをクリックして、作成した API 宛先を表示します。

      image

ステップ 3:イベントルールの作成

  1. main.tf ファイルに、次の内容を追加します:

    # イベントルール
    resource "alicloud_event_bridge_rule" "default" {
      event_bus_name = alicloud_event_bridge_event_bus.example.event_bus_name
      rule_name      = "rule_name_${random_integer.default.result}"
      description    = "description_${random_integer.default.result}"
      filter_pattern = "{\n \"source\": [\n \"tf-test-api-key\"\n ]\n}"
      targets {
        target_id = "tf_example_${random_integer.default.result}"
        endpoint  = "acs:api-destination:${var.region}:${data.alicloud_account.default.id}:name/${alicloud_event_bridge_api_destination.default.api_destination_name}"
        type      = "acs.api.destination"
        param_list {
          resource_key = "Name"
          form         = "CONSTANT"
          value        = "terraform-api_destination-name-api-key"
        }
        param_list {
          resource_key = "QueryStringParameters"
          form         = "TEMPLATE"
          value        = "{\"queryKey1\":\"id\",\"queryValue1\":\"$.data.name\"}"
          # ${} は EventBridge の変数を指定するために使用され、$${} は Terraform ファイルの変数を指定するために使用されます。
          template     = "{\"$${queryKey1}\":\"$${queryValue1}\"}"
        }
      }
    }
  2. 実行計画を作成し、変更をプレビューします。

    terraform plan
  3. 次のコマンドを実行して、コードの実行を開始します:

    terraform apply

    コードの実行中に、プロンプトが表示されたら yes と入力し、Enter キーを押します。コマンドが完了するまで待ちます。次の情報が返された場合、コードは正常に実行されています。

    Apply complete!  Resources: 1 added, 0 changed, 0 destroyed.
  4. 結果の確認

    terraform show コマンドの使用

    作業ディレクトリで次のコマンドを実行して、Terraform を使用して作成したリソースの詳細をクエリします:

    terraform show

    image

    EventBridge コンソールの使用

    1. EventBridge コンソールにログインし、[イベントバス] ページに移動します。上部のナビゲーションバーで、作成したイベントルールが存在するリージョンを選択します。この例では、[中国 (上海)] が選択されています。次に、イベントバスリストで作成したカスタムイベントバスを見つけ、[操作] 列の [イベントルール] をクリックして、作成したイベントルールを表示します。

      image

リソースのリリース

Terraform を使用して作成または管理された上記のリソースが不要になった場合は、次のコマンドを実行してリソースをリリースします。terraform destroy コマンドの詳細については、「一般的なコマンド」をご参照ください。

terraform destroy

完全なサンプルコード

説明

このトピックのサンプルコードは、数回のクリックで実行できます。サンプルコードを実行するには、こちらをクリックしてください。

サンプルコード

variable "region" {
  default = "cn-shanghai"
}

provider "alicloud" {
  region = var.region
}

variable "api_key_name"{
  # 認証に使用する API キーのユーザー名
  default = "<The username of the API key that you use for authentication>"
}


variable "api_key_value"{
  # 認証に使用する API キーの値
  default = "<The value of the API key that you use for authentication>"
}

# API 宛先の URL
variable "endpoint"{
  default = "http://xxxx:8080/putEventsByAPiKey"
}

data "alicloud_account" "default" {
}

resource "random_integer" "default" {
  min = 10000
  max = 99999
}

# カスタムイベントバス
resource "alicloud_event_bridge_event_bus" "example" {
  event_bus_name = "event_bus_name_${random_integer.default.result}"
}

# SMQ キュー。このキューはイベントソースとして使用されます。
resource "alicloud_mns_queue" "source" {
  name = "queue-name-source-${random_integer.default.result}"
}

# カスタムイベントソース
resource "alicloud_event_bridge_event_source" "default" {
  event_bus_name         = alicloud_event_bridge_event_bus.example.event_bus_name
  event_source_name      = "event_source_name_${random_integer.default.result}"
  description            = "description_${random_integer.default.result}"
  linked_external_source = true
  external_source_type   = "MNS"
  external_source_config = {
    QueueName = alicloud_mns_queue.source.name
  }
}

# 接続
resource "alicloud_event_bridge_connection" "defaultConnection" {
  connection_name = "connection_name_${random_integer.default.result}"
  description     = "description_alicloud_event_bridge_connection_${random_integer.default.result}"
  network_parameters {
    network_type      = "PublicNetwork"
  }
  auth_parameters {
    authorization_type = "API_KEY_AUTH"
    api_key_auth_parameters {
      api_key_name  = var.api_key_name
      api_key_value = var.api_key_value
    }
  }
}

# API 宛先
resource "alicloud_event_bridge_api_destination" "default" {
  description = "description_alicloud_event_bridge_api_destinationn_${random_integer.default.result}"
  http_api_parameters {
    endpoint = var.endpoint
    method   = "POST"
  }
  api_destination_name = "api_destination_name_${random_integer.default.result}"
  connection_name = alicloud_event_bridge_connection.defaultConnection.connection_name
}

# イベントルール
resource "alicloud_event_bridge_rule" "default" {
  event_bus_name = alicloud_event_bridge_event_bus.example.event_bus_name
  rule_name      = "rule_name_${random_integer.default.result}"
  description    = "description_${random_integer.default.result}"
  filter_pattern = "{\n \"source\": [\n \"tf-test-api-key\"\n ]\n}"
  targets {
    target_id = "tf_example_${random_integer.default.result}"
    endpoint  = "acs:api-destination:${var.region}:${data.alicloud_account.default.id}:name/${alicloud_event_bridge_api_destination.default.api_destination_name}"
    type      = "acs.api.destination"
    param_list {
      resource_key = "Name"
      form         = "CONSTANT"
      value        = "terraform-api_destination-name-api-key"
    }
    param_list {
      resource_key = "QueryStringParameters"
      form         = "TEMPLATE"
      value        = "{\"queryKey1\":\"id\",\"queryValue1\":\"$.data.name\"}"
      # ${} は EventBridge の変数を指定するために使用され、$${} は Terraform ファイルの変数を指定するために使用されます。
      template     = "{\"$${queryKey1}\":\"$${queryValue1}\"}"
    }
  }
}

その他の例については、GitHub の Event_Bridge フォルダをご参照ください。