All Products
Document Center

Elastic Compute Service:Create multiple ECS instances at a time

Last Updated:Sep 13, 2023

This topic describes how to use Alibaba Cloud ECS SDK for Java to call the RunInstances operation to create one or more Elastic Compute Service (ECS) instances.


The following operations are performed before you create ECS instances:

  • Call the DescribeRegions operation to query the region where you want to create ECS instances. In this example, the region is cn-hangzhou.

  • Call the DescribeImages operation to query the ID of the image that you want to use. In this example, the ID of the image is freebsd_11_02_64_30G_alibase_20190722.vhd.

  • Call the DescribeInstanceTypes operation to query the instance type that you want to select. In this example, the instance type is ecs.g5.large. For more information, see Overview of instance families.

  • Call the DescribeSecurityGroups operation to query IDs of one or more security groups in the specified region. In this example, the ID of the security group is sg-bp1fg655nh68xyz9i***. The network type of the security group determines the network type of the ECS instance. For example, if you choose a security group in a virtual private cloud (VPC), the new ECS instance is automatically added to the VPC.

  • If the security group is in a VPC, call the DescribeVSwitches operation of the VPC API to query the IDs of vSwitches in the VPC. In this example, the vSwitch ID vsw-bp1wt4qpuavdb6y6k8*** is used.

Background information

The following section demonstrates how to call the RunInstances operation to batch create and start ECS instances. For more information, see RunInstances.


If you call the RunInstances operation, billable resources such as ECS instances can be created and fees are incurred. If you want to test the sample code only, you can set the DryRun parameter in the code to perform a dry run without creating instances. Check items include whether the required parameters are set, the request format, service limits, and available ECS instances.

Sample code

The following code can be used to create pay-as-you-go ECS instances that use the pay-by-traffic billing method for network usage in a VPC:

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 java.util.UUID;
import com.aliyuncs.ecs.model.v20140526.*;

public class RunInstances {

    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 environment. 
        // If the project code is leaked, the AccessKey pair may be leaked and security issues may occur on all resources of your account. 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. 
        RunInstancesRequest request = new RunInstancesRequest();

        // Add a 100 GiB standard SSD data disk and enable the Release Disk with Instance feature for the disk. 
        List<RunInstancesRequest.DataDisk> dataDiskList = new ArrayList<RunInstancesRequest.DataDisk>();

        RunInstancesRequest.DataDisk dataDisk1 = new RunInstancesRequest.DataDisk();
        // Set the Amount parameter to 5. If Amount is not set, one ECS instance is created. 
        // request.setAmount(5);
        // Specify the minimum number of instances to create if resources are insufficient. 
        // request.setMinAmount(2);

        List<RunInstancesRequest.Tag> tagList = new ArrayList<RunInstancesRequest.Tag>();

        RunInstancesRequest.Tag tag1 = new RunInstancesRequest.Tag();
        // If you enable the dry run feature, the system checks issues such as parameter correctness, user permissions, or ECS resource inventory without actually creating ECS instances. 
        // If the DryRun parameter is set to true, Amount must be 1 and MinAmount must be empty. You can modify the code as needed. 
        // request.setDryRun(true);

        // Initiate the request and handle the response or exceptions. 
        RunInstancesResponse response;
        try {
            response = client.getAcsResponse(request);


        } catch (ServerException e) {
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());



The following response is returned: