This topic describes how to use the Alibaba Cloud ECS Java SDK to call the RunInstances operation to create one or more ECS instances.


You must query the following information 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 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 VPC, the new ECS instance is automatically added to the VPC.
  • If the security group is in a VPC, call the VPC API DescribeVSwitches operation to query the ID of the VSwitch in the VPC. In this example, the ID of the VSwitch is vsw-bp1wt4qpuavdb6y6k8***.

Background information

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

Note If you call the RunInstances operation, billable resources such as ECS instances can be created and incur fees. If you need to test the sample code, you can set the DryRun parameter in the code to send check requests without creating instances. Check items include whether the required parameters are set, and verifies 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) {
        // Create and initialize a DefaultAcsClient instance.
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<yourAccessKeyId>", "<yourAccessSecret>");
        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();
        // Batch create five ECS instances. If the Amount parameter is not configured, one ECS instance will be created.
        // request.setAmount(5);
        // The minimum number of instance to create if available resources are insufficient.
        // request.setMinAmount(2);

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

        RunInstancesRequest.Tag tag1 = new RunInstancesRequest.Tag();
        // If you enable the precheck parameter function, the system will not create an ECS instance and only check parameter correctness, user permissions, or ECS resource inventory.
        // If the DryRun parameter is configured 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());