edit-icon download-icon

Elastic Compute Service

Last Updated: Mar 20, 2018

Elastic Compute Service (ECS) is a type of computing service that features elastic processing capabilities. ECS has a simpler and more efficient management mode than physical servers. You can create instances, change the operating system, and add or release any quantity of ECS instances at any time to fit your business needs. For more information about ECS, see Elastic Compute Service Documentation.

Example overview

The example in this section shows you how to create an ECS instance by creating a CreateInstanceRequest. To create an ECS instance, obtain the following information:

Code example

Note: Running the code in this example will create an ECS instance and generate fees.

  1. import java.util.UUID;
  2. import com.aliyuncs.DefaultAcsClient;
  3. import com.aliyuncs.IAcsClient;
  4. import com.aliyuncs.ecs.model.v20140526.CreateInstanceRequest;
  5. import com.aliyuncs.ecs.model.v20140526.CreateInstanceResponse;
  6. import com.aliyuncs.exceptions.ClientException;
  7. import com.aliyuncs.exceptions.ServerException;
  8. import com.aliyuncs.profile.DefaultProfile;
  9. public class Demo {
  10. public static void main(String[] args) {
  11. // Create and initialize the DefaultAcsClient instance
  12. DefaultProfile profile = DefaultProfile.getProfile(
  13. "<your-region-id>", // Your region ID
  14. "<your-access-key-id>", // Your AccessKey ID
  15. "<your-access-key-secret>"); // Your AccessKey Secret
  16. IAcsClient client = new DefaultAcsClient(profile);
  17. // Create a request and set parameters
  18. CreateInstanceRequest request = new CreateInstanceRequest();
  19. request.setImageId("alinux_17_01_64_20G_cloudinit_20171222.vhd");
  20. request.setInstanceName("MyEcsInstance");
  21. request.setSecurityGroupId("<your-security-group-id>");
  22. request.setInstanceType("ecs.t1.small");
  23. request.setClientToken(UUID.randomUUID().toString());
  24. // Initiate the request and handle the response or exceptions
  25. CreateInstanceResponse response;
  26. try {
  27. response = client.getAcsResponse(request);
  28. String instanceId = response.getInstanceId();
  29. System.out.println("Create instance success, instanceId = " + instanceId);
  30. } catch (ServerException e) {
  31. e.printStackTrace();
  32. } catch (ClientException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }
Thank you! We've received your feedback.