All Products
Search
Document Center

EventBridge:Automate the deployment of EventBridge resources based on IaC

Last Updated:Jan 17, 2024

This topic describes how to automate the deployment of EventBridge resources based on Infrastructure as Code (IaC). This helps reduce workload in managing a large number of cloud services.

Background information

IaC is a practical method that can be used to configure and manage infrastructure such as virtual machines (VMs) and networks by using code. This method uses the code-based and version-based management mode to replace the traditional manual management mode. This not only improves the management efficiency, but also prevents accidental operations caused by manual management. In the process of IaC, developers use code to describe the configuration and changes of infrastructure, and then run the code to configure and change the infrastructure. Cloud services such as VMs and networks can be deployed within several minutes. This greatly shortens the deployment duration, ensures the configuration consistency of multiple environments, reduces manual operations, and minimizes errors.

HashiCorp Terraform is a tool that you can use to automate the orchestration of IT infrastructure. As one of the mainstream IaC tools, Terraform provides powerful capabilities to automate the management of infrastructure. The easy-to-use CLI of Terraform allows you to deploy configuration files on Alibaba Cloud or in other supported clouds and control the versions of the configuration files. Most Alibaba Cloud services, including EventBridge, support Terraform. This simplifies the deployment of infrastructure across multiple clouds. For more information, see What is Terraform?

This topic describes how to use Terraform to automate the deployment of infrastructure based on IaC.

Prerequisites

Make sure that the following operations are performed:

Use a custom event bus to trigger Function Compute

If events that are generated by your application are processed by Function Compute, you can use a custom event source in EventBridge and Function Compute as the event target. In the following example, Terraform, instead of the EventBridge console and the Function Compute console, is used to deploy EventBridge and Function Compute resources.

  1. Create a file named terraform.tf. Terraform can use this file to call resources on Alibaba Cloud.

    # You can obtain the AccessKey pair on the Security Management page in the Alibaba Cloud Management Console. 
    # Replace <Region Id> with the ID of the region where you want to use a custom event bus to trigger Function Compute, such as cn-hangzhou. 
    provider "alicloud" {
      access_key = "<your Access Key Id>"            
      secret_key = "<your Access Key Secret>"        
      region = "<Region Id>"                        
    }
  2. Create a Python script file named src/index.py to simulate the processing of events.

    # -*- coding: utf-8 -*-
    import logging
    
    def handler(event, context):
      logger = logging.getLogger()
      logger.info('evt: ' + str(event))
      return str(event)
  3. Create a file named 2_trigger_function.tf to declare a custom event bus, a custom event source, a Function Compute service, a Function Compute function, and an event rule that is used to filter events from the custom event source.

    # Declare a custom event bus named demo_event_bus. 
    resource "alicloud_event_bridge_event_bus" "demo_event_bus" {
      event_bus_name = "demo_event_bus"
      description    = "demo"
    }
    
    # Declare a custom event source for the custom event bus demo_event_bus. 
    resource "alicloud_event_bridge_event_source" "demo_event_source" {
      event_bus_name         = alicloud_event_bridge_event_bus.demo_event_bus.event_bus_name
      event_source_name      = "demo_event_source"
      description            = "demo"
      linked_external_source = false
    }
    
    # Declare a Function Compute service named fc_service. Set the publish parameter to true. This indicates that function code is immediately deployed after the code is uploaded. 
    resource "alicloud_fc_service" "fc_service" {
      name        = "eb-fc-service"
      description = "demo"
      publish     = true
    }
    
    # Compress the created Python script file into a ZIP package for deployment to Function Compute. 
    data "archive_file" "code" {
      type        = "zip"
      source_file = "${path.module}/src/index.py"
      output_path = "${path.module}/code.zip"
    }
    
    # Declare a Function Compute function in the fc_service service. Set the filename parameter to the name of the ZIP package to upload this code package. 
    resource "alicloud_fc_function" "fc_function" {
      service     = alicloud_fc_service.fc_service.name
      name        = "eb-fc-function"
      description = "demo"
      filename    = data.archive_file.code.output_path
      memory_size = "128"
      runtime     = "python3"
      handler     = "index.handler"
    }
    
    # Declare an event rule for the custom event bus demo_event_bus. 
    resource "alicloud_event_bridge_rule" "demo_rule" {
      event_bus_name = alicloud_event_bridge_event_bus.demo_event_bus.event_bus_name
      rule_name      = "demo_rule"
      description    = "demo"
      # Set the source parameter to filter events from the custom event source. 
      filter_pattern = jsonencode(
        {
          "source" : ["${alicloud_event_bridge_event_source.demo_event_source.id}"]
        }
      )
    
      targets {
        target_id = "demo-fc-target"
        type      = "acs.fc.function"
        # Replace <Region Id> with the ID of the region where services within your Alibaba Cloud account are activated, and replace <your Account Id> with the ID of your Alibaba Cloud account. 
        endpoint  = "acs:fc:<Region Id>:<your Account Id>:services/${alicloud_fc_service.fc_service.name}.LATEST/functions/${alicloud_fc_function.fc_function.name}"
        param_list {
          resource_key = "serviceName"
          form         = "CONSTANT"
          value        = alicloud_fc_service.fc_service.name
        }
        param_list {
          resource_key = "functionName"
          form         = "CONSTANT"
          value        = alicloud_fc_function.fc_function.name
        }
        param_list {
          resource_key = "Qualifier"
          form         = "CONSTANT"
          value        = "LATEST"
        }
        # Set the form parameter to ORIGINAL. This indicates that the original content of each event is delivered. 
        param_list {
          resource_key = "Body"
          form         = "ORIGINAL"
        }
      }
    }                        
  4. Use Terraform to create the resources that are declared in the 2_trigger_function.tf file.

    1. Run the initialization command terraform init on the CLI. The following output indicates that the initialization is successful:

      Terraform has been successfully initialized!
    2. Run the terraform plan command to preview the changes. In the following output, you can preview the resources to be created:

      alicloud_event_bridge_event_bus.demo_event_bus will be created
      alicloud_event_bridge_event_source.demo_event_source will be created
      alicloud_event_bridge_rule.demo_rule will be created
      alicloud_fc_service.fc_service will be created
      alicloud_fc_function.fc_function will be created
    3. Run the terraform apply command to apply the changes to your application. The following output indicates that the resources are created:

      Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
  5. Verify the created resources in the EventBridge console and the Function Compute console, including the custom event bus, custom event source, Function Compute service, Function Compute function, and event rule that is used to filter events from the custom event source.

    1. Check whether the custom event bus and custom event source are created.

      1. Log on to the EventBridge console. In the left-side navigation pane, click Event Buses.

      2. In the top navigation bar, select the region where the resources were created.

      3. On the Event Buses page, find and click demo_event_bus.

      4. In the left-side navigation pane, click Event Sources. On the Event Sources page, find and click demo_event_source to view the details.

    2. Check whether the Function Compute service and function are created.

      1. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.

      2. In the top navigation bar, select the region where the resources were created.

      3. On the Services page, find and click eb-fc-service.

      4. In the left-side navigation pane, click Functions. On the Functions page, find and click eb-fc-function to view the details of the function.

    3. Trigger an event.

      1. Log on to the EventBridge console. In the left-side navigation pane, click Event Buses.

      2. In the top navigation bar, select the region where the resources were created.

      3. On the Event Buses page, find and click demo_event_bus.

      4. In the left-side navigation pane, click Event Rules. On the Event Rules page, find and click demo_rule.

      5. On the demo_rule details page, click Publish Event in the upper-right corner.

      6. In the Publish Event to Custom Event Bus demo_event_bus panel, select demo_event_source from the Custom Event Source drop-down list and click OK.

    4. View event details.

      1. In the left-side navigation pane of the demo_event_bus details page, click Event Tracking.

      2. Find the published event and click Event Trace in the Operations column. In the Event Trace dialog box, check whether the event is delivered to the eb-fc-function function.事件投递成功