This topic describes how to use the Alibaba Cloud ECS Java SDK to call the ModifyInstanceSpec operation to change the instance type of a pay-as-you-go instance and call the ModifyPrepayInstanceSpec operation to change the instance type of a subscription instance.

Prerequisites

When you change the instance type of an ECS instance, note that:
  • You must not have overdue payments for the pay-as-you-go instance.
  • The subscription instance cannot be in the Expired state.
  • The instance must be in the Stopped state.

Change the instance type of a subscription instance

The following code can be used to call the ModifyPrepayInstanceSpec operation to change the instance type of a subscription instance to ecs.g5.large in the China (Hangzhou) region:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import java.util.*;
import com.aliyuncs.ecs.model.v20140526.*;

public class ModifyPrepayInstanceSpec {

    public static void main(String[] args) {
        // Create and initialize a DefaultAcsClient instance.
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<yourAccessKeyId>", "<yourAccessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);
        // Create an API request and configure the parameters.
        ModifyPrepayInstanceSpecRequest request = new ModifyPrepayInstanceSpecRequest();
        request.setRegionId("cn-hangzhou");
        request.setInstanceId("i-bp1jd3uddaduyo8*****");
        // Set the new instance type. ModifyPrepayInstanceSpecRequest allows you to upgrade and downgrade the instance type.        
        request.setInstanceType("ecs.g5.large");

        try {
            ModifyPrepayInstanceSpecResponse response = client.getAcsResponse(request);
            logInfo(response.getOrderId());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }
    private static void logInfo(String message) {
        System.out.println(message);
    }
}

Change the instance type of a pay-as-you-go instance

The following code can be used to call the ModifyInstanceSpec operation to change the instance type of a pay-as-you-go instance to ecs.g5.large in the China (Hangzhou) region:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.alibaba.fastjson.JSON;
import java.util.*;
import com.aliyuncs.ecs.model.v20140526.*;

public class ModifyInstanceSpec {

    public static void main(String[] args) {
        // Create and initialize a DefaultAcsClient instance.
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<yourAccessKeyId>", "<yourAccessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);
        // Create an API request and configure the parameters.
        ModifyInstanceSpecRequest request = new ModifyInstanceSpecRequest();
        request.setRegionId("default");
        // You must specify the instance ID.
        request.setInstanceId("i-bp1gc5z6103qs2t40***");
        // Set the new instance type. ModifyInstanceSpecRequest allows you to upgrade and downgrade the instance type.
        request.setInstanceType("ecs.g5.large");

        try {
            ModifyInstanceSpecResponse response = client.getAcsResponse(request);
            System.out.println(JSON.toJSONString(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }
}