This topic describes how to use Alibaba Cloud SDK for Java to change the instance type of an Elastic Compute Service (ECS) instance, including modifying the Internet bandwidth.

Prerequisites

Before you begin, ensure that the following requirements are met:
  • You must have an Alibaba Cloud account and an AccessKey pair (AccessKey ID and AccessKey secret) to use Alibaba Cloud SDK for Java. You can create and view your AccessKey pair on the AccessKey Management page in the Alibaba Cloud Management Console.
  • You have installed Alibaba Cloud SDK for Java. For more information about SDK versions, see Alibaba Cloud SDK.
    <dependencies>
        <! -- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.3</version>
        </dependency>
        <! -- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-ecs-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ecs</artifactId>
            <version>4.17.4</version>
        </dependency>
    </dependencies>

Sample code

This section shows the sample request code:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.ecs.model.v20140526.DescribeResourcesModificationRequest;
import com.aliyuncs.ecs.model.v20140526.DescribeResourcesModificationResponse;
import com.aliyuncs.ecs.model.v20140526.DescribeResourcesModificationResponse.AvailableZone.AvailableResource.SupportedResource;
import com.aliyuncs.ecs.model.v20140526.ModifyInstanceSpecRequest;
import com.aliyuncs.ecs.model.v20140526.ModifyInstanceSpecResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.List;

/**
 * DescribeResourcesModification    Queries the available resources in a zone when you upgrade or downgrade instance types or system disks.
 * ModifyInstanceSpec    Modifies the instance type and Internet bandwidth of a pay-as-you-go ECS instance.
 */
public class ModifyInstance {

    // An ECS instance in the shutdown state.
    private static String instancenId = "i-bp1gh*****";

    public static void main(String[] args) {
        Gson gson = new Gson();
        IAcsClient client = Initialization();
        // Query the available resources when you upgrade an instance.
        List<DescribeResourcesModificationResponse.AvailableZone> availableZones = DescribeResourcesModification(client);
        System.out.println("--------------------Available resource information for instance rules--------------------");
        System.out.println(gson.toJson(availableZones));
        // Query the available resources of the instance.
        // Proceed based on your actual requirement or frontend services.
        SupportedResource supportedResource = availableZones.get(0).getAvailableResources().get(0).getSupportedResources().get(0);
        // Query specific resources that can be used for creating an instance.
        ModifyInstanceSpecResponse response = ModifyInstanceSpec(client, supportedResource.getValue());
        System.out.println("--------------------The specification of the instance was changed.--------------------");
        System.out.println(gson.toJson(response));
    }

    /**
     * ModifyInstanceSpec    Modifies the instance type and Internet bandwidth of a pay-as-you-go ECS instance.
     */
    private static ModifyInstanceSpecResponse ModifyInstanceSpec(IAcsClient client, String value) {
        ModifyInstanceSpecRequest request = new ModifyInstanceSpecRequest();
        // A specified instance ID.
        request.setInstanceId(instancenId);
        /**
         * Specify whether instance types can be upgraded across clusters. Default value: false.
         * When AllowMigrateAcrossZone is set to true and you upgrade an instance based on the returned information, take note of the following items:
         *
         * Instances of the classic network type:
         * For phased-out instance types, when a non-I/O optimized instance is upgraded to an I/O optimized instance, instance information including the private IP address, disk device name, and software authorization code
         * is changed. For Linux instance types, basic disks (cloud) are identified as xvda or xvdb, and ultra disks (cloud_efficiency)
         * and standard SSDs (cloud_ssd) are identified as vda or vdb.
         * For instance families available for purchase, when the type of instance is changed, the private IP address of the instance is also changed.
         *
         * Instances of the virtual private cloud (VPC) type:
         * For phased-out instance types, when a non-I/O optimized instance is upgraded to an I/O optimized instance, the disk device name and software authorization code are changed. Linux
         * Basic disks (cloud) are identified as xvda or xvdb. Ultra disks (cloud_efficiency) and standard SSDs (cloud_ssd)
         * are identified as vda or vdb.
         */
        request.setAllowMigrateAcrossZone(false);
        request.setAsync(true);
        request.setInstanceType(value);
        try {
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
            throw new RuntimeException();
        }
    }

    /**
     * DescribeResourcesModification    Queries the available resources in a zone when you upgrade or downgrade instance types or system disks.
     */
    private static List<DescribeResourcesModificationResponse.AvailableZone> DescribeResourcesModification(IAcsClient client) {
        DescribeResourcesModificationRequest request = new DescribeResourcesModificationRequest();
        //
        request.setDestinationResource("InstanceType");
        request.setResourceId(instancenId);
        try {
            DescribeResourcesModificationResponse response = client.getAcsResponse(request);
            return response.getAvailableZones();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
            throw new RuntimeException();
        }
    }


    /**
     * Initialization  Initializes common request parameters.
     */
    private static IAcsClient Initialization() {
        // Initialize request parameters.
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>", // The ID of the region.
                "<your-access-key-id>", // The AccessKey ID.
                "<your-access-key-secret>"); // The AccessKey secret.
        return new DefaultAcsClient(profile);
    }
}
                

Output

This section shows the sample success response:
--------------------Available resource information for instance rules--------------------
[
    {
        "regionId": "cn-hangzhou-dg-a01", 
        "zoneId": "cn-hangzhou-h", 
        "status": "Available", 
        "statusCategory": "WithStock", 
        "availableResources": [
            {
                "type": "InstanceType", 
                "supportedResources": [
                    {
                        "value": "ecs.hfc5.large", 
                        "status": "Available", 
                        "statusCategory": "WithStock"
                    }, 
                    {
                        "value": "ecs.t5-c1m1.large", 
                        "status": "Available", 
                        "statusCategory": "WithStock"
                    }, 
                    {
                        "value": "ecs.ic5.3xlarge", 
                        "status": "Available", 
                        "statusCategory": "WithStock"
                    }, 
                    {
                        "value": "ecs.c5.large", 
                        "status": "Available", 
                        "statusCategory": "WithStock"
                    }, 
                    {
                        "value": "ecs.t5-c1m1.xlarge", 
                        "status": "Available", 
                        "statusCategory": "WithStock"
                    }
                ]
            }
        ]
    }
]
--------------------The specification of the instance was changed.--------------------
{"requestId":"BAC795D9-D8F1-4E76-9F69-75EA47B5FD4B"}