All Products
Search
Document Center

Elastic Compute Service:Simulate a preemptible instance interruption event

Last Updated:Aug 18, 2023

Preemptible instance interruption events are triggered events. When you develop an interruption event handler for a preemptible instance, you cannot debug the code. Alibaba Cloud provides methods to simulate preemptible instance interruption events for easy debugging of O&M programs.

Background information

Alibaba Cloud CloudMonitor is required to simulate preemptible instance interruption events. You can use the CloudMonitor console or call CloudMonitor API operations to simulate interruption events.

Procedure

Use the CloudMonitor console to simulate an interruption event

This section describes how to use the CloudMonitor console to simulate an interruption event. In this example, an interruption event-triggered alert rule is configured in the CloudMonitor console, and the event is delivered to a Message Service (MNS) queue.

  1. Log on to the CloudMonitor console.
  2. In the left-side navigation pane, choose Event Monitoring > System Event.

  3. Click the Event-triggered Alert Rules tab.

  4. Click Create Alert Rule.

  5. In the Create/Modify Event-triggered Alert Rule panel, configure the following parameters and click OK:

    • Alert Rule Name: Specify a name for the alert rule. Example: preemptible instance interruption event alert.

    • Product Type: Select Elastic Compute Service (ECS).

    • Event Type: Select Status Notification.

    • Event Level: Select WARN.

    • Event Name: Select Instance:PreemptibleInstanceInterruption.

    • Resource Range: Use the default setting.

    • Notification Method: Select notification methods based on your business requirements. In this example, Message Service - Queue is selected.

    After the alert rule is created, you can view the rule on the Event-triggered Alert Rules tab.

  6. Find the alert rule for preemptible instance interruption events and choose image.png > Test in the Actions column.

  7. In the Create Event Test panel, modify the JSON file and click OK.

    Replace the resource information in the JSON file with the information about the preemptible instance for which you want to simulate an interruption event. The following code provides an example of the JSON file. Take note of the following variables:

    • Replace the <Alibaba Cloud account ID> variable with the ID of your Alibaba Cloud account.

    • Replace the <resource-id> and i-2zeg014dfo4y892z*** variables with the ID of the preemptible instance.

    • Replace the <region ID> variable with the region ID of the preemptible instance.

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

    After debugging starts, alert notifications are sent based on the notification methods that you specified.

Call CloudMonitor API operations to simulate an interruption event

This section describes how to call CloudMonitor API operations to simulate an interruption event. In this example, Alibaba Cloud CloudMonitor SDK for Java is used to configure an interruption event-triggered alert rule and deliver the event to an MNS queue.

  1. Make preparations.

    1. Obtain the AccessKey ID of your Alibaba Cloud account.

      For more information, see Create an AccessKey pair.

    2. Install CloudMonitor SDK for Java in the development environment.

      Add the following dependencies to the Maven project. For more information, see SDK reference.

      <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>tea-openapi</artifactId>
        <version>0.0.13</version>
      </dependency>
      
      <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>cms20190101</artifactId>
        <version>1.0.1</version>
      </dependency>
  2. Call the PutEventRule operation of CloudMonitor to create an interruption event-triggered alert rule for a preemptible instance.

    Sample Java code:

    com.aliyun.cms20190101.Client client = Sample.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));import com.aliyun.cms20190101.models.*;
    import com.aliyun.teaopenapi.models.*;
    
    /**
     * Call the PutEventRule operation to create or modify an event-triggered alert rule. 
     */
    public class Sample {
    
        // Initialize request parameters. 
        public static com.aliyun.cms20190101.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
            # Specify the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
            Config config = new Config().setAccessKeyId(accessKeyId).setAccessKeySecret(accessKeySecret);
            // Specify the endpoint of the API operation. 
            config.endpoint = "metrics.cn-hangzhou.aliyuncs.com";
            return new com.aliyun.cms20190101.Client(config);
        }
    
        public static void main(String[] args_) throws Exception {
            java.util.List<String> args = java.util.Arrays.asList(args_);
            // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured in the code runtime. 
            // If the project code is leaked, the AccessKey pair may be leaked and security issues may occur on all resources of your account. The following sample code obtains an AccessKey pair based on environment variables. We recommend that you use Security Token Service (STS). 
            com.aliyun.cms20190101.Client client = Sample.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            PutEventRuleRequest.PutEventRuleRequestEventPattern eventPattern0 = new PutEventRuleRequest.PutEventRuleRequestEventPattern()
                    // Specify the type of the event-triggered alert rule. 
                    .setEventTypeList(java.util.Arrays.asList("*"))
                    // Specify the level of the event-triggered alert rule. 
                    .setLevelList(java.util.Arrays.asList("*"))
                    // Specify a name for the event-triggered alert rule.
                    .setNameList(java.util.Arrays.asList("Instance:PreemptibleInstanceInterruption"))
                    // Specify the Alibaba Cloud service to which the event-triggered alert rule is applied.
                    .setProduct("ECS");
            PutEventRuleRequest putEventRuleRequest = new PutEventRuleRequest()
                    // Specify a name for the alert rule. 
                    .setRuleName("spot_release_event_test")
                    .setEventPattern(java.util.Arrays.asList(eventPattern0))
                    // Specify the type of the event-triggered alert rule. 
                    .setEventType("SYSTEM")
                    // Specify the state of the event-triggered alert rule. 
                    .setState("ENABLED");
            // Obtain the response. 
            client.putEventRule(putEventRuleRequest);
        }
    }
  3. Create an MNS queue by using Alibaba Cloud MNS.

    For the sample code on how to create an MNS queue, see the "Step 2: Create a queue" section of Manage queues.

  4. Call the PutEventRuleTargets operation of CloudMonitor to configure notification methods for the alert rule and deliver messages to the MNS queue.

    Sample Java code:

    import com.aliyun.cms20190101.models.*;
    import com.aliyun.teaopenapi.models.*;
    
    /**
     * Call the PutEventRuleTargets operation to add or modify an MNS queue. 
     */
    public class Sample {
    
        // Initialize request parameters. 
        public static com.aliyun.cms20190101.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
            # Specify the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
            Config config = new Config().setAccessKeyId(accessKeyId).setAccessKeySecret(accessKeySecret);
            // Specify the endpoint of the API operation. 
            config.endpoint = "metrics.cn-hangzhou.aliyuncs.com";
            return new com.aliyun.cms20190101.Client(config);
        }
    
        public static void main(String[] args_) throws Exception {
            // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured in the code runtime. 
            // If the project code is leaked, the AccessKey pair may be leaked and security issues may occur on all resources of your account. The following sample code obtains an AccessKey pair based on environment variables. We recommend that you use STS. 
            com.aliyun.cms20190101.Client client = Sample.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            PutEventRuleTargetsRequest.PutEventRuleTargetsRequestMnsParameters mnsParameters0 = new PutEventRuleTargetsRequest.PutEventRuleTargetsRequestMnsParameters()
                    // Specify the region ID of the MNS queue. 
                    .setRegion("cn-hangzhou")
                    // Specify the ID of the MNS queue. 
                    .setId("1")
                    // Specify the name of the MNS queue. 
                    .setQueue("mq-test");
            PutEventRuleTargetsRequest putEventRuleTargetsRequest = new PutEventRuleTargetsRequest()
                    // Specify the name of the alert rule. 
                    .setRuleName("spot_release_event_test")
                    .setMnsParameters(java.util.Arrays.asList(
                            mnsParameters0
                    ));
            // Obtain the response. 
            client.putEventRuleTargets(putEventRuleTargetsRequest);
        }
    }
  5. Call the SendDryRunSystemEvent operation of CloudMonitor to send the request to simulate an interruption event.

    Sample Java code:

    import com.aliyun.cms20190101.models.*;
    import com.aliyun.teaopenapi.models.*;
    
    /**
     * Call the SendDryRunSystemEvent operation to debug a system event of an Alibaba Cloud resource. 
     */
    public class Sample {
    
        // Initialize request parameters. 
        public static com.aliyun.cms20190101.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
            # Specify the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
            Config config = new Config().setAccessKeyId(accessKeyId).setAccessKeySecret(accessKeySecret);
            // Specify the endpoint of the API operation. 
            config.endpoint = "metrics.cn-hangzhou.aliyuncs.com";
            return new com.aliyun.cms20190101.Client(config);
        }
    
        public static void main(String[] args_) throws Exception {
            // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured in the code runtime. 
            // If the project code is leaked, the AccessKey pair may be leaked and security issues may occur on all resources of your account. The following sample code obtains an AccessKey pair based on environment variables. We recommend that you use STS. 
            com.aliyun.cms20190101.Client client = Sample.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            SendDryRunSystemEventRequest sendDryRunSystemEventRequest = new SendDryRunSystemEventRequest()
                    // Specify the name of the Alibaba Cloud service. 
                    .setProduct("ecs")
                    // Specify the name of the alert event. 
                    .setEventName("Instance:PreemptibleInstanceInterruption")
                    // Specify the content of the event that you want to simulate. 
                    .setEventContent("{\"product\":\"ECS\",\"resourceId\":\"acs:ecs:cn-shanghai:133160284996****:instance/i-abcdef\",\"level\":\"WARN\",\"instanceName\":\"instanceName\",\"regionId\":\"cn-beijing\",\"name\":\"Instance:PreemptibleInstanceInterruption\",\"content\": {\"instanceId\":\"i-abcdef****\",\"action\":\"delete\"},\"status\":\"Normal\"}");
            // Obtain the response. 
            client.sendDryRunSystemEvent(sendDryRunSystemEventRequest);
        }
    }
  6. Receive messages of the simulated event in the MNS queue.

    For the sample code on how to receive messages from a queue, see the "Step 4: Receive and delete the message" section of Manage queues.