This topic describes how to use the Alibaba Cloud ECS Java SDK to call the DescribeAvailableResource operation to query the list of available instance types in a zone. You can query available resources before you create ECS instances.

Prerequisites

You must use aliyun-java-sdk-ecs V4.6.3 or later.

Background information

Before you create an ECS instance, you can call the DescribeAvailableResource operation to view the available instance resources in the specified region or zone. For more information, see DescribeAvailableResource. You can set object parameters such as region, zone, billing method, and instance family to query available instance resources. You can also set parameters of I/O optimization, system disk, and data disk to filter the query results.

Query the instance types available for subscription instances in a specific region

The following example demonstrates how to query available instance types to create subscription instances in the China (Hangzhou) region.

    /**
     *  Assume that you query the instance types that are available for subscription instances in the China (Hangzhou) region.
     *  Target region: cn-hangzhou
     *  If all zones are to be selected, zoneId is not specified.
     *  Subscription: Set the InstanceChargeType parameter to PrePaid. Leave the SpotStrategy parameter unspecified or set the parameter to NoSpot.
     *
     *  Note: If you set the DestinationResource parameter to InstanceType, you must specify the IoOptimized parameter.
     *  Steps:
     *  1. Query available I/O optimized resources
     *  2. Query available instance types
     *  Response:
     *     A list of instance types by zone ID is returned.
     *     If no instance types are available, null or an empty map is returned.
     */
    public Map<String,Set<String>> doDescribeScene1() {
        DescribeAvailableResourceRequest describe = new DescribeAvailableResourceRequest();
        describe.setRegionId("cn-hangzhou");
        describe.setInstanceChargeType("PrePaid");
        // 1. Query available I/O optimized resources
        describe.setDestinationResource(IoOptimized);
        Map<String, Set<String>> ioOptimizeds = doActionAndProcessResponse(describe);
        if (null == ioOptimizeds) {
            return null;
        }
        // Set allTypes to specify a list of instance types, set the zone ID as the key, and enter the available instance types in the corresponding zone as the value.
        Map<String,Set<String>> allTypes = new HashMap<String, Set<String>>();
        for (String zoneId : ioOptimizeds.keySet()) {
            describe.setZoneId(zoneId);
            describe.setDestinationResource(InstanceType);
            for(String iopts : ioOptimizeds.get(zoneId)){
                describe.setIoOptimized(iopts);
            }

            Set<String> allTypesInZoneId = allTypes.get(zoneId);
            Map<String, Set<String>> types = doActionAndProcessResponse(describe);
            Set<String> typesInZoneId = types.get(zoneId);
            if(null ! = allTypesInZoneId){
                allTypesInZoneId.addAll(typesInZoneId);
            }else{
                allTypes.put(zoneId, typesInZoneId);
            }
        }
        return allTypes;
    }

Query the instance types available for pay-as-you-go instances in a specific region

The following example demonstrates how to query available instance types to create pay-as-you-go instances in the China (Hangzhou) region.

   /**
     Assume that you are querying the instance types that are available for pay-as-you-go instances in the China (Hangzhou) region.
     *  Target region: cn-hangzhou
     *  If all zones are to be selected, zoneId is not specified.
     *  Pay-as-you-go: Leave the InstanceChargeType parameter unspecified or set the parameter to PostPaid. Leave the SpotStrategy parameter unspecified or set the parameter to NoSpot.
     *
     *  Note: If you set the DestinationResource parameter to InstanceType, you must specify the IoOptimized parameter.
     *  Steps:
     *  1. Query available I/O optimized resources
     *  2. Query available instance types
     *  Response:
     *     A list of instance types by zone ID is returned.
     *     If no instance types are available, null or an empty map is returned.
     */
    public Map<String,Set<String>> doDescribeScene2() {
        DescribeAvailableResourceRequest describe = new DescribeAvailableResourceRequest();
        describe.setRegionId("cn-hangzhou");
        describe.setInstanceChargeType("PostPaid");
        // 1. Query available I/O optimized resources
        describe.setDestinationResource(IoOptimized);
        Map<String, Set<String>> ioOptimizeds = doActionAndProcessResponse(describe);
        if (null == ioOptimizeds) {
            return null;
        }
        // Set allTypes to specify a list of instance types, set the zone ID as the key, and enter the available instance types in the corresponding zone as the value.
        Map<String,Set<String>> allTypes = new HashMap<String, Set<String>>();
        for (String zoneId : ioOptimizeds.keySet()) {
            describe.setZoneId(zoneId);
            describe.setDestinationResource(InstanceType);
            for(String iopts : ioOptimizeds.get(zoneId)){
                describe.setIoOptimized(iopts);
            }

            Set<String> allTypesInZoneId = allTypes.get(zoneId);
            Map<String, Set<String>> types = doActionAndProcessResponse(describe);
            Set<String> typesInZoneId = types.get(zoneId);
            if(null ! = allTypesInZoneId){
                allTypesInZoneId.addAll(typesInZoneId);
            }else{
                allTypes.put(zoneId, typesInZoneId);
            }
        }

        return allTypes;
    }

Query the instance types available for preemptible instances in a specific region

The following example demonstrates how to query available instance types to create preemptible instances in the China (Hangzhou) region.

    /**
     Assume that you are querying the instance types that are available for preemptible instances in the China (Hangzhou) region.
     *  Target region: cn-hangzhou
     *  If all zones are to be selected, zoneId is not specified.
     *  Pay-as-you-go: Leave the InstanceChargeType parameter unspecified or set the parameter to PostPaid. Set the SpotStrategy parameter to SpotWithPriceLimit or SpotAsPriceGo.
     *  Note: If you set the DestinationResource parameter to InstanceType, you must specify the IoOptimized parameter.
     *  Steps:
     *  1. Query available I/O optimized resources
     *  2. Query available instance types
     *  Response:
     *     A list of instance types by zone ID is returned.
     *     If no instance types are available, null or an empty map is returned.
     */
    public Map<String,Set<String>> doDescribeScene3() {
        DescribeAvailableResourceRequest describe = new DescribeAvailableResourceRequest();
        describe.setRegionId("cn-hangzhou");
        describe.setInstanceChargeType("PostPaid");
        describe.setSpotStrategy("SpotWithPriceLimit");
        // describe.setSpotStrategy("SpotAsPriceGo");
        // 1. Query available I/O optimized resources
        describe.setDestinationResource(IoOptimized);
        Map<String, Set<String>> ioOptimizeds = doActionAndProcessResponse(describe);
        if (null == ioOptimizeds) {
            return null;
        }
        // Set allTypes to specify a list of instance types, set the zone ID as the key, and enter the available instance types in the corresponding zone as the value.
        Map<String,Set<String>> allTypes = new HashMap<String, Set<String>>();
        for (String zoneId : ioOptimizeds.keySet()) {
            describe.setZoneId(zoneId);
            describe.setDestinationResource(InstanceType);
            for(String iopts : ioOptimizeds.get(zoneId)){
                describe.setIoOptimized(iopts);
            }

            Set<String> allTypesInZoneId = allTypes.get(zoneId);
            Map<String, Set<String>> types = doActionAndProcessResponse(describe);
            Set<String> typesInZoneId = types.get(zoneId);
            if(null ! = allTypesInZoneId){
                allTypesInZoneId.addAll(typesInZoneId);
            }else{
                allTypes.put(zoneId, typesInZoneId);
            }
        }

        return allTypes;
    }

Query the zones where a specific instance type is available

The following example demonstrates how to query the zones where a specific subscription instance type is available.

   /**
     *  Assume that you are querying the zones where the ecs.gn4.8xlarge instance type is available for subscription instances.
     *  Target region: cn-hangzhou
     *  If all zones are to be selected, zoneId is not specified.
     *  Target instance type: ecs.gn4.8xlarge
     *  Subscription: Set the InstanceChargeType parameter to PrePaid. Leave the SpotStrategy parameter unspecified or set the parameter to NoSpot.
     *
     *  Note: If you set the DestinationResource parameter to InstanceType, you must specify the IoOptimized parameter.
     *  Steps:
     *  1. Query available I/O optimized resources
     *  2. Query available instance types
     *  Response:
     *     The list of zones where the specified instance type is available
     *     If the specified instance type is unavailable in all zones, null or an empty value is returned.
     */
    public List<String> doDescribeScene4() {
        DescribeAvailableResourceRequest describe = new DescribeAvailableResourceRequest();
        describe.setRegionId("cn-hangzhou");
        describe.setInstanceChargeType("PrePaid");
        describe.setInstanceType("ecs.gn4.8xlarge");
        // 1. Query available I/O optimized resources
        describe.setDestinationResource(IoOptimized);
        Map<String, Set<String>> ioOptimizeds = doActionAndProcessResponse(describe);
        if (null == ioOptimizeds) {
            return null;
        }
        // Zones where the specified instance type is available
        List<String> zones = new ArrayList<String>(ioOptimizeds.size());
        for (String zoneId : ioOptimizeds.keySet()) {
            describe.setZoneId(zoneId);
            describe.setDestinationResource(InstanceType);
            for(String iopts : ioOptimizeds.get(zoneId)){
                describe.setIoOptimized(iopts);
            }
            Map<String, Set<String>> typesMap = doActionAndProcessResponse(describe);
            Set<String> types = typesMap.get(zoneId);
            if(CollectionUtils.isNotEmpty(types)){
                if(types.contains("ecs.gn4.8xlarge")){
                    zones.add(zoneId);
                }
            }
        }
        if(CollectionUtils.isNotEmpty(zones)){
            return zones;
        }
        return null;
    }

Query the instance types available for VPC-type subscription instances in a specific zone

The following example demonstrates how to query available instance types to create VPC-type subscription instances in the China (Hangzhou) region.

    /**
     *  Assume that you are querying the instance types that are available for VPC-type subscription instances in Hangzhou Zone E.
     *  Target region: cn-hangzhou
     *  Target zone: cn-hangzhou-e
     *  Target network type: VPC
     *  Subscription: Set the InstanceChargeType parameter to PrePaid. Leave the SpotStrategy parameter unspecified or set the parameter to NoSpot.
     *
     *  Note: If you set the DestinationResource parameter to InstanceType, you must specify the IoOptimized parameter.
     *  Steps:
     *  1. Query available I/O optimized resources
     *  2. Query available instance types
     *  Response:
     *     The list of qualified instance types that are available in the specified zone.
     *     If no qualified instance type is available in the specified zone, null or an empty value is returned.
     */
    public List<String> doDescribeScene5() {
        DescribeAvailableResourceRequest describe = new DescribeAvailableResourceRequest();
        describe.setRegionId("cn-hangzhou");
        describe.setZoneId("cn-hangzhou-e");
        describe.setInstanceChargeType("PrePaid");
        describe.setNetworkCategory("Vpc");
        // Query available I/O optimized resources
        describe.setDestinationResource(IoOptimized);
        Map<String, Set<String>> ioOptimizedMap = doActionAndProcessResponse(describe);
        if (null == ioOptimizedMap) {
            return null;
        }
        Set<String> ioOptimizeds = ioOptimizedMap.get("cn-hangzhou-e");
        if(CollectionUtils.isEmpty(ioOptimizeds)) {
            return null;
        }
        // The instance types that meet the specifications
        Set<String> types = new HashSet<String>();
        describe.setDestinationResource(InstanceType);

        for(String iopts : ioOptimizeds){
            describe.setIoOptimized(iopts);
            Map<String, Set<String>> typesMap = doActionAndProcessResponse(describe);
            Set<String> typesInMap = typesMap.get("cn-hangzhou-e");
            if(CollectionUtils.isNotEmpty(typesInMap)){
                types.addAll(typesInMap);
            }
        }
        if(CollectionUtils.isNotEmpty(types)){
            return new ArrayList<String>(types);
        }
        return null;
    }

What to do next

Batch create ECS instances