All Products
Search
Document Center

Elastic Compute Service:Perceive interruption events and responses

Last Updated:Apr 23, 2025

Preemptible instances may be forcibly reclaimed due to price factors or market supply and demand changes. If your business is sensitive to instance interruptions, you need to pay attention to timely perceiving the interruption events of preemptible instances and respond accordingly. This document will guide you on how to perceive interruption events through different methods.

Perceive interruption events

Query through ECS SDK

Query instance information through the DescribeInstances interface of Elastic Compute Service (ECS) and determine whether the instance has entered the OperationLocks state based on the returned Recycling status.

  • If null is returned, the instance is available.

  • If the LockReason value is Recycling, the preemptible instance is interrupted and is in the recycling state.

SDK call example

Preparations

  1. Create an AccessKey

    Because an Alibaba Cloud account has all permissions on resources, if the AccessKey is leaked, it poses a great risk. Therefore, it is recommended to use the AccessKey of a RAM user that meets the minimum permission requirements. For more information, see Create an AccessKey.

  2. Grant ECS-related permissions to the RAM user

    Grant the RAM user permissions to operate ECS-related resources. The sample code provided in this document requires creating instance information queries. It is recommended to grant the following permissions:

    Cloud Product

    Granted Permissions

    Elastic Compute Service (ECS)

    This example selects the system policy: AliyunECSFullAccess

  3. Configure Access Credentials

    The sample code in this document reads the AccessKey from the system environment variable as the credential to access cloud services. For specific steps, see Configure Environment Variables on Linux, macOS, and Windows.

  4. Install ECS SDK

    Obtain the ECS SDK. This document installs it by adding Maven dependencies. For more installation methods, see Install ECS Java SDK.

    The following sample code provides an example on how to add Maven dependencies:

    <dependencies>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>ecs20140526</artifactId>
            <version>5.3.0</version>
        </dependency>
    </dependencies>

Initialize the client

The Alibaba Cloud SDK supports multiple access credentials to initialize the client, such as AccessKey and STS Token. For more methods, see Manage Access Credentials. This example uses AccessKey to initialize the client.

import com.aliyun.ecs20140526.Client;
import com.aliyun.teaopenapi.models.Config;

public class Sample {
    private static Client createClient() throws Exception {
        Config config = new Config()
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                // Specify an endpoint. For information about endpoints, see https://api.aliyun.com/product/Ecs
                .setEndpoint("ecs.cn-hangzhou.aliyuncs.com");
        return new Client(config);
    }
}

Build the request object for the interface

Before building the request object, view the API documentation to obtain parameter information.

// Create a request object
DescribeInstancesRequest request = new DescribeInstancesRequest().setRegionId("cn-hangzhou");

Initiate the call

When calling OpenAPI through the client, you can set runtime parameters, such as timeout configuration and proxy configuration. For more information, see Advanced Configuration.

// Specify runtime parameters
RuntimeOptions runtime = new RuntimeOptions();
// Call the DescribeInstances interface
DescribeInstancesResponse response = client.describeInstancesWithOptions(request, runtime);
System.out.println(response.body.toMap());

Exception handling

The Java SDK classifies exceptions into TeaUnretryableException and TeaException.

  • TeaUnretryableException: This type of exception is caused by network issues. TeaUnretryableException is thrown if the number of retries reaches the upper limit.

  • TeaException: This type of exception is mainly caused by business errors.

It is recommended to take appropriate measures to handle exceptions, such as propagating exceptions reasonably, logging, and attempting recovery to ensure the robustness and stability of the system.

Complete example

import com.aliyun.ecs20140526.Client;
import com.aliyun.ecs20140526.models.DescribeInstancesRequest;
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaUnretryableException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.alibaba.fastjson.JSONArray;

import java.util.Arrays;

public class Sample {
    private static Client createClient() throws Exception {
        Config config = new Config()
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                // Specify an endpoint. For information about endpoints, see https://api.aliyun.com/product/Ecs
                .setEndpoint("ecs.cn-hangzhou.aliyuncs.com");
        return new Client(config);
    }

    public static void main(String[] args) {
        try {
            Client client = Sample.createClient();
            // Create a request object
            // Specify the IDs of one or more ECS instances.
            JSONArray instanceIds = new JSONArray();
            instanceIds.addAll(Arrays.asList("i-bp145cvd0exyqj****","i-bp1gehfgfrrk4lah****"));
            DescribeInstancesRequest request = new DescribeInstancesRequest()
                    .setRegionId("cn-hangzhou")
                    .setInstanceIds(instanceIds.toJSONString());
            // Specify runtime parameters
            RuntimeOptions runtime = new RuntimeOptions();
            while (!instanceIds.isEmpty()) {
              // Call the DescribeInstances interface
              DescribeInstancesResponse response = client.describeInstancesWithOptions(request, runtime);
              // Obtain instance-specific results.
              DescribeInstancesResponseBody responseBody = response.getBody();
              DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstances instanceList = responseBody.getInstances();
              // Obtain instance information and determine based on the lockReason return value
              if (instanceList != null && instanceList.getInstance()!= null && !instanceList.getInstance().isEmpty()) {
                  for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : instanceList.getInstance()) {
                      // View the IDs and zone information of the queried instances.
                      System.out.println("result:instance:" + instance.getInstanceId() + ",az:" + instance.getZoneId());
                      if (instance.getOperationLocks() != null ) {
                          DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstanceOperationLocks operationLocks  = instance.getOperationLocks();
                          if(operationLocks.getLockReason()!=null && !operationLocks.getLockReason().isEmpty()){
                              for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstanceOperationLocksLockReason lockReason : operationLocks.getLockReason()) {
                                  // If an instance is locked, view the instance ID and the lock reason.
                                  System.out.println("instance:" + instance.getInstanceId() + "-->lockReason:" + lockReason.getLockReason() + ",vmStatus:" + instance.getStatus());
                                  if ("Recycling".equals(lockReason.getLockReason())) {
                                      // View the IDs of the instances to be recycled.
                                      System.out.println("spot instance will be recycled immediately, instance id:" + instance.getInstanceId());
                                      instanceIds.remove(instance.getInstanceId());
                                  }
                              }
                          }

                      }
                  }
                  // If a preemptible instance is not locked, it is queried every 2 minutes.
                  System.out.print("try describeInstances again later ...");
                  Thread.sleep(2 * 60 * 1000);
              } else {
                  break;
              }
            }
        } catch (TeaUnretryableException ue) {
            // Handle exceptions with caution based on your actual business scenario and do not ignore exceptions in your project. The error messages displayed in this example are for reference only.
            ue.printStackTrace();
            // Obtain the error message
            System.out.println(ue.getMessage());
            // Obtain the request message and query the request information when an error occurs
            System.out.println(ue.getLastRequest());
        } catch (TeaException e) {
            // Handle exceptions with caution based on your actual business scenario and do not ignore exceptions in your project. The error messages displayed in this example are for reference only.
            e.printStackTrace();
            // Obtain the error code
            System.out.println(e.getCode());
            // Obtain the error message that contains the request ID
            System.out.println(e.getMessage());
            // Obtain the detailed error information that is returned by the server
            System.out.println(e.getData());
        } catch (Exception e) {
            // Handle exceptions with caution based on your actual business scenario and do not ignore exceptions in your project. The error messages displayed in this example are for reference only.
            e.printStackTrace();
        }
    }
}

Return results

The following response is returned if the recycle is triggered:

result:instance:i-bp1i9c3qiv1qs6nc****,az:cn-hangzhou-i
instance:i-bp1i9c3qiv1qs6nc****-->lockReason:Recycling,vmStatus:Stopped
spot instance will be recycled immediately, instance id:i-bp1i9c3qiv1qs6nc****

Query from within the instance through metadata

You can access the Metadata Service within an ECS instance to retrieve the termination time of a preemptible instance. For more information about instance metadata, see Instance Metadata.

To obtain the metadata for the shutdown release time of a preemptible instance, use: instance/spot/termination-time

Expected results:

  • If a 404 error is returned, the instance is not scheduled for termination.

  • If a timestamp in the format 2015-01-05T18:02:00Z (UTC) is returned, the instance is scheduled to be terminated at that time.

Example of making a call:

Linux instance

# Obtain the access credentials of the metadata server for authentication
TOKEN=`curl -X PUT "http://100.100.100.200/latest/api/token" -H "X-aliyun-ecs-metadata-token-ttl-seconds:<Validity period of the metadata server access credentials>"`
# Query whether the preemptible instance is interrupted and recycled
curl -H "X-aliyun-ecs-metadata-token: $TOKEN" http://100.100.100.200/latest/meta-data/instance/spot/termination-time

Windows instance

# Obtain the access credentials of the metadata server for authentication
$token = Invoke-RestMethod -Headers @{"X-aliyun-ecs-metadata-token-ttl-seconds" = "<Validity period of the metadata server access credentials>"} -Method PUT -Uri http://100.100.100.200/latest/api/token
# Query whether the preemptible instance is interrupted and recycled
Invoke-RestMethod -Headers @{"X-aliyun-ecs-metadata-token" = $token} -Method GET -Uri http://100.100.100.200/latest/meta-data/instance/spot/termination-time

Query through CloudMonitor SDK

All events related to ECS instances are synchronized to CloudMonitor. You can also directly query preemptible instance interruption events through the DescribeSystemEventAttribute interface of CloudMonitor. Determine whether a preemptible instance triggers an interruption recycle based on whether the return value of the content field in the return result is delete.Delete

SDK call example

Preparations

  1. Create an AccessKey

    Since an Alibaba Cloud account has full permissions over resources, a leaked AccessKey poses significant risks. It is recommended to use an AccessKey from a RAM user with the minimum necessary permissions. For more information, see Create an AccessKey.

  2. Grant CMS-related Permissions to the RAM User

    Grant the RAM user permissions to operate CloudMonitor CMS. The sample code in this document requires querying system events, so it is recommended to grant the following permissions:

    Cloud Product

    Granted Permissions

    CloudMonitor CMS

    AliyunCloudMonitorFullAccess

  3. Configure Access Credentials

    The sample code in this document uses the AccessKey from the system environment variable as the credential to access cloud services. For specific configuration steps, see Configure Environment Variables on Linux, macOS, and Windows.

  4. Install CMS SDK

    Retrieve the CMS SDK. This document describes its installation via Maven dependencies. For additional installation methods, see and Install CMS Java SDK.

    Here is an example of how to add Maven dependencies:

    <dependencies>
        <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>cms20190101</artifactId>
          <version>8.1.3</version>
        </dependency>
    </dependencies>

Initialize the client

The Alibaba Cloud SDK supports various access credentials for client initialization, such as AccessKey and STS Token. For additional methods, see Manage Access Credentials. This example uses an AccessKey to initialize the client.

import com.aliyun.cms20190101.Client;
import com.aliyun.teaopenapi.models.Config;

public class Sample {
    private static Client createClient() throws Exception {
        Config config = new Config()
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                // Specify an endpoint. For information about endpoints, see https://api.aliyun.com/product/Ecs
                .setEndpoint("metrics.cn-hangzhou.aliyuncs.com");
        return new Client(config);
    }
}

Build the request object for the interface

Set up the query parameters for preemptible instance interruption events. For additional parameters, see DescribeSystemEventAttribute.

API

Parameter

Sample Value

DescribeSystemEventAttribute

Product

Product abbreviation: ECS

EventType

Event type: StatusNotification

Name

Event name: Instance:PreemptibleInstanceInterruption

// Create a request object
DescribeSystemEventAttributeRequest request = new DescribeSystemEventAttributeRequest()
    .setProduct("ECS");
    .setEventType("StatusNotification");
     .setName("Instance:PreemptibleInstanceInterruption");

Initiate the call

When calling OpenAPI through the client, runtime parameters such as timeout and proxy configurations can be set. For more information, see Advanced Configuration.

// Specify runtime parameters
RuntimeOptions runtime = new RuntimeOptions();
// Call the DescribeInstances interface
DescribeSystemEventAttributeResponse response = client.describeSystemEventAttributeWithOptions(request, runtime);
System.out.println(response.body.toMap());

Exception handling

The Java SDK categorizes exceptions into TeaUnretryableException and TeaException.

  • TeaUnretryableException: This exception type is thrown due to network issues or when the retry limit is reached.

  • TeaException: This exception type is mainly due to business errors.

Appropriate measures should be taken to handle exceptions, such as reasonable exception propagation, logging, and recovery attempts, to ensure system robustness and stability.

Complete example

import com.aliyun.cms20190101.Client;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.cms20190101.models.DescribeSystemEventAttributeRequest;
import com.aliyun.cms20190101.models.DescribeSystemEventAttributeResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaUnretryableException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

public class Sample {
    private static Client createClient() throws Exception {
        Config config = new Config()
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                // Specify an endpoint. For information about endpoints, see https://api.aliyun.com/product/Ecs
                .setEndpoint("metrics.cn-hangzhou.aliyuncs.com");
        return new Client(config);
    }

    public static void main(String[] args) {
        try {
            Client client = Sample.createClient();
            // Create a request object
            DescribeSystemEventAttributeRequest request = new DescribeSystemEventAttributeRequest()
                .setProduct("ECS");
                .setEventType("StatusNotification");
                .setName("Instance:PreemptibleInstanceInterruption");
            // Specify runtime parameters
            RuntimeOptions runtime = new RuntimeOptions();
            // Call the DescribeSystemEventAttribute interface
            DescribeSystemEventAttributeResponse response = client.describeSystemEventAttributeWithOptions(request, runtime);
            System.out.println(response.body.toMap());
        } catch (TeaUnretryableException ue) {
            // Handle exceptions with caution based on your actual business scenario and do not ignore exceptions in your project. The error messages displayed in this example are for reference only.
            ue.printStackTrace();
            // Obtain the error message
            System.out.println(ue.getMessage());
            // Obtain the request message and query the request information when an error occurs
            System.out.println(ue.getLastRequest());
        } catch (TeaException e) {
            // Handle exceptions with caution based on your actual business scenario and do not ignore exceptions in your project. The error messages displayed in this example are for reference only.
            e.printStackTrace();
            // Obtain the error code
            System.out.println(e.getCode());
            // Obtain the error message that contains the request ID
            System.out.println(e.getMessage());
            // Obtain the detailed error information that is returned by the server
            System.out.println(e.getData());
        } catch (Exception e) {
            // Handle exceptions with caution based on your actual business scenario and do not ignore exceptions in your project. The error messages displayed in this example are for reference only.
            e.printStackTrace();
        }
    }
}

Return results

Analyze the interruption events of preemptible instances based on the response data.

Below is a sample interruption event notification in JSON format:

{
  "ver": "1.0",
  "id": "2256A988-0B26-4E2B-820A-8A********E5",
  "product": "ECS",
  "resourceId": "acs:ecs:cn-hangzhou:169070********30:instance/i-bp1ecr********5go2go",
  "level": "INFO",
  "name": "Instance:PreemptibleInstanceInterruption",
  "userId": "169070********30",
  "eventTime": "20190409T121826.922+0800",
  "regionId": "cn-hangzhou",
  "content": {
    "instanceId": "i-bp1ecr********5go2go",  
    "action": "delete"                       
  }
}

The content field is detailed in the table below. For more information on parameters, see DescribeSystemEventAttribute.

Field

Description

Sample Value

instanceId

The ID of the preemptible instance.

i-bp1ecr********5go2go

action

The action that is performed on the preemptible instance. If the parameter is set to delete, the interrupted preemptible instance is forcefully recycled.

delete

Subscribe to CloudMonitor system events

By subscribing to CloudMonitor system events, you can monitor real-time interruption events of preemptible instances and push notifications through various channels such as text messages, emails, Function Compute, message queues, and webhooks.

Workflow

image

Procedure

  1. Prepare the Receiving Channel

    Function Compute

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

    2. In the top navigation bar, select a region. On the Services page, click the desired service.

    3. On the Functions page, click Create Function.
    4. On the Create Function page, select a method to create a function, configure the following parameters, and then click Create.

      Note

      In this example, Use Built-in Runtime and Use Custom Runtime are used. If the runtimes provided by Function Compute cannot meet your business requirements, you can select Use Container Image to create a function. For more information, see Create a Custom Container function.

      • Basic Settings: Configure the basic information of the function, including Function Name and Handler Type. Handler Type can be set to one of the following values:

        • Event Handler: triggers the function by using a timer, calling API operations, using SDKs, or using triggers integrated with other Alibaba Cloud services.

        • HTTP Handler: triggers function execution by HTTP requests or WebSocket requests. In web scenarios, we recommend that you select Use Custom Runtime.

      • In the Code section, configure the runtime and code-related information of the function.

        Parameter

        Description

        Example

        Runtime

        Select a programming language, such as Python, Java, PHP, or Node.js. For more information about the runtimes that are supported by Function Compute, see Runtimes that are supported by Function Compute.

        Node.js14

        Code Upload Method

        Select a method to upload function code to Function Compute.

        • Use Sample Code: Select sample code provided by Function Compute to create a function based on your business requirements. This is the default method.

        • Upload ZIP: Select and upload a ZIP package that contains your code.

        • Upload JAR: Select and upload a JAR file that contains your function code.

        • Upload Folder: Select and upload a folder that contains function code.

        • OSS: Specify the Bucket Name and Object Name parameters for your function code.

        Note
        • If you set Code Upload Method to Use Sample Code, you do not need to modify Handler. If you select another code upload method, you must modify Handler based on your business requirements. Otherwise, an error is reported when you run the function.

        • If you set Runtime to Java 8 or Java 11, you can select only Use Sample Code, Upload JAR, or OSS to upload function code. For other runtimes, you can select Use Sample Code, Upload ZIP, Upload Folder, or OSS.

        Use Sample Code

        Startup Command

        Note

        You must configure this parameter only if you select Use Custom Runtime to create a function.

        Configure the startup command of the program. If you do not configure the startup command, you must manually create a startup script named bootstrap in the root directory of the code. The bootstrap script is used to start your program.

        npm run start

        Listening Port

        Note

        You must configure this parameter only if you select Use Custom Runtime to create a function.

        Specify the port on which the HTTP server in your code listens.

        9000

      • Advanced Settings: Configure instance information and the function execution timeout period.

        Parameter

        Description

        Example

        Specifications

        Configure vCPU Capacity and Memory Capacity based on your business requirements. For more information about billing of resources, see Billing overview.

        Note

        The ratio of vCPU capacity to memory capacity (in GB) must be set from 1:1 to 1:4.

        0.35 vCPUs, 512 MB

        Size of Temporary Disk

        Specify the size of the hard disk used to temporarily store files based on your business requirements.

        Valid values:

        • 512 MB (default): You are not charged for using a temporary disk of this size. Function Compute provides you with a free disk capacity of 512 MB.

        • 10 GB: You are charged based on a disk size of 9.5 GB.

        Note

        Data can be written to all directories in the temporary hard disk. The directories share the space of the temporary hard disk.

        The lifecycle of the temporary hard disk is consistent with that of the underlying instance. After the instance is recycled by the system, the data on the hard disk is cleared. To persist stored data, you can use File Storage NAS (NAS) or Object Storage Service (OSS). For more information, see Configure a NAS file system and Configure an OSS file system.

        512 MB

        Instance Concurrency

        Specify the instance concurrency. For more information, see Configure instance concurrency.

        10

        Execution Timeout Period

        Specify the timeout period of a function execution. The default value of Execution Timeout Period is 60 seconds and the maximum value is 86,400 seconds.

        60

        Handler

        Specify the handler of the function. The Function Compute runtime loads and invokes the handler to process requests. This parameter is not required if you select Use Custom Runtime or Use Container Image.

        Note

        If you set Code Upload Method to Use Sample Code, you do not need to modify Handler. If you select another code upload method, you must modify Handler based on your business requirements. Otherwise, an error is reported when you run the function.

        index.handler

        Time Zone

        Specify the time zone of the function. After you configure the time zone of the function, the environment variable TZ is automatically added to the function. The value is the time zone that you configure.

        UTC

      • Environment Variables: Configure environment variables for the runtime of your function. For more information, see Environment variables.

      • In the Trigger Configurations section, configure a trigger for the function based on your business requirements. You can use the trigger to trigger the function. For more information, see Manage triggers.

    Simple message queue

    1. Log on to the SMQ console.

    2. In the left-side navigation pane, choose Queue Model > Queues.

    3. In the top navigation bar, select a region.

    4. On the Queues page, click Create Queue.

    5. In the Create Queue panel, configure the following parameters and click OK:

      • Name: the name of the queue.

      • Maximum Message Length: the maximum length of the message that is sent to the queue.

      • Long Polling Period: the maximum duration for which long polling requests are held after the ReceiveMessage operation is called.

      • Visibility Timeout Period: the duration for which a message stays in the Inactive state after the message is received from the queue.

      • Message Retention Period: the maximum duration for which a message exists in the queue. After the specified retention period, the message is deleted regardless of whether the message is received.

      • Message Delay Period: the period after which all messages sent to the queue are consumed.

      • Enable Logging Feature: specifies whether to enable the logging feature.

      After the queue is created, it is displayed on the Queues page.

    Webhook

    Important

    The webhook service must be deployed on a server with public network access. Ensure that the server has access permissions for the response port.

    The following sample code in Java:

    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class WebhookController {
        @PostMapping("/callback")
        public ResponseEntity<String> receiveWebhook(@RequestBody String payload) {
            // Process the payload, such as logging or business logic processing
            System.out.println("Received webhook payload: " + payload);
            // Return a successful response
            return ResponseEntity.ok("Webhook received");
        }
    }

  2. Create a Subscription Policy

    1. Log on to the CloudMonitor console.

    2. In the left-side navigation pane, select Event Hub > Event Subscription.

    3. On the Subscription Policy tab, click Create Subscription Policy.

    4. On the Create Subscription Policy page, set the relevant parameters of the subscription policy.

      This example only describes the parameters involved in subscribing to preemptible instance interruption events. For more parameter descriptions, see Manage Event Subscriptions (Recommended).

      • Basic Information: Enter the Name and Description of the subscription policy.

      • Alert Subscription:

        • Subscription Type: Select System Event

        • Subscription Scope:

        • Product: Select Elastic Compute Service (ECS)

        • Event Type: Select Status Notification

        • Event Name: Select Preemptible Instance Interruption Notification

        • Event Level: Select Warning

        • Application Group, Event Content, and Event Resource: Do not set these parameters, which indicates that all system events of all ECS instances in all application groups within this account are subscribed to Preemptible Instance Interruption Notification.

      • Merge And Noise Reduction: Use the default value.

      • Notification: Use the default notification method for Custom Notification Method.

      • Push And Integration: Click Add Channel. In the pop-up window, click Add Channel. Select the channel prepared in step 1 for Target Type. Complete the other parameters as prompted. For more information about push channels, see Manage Push Channels.

  3. Simulate Interruption Events

    Interruption events of preemptible instances are passively triggered events. When you develop interruption event handlers for preemptible instances, you cannot effectively debug the code. You can use Debug Event Subscription to simulate interruption events of preemptible instances.

    1. On the Subscription Policy tab, click Debug Event Subscription.

    2. In the Create Event Debugging panel, select Product as Elastic Compute Service (ECS), and select Name as Preemptible Instance Interruption Notification.

      The system automatically generates debugging content in JSON format. You need to replace the resource-related information in the JSON file with the information of the preemptible instance for which you want to simulate an interruption event.

      • The Alibaba Cloud account UID variable needs to be replaced with the UID of the current logged-in Alibaba Cloud account.

      • The <resource-id> and <instanceId> need to be replaced with the instance ID of the preemptible instance.

      • The <region ID> needs to be replaced with the region ID of the preemptible instance.

        {
            "product": "ECS",
            "resourceId": "acs:ecs:cn-shanghai:Alibaba Cloud account UID:instance/<resource-id>",
            "level": "WARN",
            "instanceName": "instanceName",
            "regionId": "<region ID>",
            "groupId": "0",
            "name": "Instance:PreemptibleInstanceInterruption",
            "content": {
                "instanceId": "i-2zeg014dfo4y892z***",
                "instanceName": "wor***b73",
                "action": "delete"
            },
            "status": "Normal"
        }
    3. Click OK.

      The system displays a Operation Successful message. CloudMonitor then automatically issues an alarm test notification according to the subscription policy's specified notification method.

  4. Receive and View

    Function Compute

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

    2. In the top menu bar, select a region. On the Service List page, click the target service in the Actions column, and then click Function Management.

    3. On the Function Management page, click the target function name. On the function details page, click the Invocation Logs tab.

    Simple message queue

    1. Log on to the Simple Message Queue (formerly MNS) console.

    2. On the Queues page, find the target queue. In the Action column, select More > Send Messages.

    3. Optional:On the Queue Send/receive Messages Quick Experience page, click Edit Parameters of Receiving Messages. In the Edit Parameters of Receiving Messages panel, configure Receive Times and Polling Period, and then click OK.

    4. In the Queue Send/receive Messages Quick Experience page, in the Receive Message section, click Receive Message.

    5. The Receive Messages section displays the message list of the queue.

    6. Optional:In the message list, find the target message. In the Actions column, click Details. In the Message Details dialog box, view the message content and other information.

    Webhook

    View the invocation status and notification content in the program where the webhook is deployed.

Respond to interruption events

Your response to interruptions should be tailored to your specific business scenarios and requirements. It is highly recommended that you test your applications to ensure they can effectively manage the interruption and recycling of preemptible instances. Below are some strategies and recommendations for your consideration:

  • Gracefully Handle Interruptions

    Respond promptly to interruption signals, save the progress of task processing, release resources, and conclude task execution.

  • Data Persistence and Checkpoints

    Consistently save the progress and interim results of tasks to persistent storage, such as local files or databases, to protect critical business data and configurations. For details on data retention and recovery configuration, see Preemptible Instance Data Retention and Recovery.

  • Test Integration Success

    Initiate test events in CloudMonitor and verify that the program responds appropriately. For more information, see Perceive and Respond to Preemptible Instance Interruption Events through Simple Message Queue.