Cloud Computing 14 min read

Automate Alibaba Cloud EventBridge with Terraform: A Step‑by‑Step IaC Guide

This article explains the core concepts of Alibaba Cloud EventBridge for building event‑driven architectures, compares imperative and declarative IaC approaches, and provides detailed Terraform examples—including a DingTalk alert and a custom bus triggering FunctionCompute—complete with code snippets and deployment steps.

Alibaba Cloud Native
Alibaba Cloud Native
Alibaba Cloud Native
Automate Alibaba Cloud EventBridge with Terraform: A Step‑by‑Step IaC Guide

Introduction

EventBridge is a serverless event bus on Alibaba Cloud that enables event collection, processing, and routing for event‑driven architectures (EDA). Using Infrastructure as Code (IaC) tools such as Terraform automates the provisioning of EventBridge resources, avoiding manual console operations.

Event‑Driven Architecture Overview

EDA is a loosely coupled, distributed model where applications emit events that are processed in real time and routed downstream without waiting for a response. EventBridge implements this model using the CloudEvents 1.0 protocol.

Event collection : gathers events such as order creation or status changes.

Event processing : sanitizes, filters, and selects events.

Event routing : forwards events to downstream services based on content.

EventBridge Core Concepts

Event sources can be Alibaba Cloud official sources or custom sources. Event buses come in two types:

Cloud service bus : a built‑in, immutable bus that receives only official events.

Custom bus : a user‑created bus that accepts events from custom applications or legacy data.

An EventBridge rule consists of an event pattern for filtering and an event target that defines how the event is transformed or consumed.

IaC Overview

IaC treats infrastructure as versioned code, providing a single source of truth, collaborative change management, code review, and CI/CD automation (including GitOps). Two IaC styles exist:

Imperative : explicitly issues actions (e.g., “create an ECS of type t2.micro”).

Declarative : describes the desired end state (e.g., “an ECS of type t2.micro exists”) and lets the tool resolve ordering and differences.

Terraform is a leading declarative IaC tool with extensive Alibaba Cloud provider support, using the HCL language.

Example 1: DingTalk Alert via EventBridge

Goal: Forward ActionTrail audit events to a DingTalk robot for real‑time notifications.

provider "alicloud" {
  access_key = "YOUR_ACCESS_KEY"
  secret_key = "YOUR_SECRET_KEY"
  region     = "YOUR_REGION_ID"
}

resource "alicloud_event_bridge_rule" "audit_notify" {
  event_bus_name = "default"
  rule_name      = "audit_notify"
  description    = "demo"
  filter_pattern = jsonencode({
    "type" : [{ "suffix" : ":ActionTrail:ApiCall" }]
  })
  targets {
    target_id = "test-target"
    endpoint  = "YOUR_DINGTALK_WEBHOOK_URL"
    type      = "acs.dingtalk"
    param_list {
      resource_key = "URL"
      form         = "CONSTANT"
      value        = "YOUR_DINGTALK_WEBHOOK_URL"
    }
    param_list {
      resource_key = "SecretKey"
      form         = "CONSTANT"
      value        = "YOUR_DINGTALK_SECRET_KEY"
    }
    param_list {
      resource_key = "Body"
      form         = "TEMPLATE"
      value = jsonencode({
        "source"   : "$.source",
        "type"     : "$.type",
        "region"   : "$.data.acsRegion",
        "accountId": "$.data.userIdentity.accountId",
        "eventName": "$.data.eventName"
      })
      template = jsonencode({
        "msgtype" : "text",
        "text"    : { "content" : "Audit event from $${source}: $${type} by $${accountId} in $${region} – $${eventName}" }
      })
    }
  }
}

Deploy with the standard Terraform workflow:

terraform init
terraform plan
terraform apply

After applying, the DingTalk robot receives formatted audit notifications, and the EventBridge console shows the event trace.

Example 2: Custom Bus Triggering FunctionCompute

Goal: Use a custom event bus to invoke a FunctionCompute function when user‑generated events occur.

Python handler (src/index.py)

# -*- coding: utf-8 -*-
import logging

def handler(event, context):
    logger = logging.getLogger()
    logger.info('evt: ' + str(event))
    return str(event)

Terraform configuration (2_trigger_function.tf)

resource "alicloud_event_bridge_event_bus" "demo_event_bus" {
  event_bus_name = "demo_event_bus"
  description    = "demo"
}

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
}

resource "alicloud_fc_service" "fc_service" {
  name        = "eb-fc-service"
  description = "demo"
  publish     = true
}

data "archive_file" "code" {
  type        = "zip"
  source_file = "${path.module}/src/index.py"
  output_path = "${path.module}/code.zip"
}

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"
}

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"
  filter_pattern = jsonencode({ "source" : ["${alicloud_event_bridge_event_source.demo_event_source.id}"] })
  targets {
    target_id = "demo-fc-target"
    type      = "acs.fc.function"
    endpoint  = "acs:fc:YOUR_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" }
    param_list { resource_key = "Body";       form = "ORIGINAL" }
  }
}

Deploy with the same Terraform commands (init, plan, apply). After publishing a custom event to the custom bus, FunctionCompute logs show the received payload, and the EventBridge console displays the event trace.

Conclusion

EventBridge provides a flexible backbone for building EDA on Alibaba Cloud. Terraform enables repeatable, automated provisioning of EventBridge resources, allowing developers to define event patterns, targets, and tracing capabilities as code. This combination supports rapid, version‑controlled deployment of robust event‑driven systems.

Reference links:

Alibaba Cloud Terraform documentation: https://help.aliyun.com/product/95817.html

Terraform Registry – EventBridge resources: https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/event_bridge_event_bus

DingTalk custom robot documentation: https://open.dingtalk.com/document/group/custom-robot-access

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

cloud computingAlibaba CloudiacTerraformEvent-Driven ArchitectureEventBridge
Alibaba Cloud Native
Written by

Alibaba Cloud Native

We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.