This topic describes how to use EventBridge to subscribe to ECS events and deliver them to a specified downstream service.
Background
EventBridge is a centralized event hub on Alibaba Cloud. It uses the standard CloudEvents 1.0 protocol to connect cloud services, SaaS products, and applications. EventBridge helps you manage events that are generated in your business domain and ensures the quality of events from upstream cloud services. It provides a reliable event subscription and delivery experience with 99.99999999% data reliability and 99.95% service availability in its SLA.
Compared with polling the ECS API operation DescribeInstanceHistoryEvents to check for new ECS events, subscribing to events with EventBridge offers the following benefits:
-
Higher real-time performance: When a subscribed ECS event occurs, a specified action is triggered in milliseconds.
-
Lower costs: You do not need to periodically poll for events. This avoids unnecessary overhead.

Scenarios
-
Subscribe to specific ECS events to trigger notifications via DingTalk, SMS, or email.
-
Push specific ECS events to downstream HTTP or HTTPS services over the internet or a Virtual Private Cloud (VPC).
-
Aggregate ECS events from multiple Alibaba Cloud accounts.
-
Trigger a specific action using Function Compute when a specific ECS event occurs.
-
If ECS events have high latency or high volume, deliver them to Simple Message Queue (formerly MNS) for event subscription.
Prerequisites
-
Create a DingTalk chatbot and record its webhook URL and secret token.
-
Create a service and a function. For more information, see Manage services and Manage functions.
-
Create a Simple Message Queue (formerly MNS) queue. For more information, see Activate SMQ and grant permissions and Create a queue.
Step 1: Use the default event bus
The default EventBridge event bus for cloud services is named default. Alibaba Cloud services deliver events to the default bus in real time through various channels. You can find and subscribe to events from cloud services on the default bus.
Log on to the EventBridge console. In the left-side navigation pane, click Event Buses.
In the top navigation bar, select a region.
-
On the EventBridge page, click default.
Step 2: Create an ECS event rule
-
On the Event Buses page, click Event Rules in the left navigation bar, and then click Create Rule.
-
In the Create Rule panel, perform the following steps.
-
On the Configure Basic Info tab, enter a rule name in the Name field and a description in the Description field. Then, click Next.
-
On the Configure Event Pattern tab, configure the following parameters and click Next.
-
Click the Alibaba Cloud Service Event Sources tab.
-
From the Event Source drop-down list, select acs.ecs.
-
From the Event Type drop-down list, select one or more event types to subscribe to.
-
In the Event Pattern Debugging section, view a sample event. For example, if you select ecs:Instance:StateChange as the event type, the following example is displayed.
{ "id": "45ef4dewdwe1-7c35-447a-bd93-fab****", "source": "acs.ecs", "specversion": "1.0", "subject": "acs.ecs:cn-hangzhou:123456789098****:215672", "time": "2020-11-19T21:04:41Z", "type": "ecs:Instance:StateChange", "aliyunaccountid": "123456789098****", "aliyunpublishtime": "2020-11-19T21:04:42Z", "aliyuneventbusname": "default", "aliyunregionid": "cn-hangzhou", "aliyunpublishaddr": "172.25.XX.XX", "data": { "instanceName": "iZ0jl0kyquo46h****", "instanceType": "ecs.c6.large", "privateIpAddress": "172.21.XX.XX", "publicIpAddress": "8.37.XX.XX", "resourceId": "i-0jl0kyquo46****", "resourceType": "ALIYUN::ECS::Instance", "spotStrategy": "SpotWithPriceLimit", "state": "Running" } }
-
-
On the Configure Targets tab, select a Service Type to configure a delivery scenario.
Simple Message Queue (formerly MNS)
Pushing events to Simple Message Queue (formerly MNS) is suitable for scenarios with low latency requirements or high event volumes. For more information about Simple Message Queue (formerly MNS) pricing, see Billing.
-
Service Type: Select Simple Message Queue (formerly MNS).
-
Queue: Select an existing queue.
-
Message Body: Select Complete Event.
-
Enable Base64 Decoding: Select Yes. This ensures that the event data is delivered in Base64-encoded format to the target queue in Simple Message Queue (formerly MNS).
If the service type is Simple Message Queue (formerly MNS), you can use the NetworkInterfaceDemo.zip package to listen for Simple Message Queue (formerly MNS) messages and obtain events.
DingTalk
-
Service Type: Select DingTalk.
-
Webhook URL: Enter the webhook URL of the DingTalk chatbot.
-
Secret Key: Enter the secret token of the DingTalk chatbot.
-
Content to Push: You can use EventBridge templates to push parameters from an event to the DingTalk chatbot. For example, to push the ECS instance name (instanceName) and state (state), configure the following parameters:
-
Variables: Use JSONPath to extract fields from the event and define variable names.
{ "instanceName":"$.data.instanceName", "state":"$.data.state" } -
Template: Define a template in the format required by the DingTalk chatbot and reference the variables that you defined.
{ "msgtype": "text", "text": { "content": "${instanceName} state is ${state}" } }
-
HTTP service
-
Service Type: Select HTTP or HTTPS.
-
URL: Specify the URL that receives events.
-
Body: Specify the format of the event payload. EventBridge can transform events before pushing them to an HTTP target. It supports four transformation formats: Complete Event, Partial Event, Fixed Value, and Template. For more information, see Event transformation.
-
Network Type:
-
Internet: The target URL is accessed over the internet.
-
VPC: If the target URL is not exposed to the internet, select this option and specify the VPC ID, vSwitch ID, and security group ID.
-
Function Compute
To trigger a custom action when an ECS event occurs, set the service type to Function Compute. EventBridge delivers the event to the specified function, and you can implement custom logic in Function Compute.
-
Service Type: Select Function Compute.
-
Service: The name of the service that you created in Function Compute.
-
Function: The name of the function that you created in Function Compute.
-
Event: Specify the format of the event payload. EventBridge can transform events and then push them to Function Compute. Four transformation formats are supported: Complete Event, Partial Event, Fixed Value, and Template. For more information, see Event transformation.
-
Invocation Mode:
-
Synchronous: Function Compute returns a success response only after the function finishes processing the event.
-
Asynchronous: Function Compute returns a success response as soon as it receives the event.
For more information, see Route events to Function Compute.
-
EventBridge
EventBridge can aggregate ECS events from multiple accounts and regions into a single event bus. To do so, set the service type to EventBridge.
-
Destination Type:
-
Same-account Event Bus: Deliver events to another event bus in the current account.
-
Cross-account Event Bus: Deliver events to an event bus in another account. Cross-account delivery requires authorization. For more information, see Route events to EventBridge.
-
-
Region: Events can be routed between regions within the Chinese mainland, and between regions outside the Chinese mainland.
-
Event Bus: The name of the destination event bus.
-
Event: By default, the complete event is delivered. This setting cannot be changed.
At the bottom of the Configure Targets tab, you can configure a retry policy and a dead-letter queue. For more information, see Retries and dead-letter queues.
For more scenarios, see Target types.
-
-
Step 3: Trigger an event
This topic uses the ecs.Instance.StateChange event type as an example. To trigger the event, change the running state of an ECS instance.
-
Log on to the ECS console.
-
In the left-side navigation pane, choose .
-
In the top navigation bar, select the region where the target resource resides.
-
In the Actions column of the target ECS instance, choose and change the instance state.
Step 4: View delivery results
-
Check whether ECS has generated an event.
EventBridge allows you to query ECS events by time range, event type, event rule name, or event ID. The event ID in EventBridge is the same as the ECS event ID. For more information, see Query events.
-
Check the delivery results for the ECS event subscription.
Use the event ID that you obtained in the previous step. In the Actions column that corresponds to the event ID, click Event Trace to view the delivery trace. The trace includes the target, status, number of attempts, and time. On the Event Trace page, the Event Reception section shows that the event source is ECS (acs.ecs), the event type is ecs:Instance:StateChange, and the event bus name is default. The Event Delivery section shows that the rule name is test, the delivery target is the DingTalk chatbot webhook URL, the delivery status is Succeeded, the delivery latency is 258 ms, and the delivery response is
200.
Monitor events and configure alerts
In the EventBridge monitoring dashboard, you can view metrics for the ECS event subscription task that you created, such as the number of calls, delivery success rate, and delivery latency. You can configure alert policies.
-
Log on to the CloudMonitor console.
-
In the left-side navigation pane, choose .
-
On the Dashboards page, click Middleware in the left-side navigation pane. Then, in the Middleware area, hover over Event Buses and click EventBus.
-
On the EventBus page, select a region at the top of the page. Then, in the Actions column for the default bus, click View Charts to view monitoring details.

On the monitoring page, you can configure alert rules for EventBridge resources. When a resource meets the conditions of an alert rule, CloudMonitor automatically sends an alert notification. For more information, see Create an alert rule.
Retries and dead-letter queues
After events are written to an event bus, EventBridge ensures that each event is delivered to downstream targets at least once. If a downstream receiver is unavailable due to downtime, service issues, or network jitter, EventBridge provides multiple ways to handle failures, including a retry policy, fault tolerance, and a dead-letter queue. You can configure these options in Step 2: Create an event rule for ECS events to ensure reliable delivery.
-
Retry policy
If a downstream recipient cannot properly receive events, an error occurs when EventBridge pushes events to it. In this case, you can configure an EventBridge retry policy to maximize the likelihood of successful delivery.
-
Backoff Retry: Retries 3 times. The retry interval is a random value between 10 seconds and 20 seconds.
-
Exponential Decay Retry: Retries 176 times. The retry interval increases exponentially to 512 seconds. The total retry duration is 1 day. The retry intervals are 1, 2, 4, 8, ..., 512 seconds.
-
-
Dead-letter handling policy
If an event is not delivered after the maximum number of retry attempts is reached, EventBridge discards the event by default. If you want to retain failed events, you can configure a dead-letter queue. EventBridge supports the following dead-letter queue types: ApsaraMQ for RocketMQ, Simple Message Queue (formerly MNS), ApsaraMQ for Kafka, and EventBridge.
-
Fault tolerance
If your scenario requires events to be processed in order, you must pause processing when a failure occurs until the event is delivered. Otherwise, events might be delivered out of order. In this case, select to prohibit fault tolerance. If event order does not matter, we recommend that you allow fault tolerance to maximize concurrent delivery.
-
Fault Tolerance Allowed: Skips failed events, allowing subsequent events to be processed without blocking.
-
Fault Tolerance Prohibited: Pauses processing on a delivery failure. If an event delivery fails and all retries are exhausted, the event stream is blocked.
-