All Products
Search
Document Center

Elastic Compute Service:Change the instance type of an ECS instance

Last Updated:Sep 06, 2023

This topic describes how to use the Alibaba Cloud Elastic Compute Service (ECS) SDK for Java 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

The following requirements are met:

  • You do not have an overdue payment for the pay-as-you-go instance whose instance type you want to change.

  • The subscription instance whose instance type you want to change is not in the Expired state.

  • The instances are in the Stopped state.

Change the instance type of a subscription instance

The following sample 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) {
        // 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 the security of resources within your account may be compromised. The following sample code shows how to use environment variables to obtain an AccessKey pair and use the AccessKey pair to call API operations. We recommend that you use Security Token Service (STS) tokens, which provide higher security. 
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        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*****");
        // Specify the new instance type. You can call the ModifyPrepayInstanceSpecRequest operation 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 sample 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) {
        // 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 the security of resources within your account may be compromised. The following sample code shows how to use environment variables to obtain an AccessKey pair and use the AccessKey pair to call API operations. We recommend that you use STS tokens, which provide higher security. 
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        IAcsClient client = new DefaultAcsClient(profile);
        // Create an API request and configure the parameters. 
        ModifyInstanceSpecRequest request = new ModifyInstanceSpecRequest();
        request.setRegionId("default");
        // Required. Specify the ID of the instance. 
        request.setInstanceId("i-bp1gc5z6103qs2t40***");
        // Specify the new instance type. You can call the ModifyInstanceSpecRequest operation 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());
        }

    }
}