Terraform is an open source tool that you can use to preview, configure, and manage cloud infrastructure and resources in a secure and efficient manner. This topic describes how to manage event buses by using Terraform, including how to create an event bus, an event source, a connection, an API destination, and an event rule.
You can run the sample code in this topic with a few clicks. Click here to run the sample code.
Before you start
Make sure that the following services are activated:
EventBridge is activated. For more information, see Activate EventBridge and grant permissions to a RAM user.
Simple Message Queue (SMQ, formerly MNS) is activated. For more information, see Activate SMQ and authorize RAM users to access SMQ.
We recommend that you use a RAM user that has the minimum required permissions to perform the operations in this topic. This reduces the risk of leaking the AccessKey pair of your Alibaba Cloud account. For more information, see Create a RAM user and Grant permissions to a RAM user. In this topic, the following policy is attached to the RAM user:
{ "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": "*" } ] }The runtime environment for Terraform is prepared by using one of the following methods:
Use Terraform in Terraform Explorer: Alibaba Cloud provides an online runtime environment for Terraform. You can log on to the environment and use Terraform without needing to install it. Suitable for scenarios where you need to use and debug Terraform in a low-cost, efficient, and convenient manner.
Use Terraform in Cloud Shell: Cloud Shell is preinstalled with Terraform and configured with your identity credentials. You can run Terraform commands in Cloud Shell. Suitable for scenarios where you need to use and access Terraform in a low-cost, efficient, and convenient manner.
Install and configure Terraform on your on-premises machine: Suitable for scenarios where network connections are unstable or a custom development environment is needed.
Resources
You are charged for specific resources required in this topic. If you no longer require the resources, release or unsubscribe from the resources at the earliest opportunity.
Event bus: alicloud_event_bridge_event_bus
SMQ queue: alicloud_mns_queue
Event source: alicloud_event_bridge_event_source
Connection: alicloud_event_bridge_connection
API destination: alicloud_event_bridge_api_destination
Event rule: alicloud_event_bridge_rule
Step 1: Create an event bus and an event source
Create a working directory that contains a configuration file named main.tf. Then, copy the following code to the main.tf configuration file. The code is used to create an event bus and an SMQ queue and specify the SMQ queue as the event source.
variable "region" { default = "cn-shanghai" } provider "alicloud" { region = var.region } variable "api_key_name"{ default = "<The username of the API key that you use for authentication>" } variable "api_key_value"{ default = "<The value of the API key that you use for authentication>" } # The URL of the API destination. variable "endpoint"{ default = "http://xxxx:8080/putEventsByAPiKey" } data "alicloud_account" "default" { } resource "random_integer" "default" { min = 10000 max = 99999 } # The custom event bus. resource "alicloud_event_bridge_event_bus" "example" { event_bus_name = "event_bus_name_${random_integer.default.result}" } # The SMQ queue. The queue is used as the event source. resource "alicloud_mns_queue" "source" { name = "queue-name-source-${random_integer.default.result}" } # The custom event source. 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 } }Run the following command to initialize the Terraform runtime environment:
terraform initIf the following information is returned, Terraform is initialized.
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.Run the following command to start code execution:
terraform applyDuring the code execution, enter
yesas prompted and press the Enter key. Wait until the command is complete. If the following information is returned, the code is successfully run.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.Verify the result.
Use the terraform show command
Run the following command in the working directory to query the details of the resources that you created by using Terraform:
terraform show
Use the EventBridge console
Log on to the EventBridge console and go to the page. In the top navigation bar, select the region where the custom event bus that you created resides. In this example, China (Shanghai) is selected. Then, in the event bus list, view the custom event bus that you created.

On the Event Buses page, click the name of the custom event bus that you created. In the left-side navigation pane of the event bus details page, click to view the event source that you created.

Step 2: Create a connection and an API destination
In the
main.tffile, add the following content:# The connection. 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 } } } # The API destination. 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 }Create an execution plan and preview the changes.
terraform planRun the following command to start code execution:
terraform applyDuring the code execution, enter
yesas prompted and press the Enter key. Wait until the command is complete. If the following information is returned, the code is successfully run.Apply complete! Resources: 2 added, 0 changed, 0 destroyed.Verify the result.
Use the terraform show command
Run the following command in the working directory to query the details of the resources that you created by using Terraform:
terraform show
Use the EventBridge console
Log on to the EventBridge console. In the left-side navigation pane, choose . In the top navigation bar, select the region where the connection that you created resides. In this example, China (Shanghai) is selected. Then, click the Connection tab to view the connection that you created.

Click the API Destination tab to view the API destination that you created.

Step 3: Create an event rule
In the
main.tffile, add the following content:# The event rule. 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\"}" # ${} is used to specify variables in EventBridge, and $${} is used to specify variables in Terraform files. template = "{\"$${queryKey1}\":\"$${queryValue1}\"}" } } }Create an execution plan and preview the changes.
terraform planRun the following command to start code execution:
terraform applyDuring the code execution, enter
yesas prompted and press the Enter key. Wait until the command is complete. If the following information is returned, the code is successfully run.Apply complete! Resources: 1 added, 0 changed, 0 destroyed.Verify the result.
Use the terraform show command
Run the following command in the working directory to query the details of the resource that you created by using Terraform:
terraform show
Use the EventBridge console
Log on to the EventBridge console and go to the page. In the top navigation bar, select the region where the event rule that you created resides. In this example, China (Shanghai) is selected. Then, in the event bus list, find the custom event bus that you created and click Event Rule in the Actions column to view the event rule that you created.

Release resources
If you no longer require the preceding resources that are created or managed by using Terraform, run the following command to release the resources. For more information about the terraform destroy command, see Common commands.
terraform destroyComplete sample code
You can run the sample code in this topic with a few clicks. Click here to run the sample code.
Sample code
To view more examples, visit the Event_Bridge folder in GitHub.