This topic describes how to use the Alibaba Cloud ECS Java SDK to call the RunInstances operation to create one or more ECS instances.
Prerequisites
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 com.alibaba.fastjson.JSON;
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();
request.setRegionId("cn-hangzhou");
request.setImageId("freebsd_11_02_64_30G_alibase_20190722.vhd");
request.setInstanceType("ecs.g5.large");
request.setSecurityGroupId("sg-bp1fg655nh68xyz9i***");
request.setVSwitchId("vsw-bp1wt4qpuavdb6y6k8***");
request.setInstanceName("MyFirstEcsInstance");
request.setDescription("MyFirstEcsInstance");
request.setInternetMaxBandwidthOut(2);
request.setInternetChargeType("PayByTraffic");
request.setClientToken(UUID.randomUUID().toString());
// 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();
dataDisk1.setSize(100);
dataDisk1.setCategory("cloud_ssd");
dataDisk1.setDeleteWithInstance(true);
dataDiskList.add(dataDisk1);
request.setDataDisks(dataDiskList);
// 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();
tag1.setKey("EcsProduct");
tag1.setValue("DocumentationDemo");
tagList.add(tag1);
request.setTags(tagList);
// 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);
request.setInstanceChargeType("PostPaid");
// Initiate the request and handle the response or exceptions.
RunInstancesResponse response;
try {
response = client.getAcsResponse(request);
System.out.println(JSON.toJSONString(response));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}
Result
Response:
{
"RequestId":"04F0F334-1335-436C-A1D7-6C044FE73368",
"InstanceIdSets":{
"InstanceIdSet":[
"i-instanceid1",
"i-instanceid2",
"i-instanceid3"
]
}
}