All Products
Search
Document Center

Elastic Compute Service:Use auto provisioning group-related API operations to batch create ECS instances

Last Updated:May 23, 2024

If you want to create a large number of pay-as-you-go Elastic Compute Service (ECS) instances, you can call API operations to batch create the instances in a more efficient manner. This topic describes how to batch create pay-as-you-go ECS instances by compiling Java code to call the CreateAutoProvisioningGroup operation. This topic also describes the differences between the features, advantages, and disadvantages of the RunInstances and CreateAutoProvisioningGroup operations.

Background information

The RunInstances operation is commonly used to create pay-as-you-go ECS instances. You can call the operation to batch create up to 100 ECS instances. However, if you want to batch create more than 100 ECS instances, you may encounter technical bottlenecks when you call the RunInstances operation. For more information, see the Possible issues caused by using the RunInstances operation section of this topic.

Note

If you are familiar with the technical bottlenecks caused by using the RunInstances operation, skip the preceding section.

To allow more than 100 ECS instances to be batch created, Alibaba Cloud provides auto provisioning groups. You can call the CreateAutoProvisioningGroup operation to create an auto provisioning group and use the group to deploy an instance cluster across billing methods, instance families, and zones within a few clicks. The CreateAutoProvisioningGroup operation is more suitable than the RunInstances operation when you want to batch create a large number of ECS instances. For information about the differences between and benefits of the two operations, see the Differences between RunInstances and CreateAutoProvisioningGroup and Benefits of auto provisioning groups sections of this topic.

Differences between RunInstances and CreateAutoProvisioningGroup

The following table describes the differences between the features of the RunInstances and CreateAutoProvisioningGroup operations to help you select an appropriate operation to create ECS instances.

Item

RunInstances

CreateAutoProvisioningGroup

Maximum number of instances that can be batch created

100.

1,000 (up to 10,000 vCPUs).

Capacity delivery method

Specific number of instances

Specific number of instances, specific number of vCPUs, and instance types of different weights.

Support for multiple zones

No.

Yes.

Support for multiple instance types

No.

Yes.

Support for multiple disk categories

No.

Yes.

Provisioning of policies that you can use to create instances

No.

Yes. The following policies are provided:

  • For pay-as-you-go instances

    • Cost optimization policy: The auto provisioning group selects the lowest-cost instance types from the candidate instance types to create instances.

    • Priority-based policy: The auto provisioning group attempts to create instances based on the priorities configured for candidate instance types.

  • For preemptible instance

    • Cost optimization policy: The auto provisioning group selects the lowest-cost instance types from the candidate instance types to create instances.

    • Balanced distribution policy: The auto provisioning group evenly distributes instances across the specified zones.

    • Capacity-optimized distribution policy: The auto provisioning group selects the optimal combinations of instance types and zones to create instances based on resource availability.

Delivery stability

Affected by resource availability.

Multiple combinations of instance types and zones reduce the impacts of resource availability.

Response method

Returns a response in real time.

Returns a response in real time.

The following sample scenarios describe how to use the CreateAutoProvisioningGroup operation instead of the RunInstances operation to create ECS instances:

  • You called the RunInstances operation to batch create instances of a single instance type in a single zone. When you call the CreateAutoProvisioningGroup operation instead to batch create instances, you need to only configure a combination of instance types and zones.

  • You configured a business deployment plan when you called the RunInstances operation to batch create instances. Instead, you can call the CreateAutoProvisioningGroup operation to deploy instances that have multiple categories of disks attached across multiple instance types and zones, based on different instance provisioning policies provided by Auto Provisioning.

    For example, you manually configured a business deployment plan that traversed multiple instance types and zones to create instances when you called the RunInstances operation to improve the success rate of creating instances. If you call the CreateAutoProvisioningGroup operation instead to batch create a large number of instances, you need to only configure multiple combinations of instance types and zones, and select an appropriate policy. The created auto provisioning group automatically batch creates instances based on the specified configurations and policy.

Important

Limits apply to policies that are used for auto provisioning groups to create instances. Up to 1,000 instances can be created at the same time. If you specify the weight of each specified instance type by using the WeightedCapacity parameter, the maximum weighted capacity that is created each time is 10,000.

Possible issues caused by using the RunInstances operation

The limits of the RunInstances operation may cause the following issues when you call the operation to create instances.

Issue

Description

Solution

A limited number of instances can be batch created.

You can call the RunInstances operation to batch create up to 100 ECS instances.

If you want to create more than 100 ECS instances, you must call this operation multiple times in a cyclic or concurrent manner.

The success rate of batch creating instances is not ensured.

Each time you call the RunInstances operation to batch create instances, the operation supports only a single instance type and a single zone. When you call the operation to batch create instances, the following issues may occur:

  • Instances cannot be created due to insufficient resources of a specific instance type.

  • Instances of a specific instance type can no longer be created due to the retirement of the instance type.

  • Instances cannot be created because the specific instance type is unavailable in the specific zone.

  • Instances cannot be created because the specific instance type does not support specific disk categories.

  • Batch instance creation fails mainly due to insufficient resources. To solve this issue, we recommend that you call the DescribeAvailableResource operation to query available instance types in zones in advance. You can configure multiple combinations of instance types and zones to ensure sufficient resources for you to batch create ECS instances. This instance creation method helps improve business delivery stability.

    After you configure multiple combinations of instance types and zones, create an appropriate policy to batch create instances. For example, you can create 100 ECS instances in sequence based on multiple combinations that you configured. If a combination can be used to create at most 50 instances by using the available resources, 50 instances more can be created by using another combination.

  • Take note of the limits of instance types. You can call the DescribeAvailableResource operation to query the limits of instance types. Then, you can develop limit-specific fault tolerance solutions to prevent the impacts induced by usage limit changes.

    Note

    You can view the limits of instance types in each instance family. For more information, see Overview of instance families.

    Sample scenario: The ecs.g6e.large instance type supports only the disk category of enhanced SSDs (ESSDs), but Beijing Zone X (cn-beijing-x) does not support ESSDs.

Simple policies that are used to create instances cannot meet diversified business requirements.

The RunInstances operation can be used to create instances only of a single instance type in a single zone. If you want to deploy instances across zones to implement geo-disaster recovery or create ECS instances at minimal costs, you must create a business deployment plan to ensure that instances are deployed. When you create a business deployment plan, you may encounter the following issues:

  • High development costs: You may need to resolve several issues when you use the business deployment plan that you configured. For example, ECS instances may fail to be created when resources are insufficient, and computing power cannot be ensured at the lowest cost of preemptible instances when you scale up your servers.

  • Low stability and less professionalism: A business deployment plan that you create for the resources provided by Alibaba Cloud cannot be tested and is less likely to be created in a professional manner. This causes risks to the production environment.

To resolve the issue, you may contact Alibaba Cloud for assistance.

Benefits of auto provisioning groups

Alibaba Cloud provides auto provisioning groups to address the issues that you may encounter when you call the RunInstances operation to batch create ECS instances. Auto provisioning groups allow you to deploy an instance cluster across billing methods, instance families, and zones with a few clicks. Auto provisioning groups provide stable computing power, reduce the instability caused by the reclaim mechanism of preemptible instances, and eliminate the need to manually create instances. The following table describes the benefits of auto provisioning groups.

Benefit

Description

Batch creation of a large number of ECS instances

You can use an auto provisioning group to batch create up to 1,000 ECS instances.

Creation of instances that have multiple categories of disks attached across instance types and zones

Auto provisioning groups allow you to configure up to 10 combinations of instance types and zones and to select up to five disk categories, which ensures high availability of batch instance creation.

Sample scenario:

When you create ECS instances based on the balanced distribution policy provided by an auto provisioning group, you can configure multiple instance types and zones. As required by the policy, instances must be created in a relatively balanced manner across zones. However, if the required number of instances cannot be created in a zone, the auto provisioning group attempts to create the same number of instances in other zones.

If you specify multiple disk categories, the auto provisioning group creates disks for instances based on the priorities of the disk categories, and changes a disk category if the disk category is unavailable.

Note

If all disk categories are unavailable, the auto provisioning group selects another creation method.

Support for multiple policies to create instances

Auto provisioning groups provide the following policies to create instances:

  • For pay-as-you-go instances

    • Cost optimization policy: The auto provisioning group selects the lowest-cost instance types from the candidate instance types to create instances.

    • Priority-based policy: The auto provisioning group attempts to create instances based on the priorities configured for candidate instance types.

  • For preemptible instances

    • Cost optimization policy: The auto provisioning group selects the lowest-cost instance types from the candidate instance types to create instances.

    • Balanced distribution policy: The auto provisioning group evenly distributes instances across the specified zones.

    • Capacity-optimized distribution policy: The auto provisioning group selects the optimal combinations of instance types and zones to create instances based on resource availability.

Improved availability of preemptible instances

The demand for preemptible instances is increasing due to the advantages in prices of preemptible instances. However, the prices of preemptible instances fluctuate, and the instances are reclaimed when bid prices become lower than current market prices after protection periods elapse. Therefore, preemptible instances are difficult to manage. Auto provisioning groups help you improve the availability of preemptible instances that remain low costs. The following items describe how to improve the availability of preemptible instances:

  • You can use the default cost optimization policy. If you use the default policy, the auto provisioning group attempts to create instances in ascending order of instance type prices each time the group is scaled out.

  • For preemptible instances, the resources that correspond to instance types are different from the resources that correspond to zones. You can configure multiple combinations of instances types and zones for preemptible instances to reduce the probability that no resources are available for all combinations.

  • You can configure multiple disk categories when you create an auto provisioning group. This ensures that the system can select appropriate disk categories to create instances.

  • You can configure the SpotInstancePoolsToUseCount parameter to ensure that preemptible instances are created by using the lowest-cost combinations of instance types and zones. This can help prevent the issue that computing power is significantly reduced when instances of an instance type are recycled.

Best practices for creating auto provisioning groups by calling the CreateAutoProvisioningGroup operation

This section provides the sample Java code that is used to create an auto provisioning group (CreateAutoProvisioningGroup). This section also describes how to call the CreateAutoProvisioningGroup operation.

  1. Install ECS SDK for Java and the Alibaba Cloud SDK core library.

    For more information, see Install ECS SDK for Java.

  2. Compile the Java code that is used to call the CreateAutoProvisioningGroup operation.

    Sample code:

    CreateAutoProvisioningGroupRequest request = new CreateAutoProvisioningGroupRequest();
    request.setRegionId(regionId);
    request.setLaunchConfigurationImageId(RequestHelper.IMAGE_ID);
    request.setLaunchConfigurationSecurityGroupId(securityGroupId);
    
    request.setTotalTargetCapacity(totalTargetCapacity);
    request.setPayAsYouGoTargetCapacity(payAsYouGoTargetCapacity);
    request.setSpotTargetCapacity(spotTargetCapacity);
    request.setLaunchConfigurationSystemDiskCategory("cloud_ssd");
    request.setLaunchConfigurationSystemDiskSize(40);
    request.setAutoProvisioningGroupType("instant");
    // Configure the policy that is used to create preemptible instances.
    request.setSpotAllocationStrategy("lowest-price");
    request.setSpotInstancePoolsToUseCount(spotInstancePoolsToUseCount);
    // Configure the policy that is used to create pay-as-you-go instances.
    request.setPayAsYouGoAllocationStrategy("prioritized");
    request.setMaxSpotPrice(maxSpotPrice);
    // Configure up to 10 combinations of instance types and zones.
    request.setLaunchTemplateConfigs(launchTemplateConfigs);
    request.setClientToken(clientToken);
    CreateAutoProvisioningGroupResponse response = client.getAcsResponse(request);

    Sample response in the JSON format:

    {
        "autoProvisioningGroupId":"apg-****",
        "launchResults":[
            {
                "instanceIds":[
                    "i-****"
                ],
                "instanceType":"ecs.c5.large",
                "spotStrategy":"NoSpot",
                "zoneId":"cn-shanghai-b"
            },
           {
                "instanceIds":[],
                "instanceType":"ecs.c5.large",
                "spotStrategy":"NoSpot",
                "zoneId":"cn-shanghai-b",
                "errorCode" : "Invalid.Parameter",
                "errorMsg" : "Specific Parameter 'imageId' is not valid"
            }
        ],
        "requestId":"20DA1E9F-BF7F-4BE7-8204-E4DE58E4FC7B"
    }

    When you call the CreateAutoProvisioningGroup operation to create an auto provisioning group, configure items that are used to batch create instances. You do not need to worry about the creation process. The auto provisioning group creates instances in a best-effort manner.

    Note

    In a best-effort manner, the system attempts to use one combination to batch create instances, and if the combination cannot be used to create enough instances, the system uses another combination of instance types and zones to batch create instances. The best-effort instance creation method requires an extended period of time, and the creation result may deviate from the creation policy that you configured.