This topic uses CreateSnapshot as an example to demonstrate how to call ECS API operations through developer tools such as Alibaba Cloud CLI, OpenAPI Explorer, and Alibaba Cloud Java SDK.

Background information

Before you call API operations, find the values of required request parameters in the API documentation. If an error is reported after a request is sent, you can obtain the description of the error code in the corresponding API documentation.

Calling methods

Example of calling ECS API operations by using Alibaba Cloud CLI

  1. Obtain the instance ID.
    • If you have connected the ECS instance, you can obtain the instance ID from the instance metadata.
      curl http://100.100.100.200/2016-01-01/meta-data/instance-id
    • On a local computer, you can obtain the instance ID by calling the DescribeInstances operation.
      aliyun ecs DescribeInstances --output cols=InstanceId,InstanceName rows=Instances.Instance[]
  2. Obtain the disk ID by calling the DescribeDisks operation.
    aliyun ecs DescribeDisks --RegionId cn-hangzhou --InstanceId i-bp1afnc98r8k69****** --output cols=DiskId rows=Disks.Disk[]
  3. Create a snapshot based on the disk ID by calling the CreateSnapshot operation.
    aliyun ecs CreateSnapshot --DiskId d-bp19pjyf12hebp******
    If the following message is returned, the snapshot creation task has been initiated.
    {"RequestId":"16B856F6-EFFB-4397-8A8A-CB73FA******","SnapshotId":"s-bp1afnc98r8kjh******"}
  4. Query the progress of snapshot creation by calling the DescribeSnapshots operation.
    If both "SnapshotId"="s-bp1afnc98r8kjh******" and "Status":"accomplished" are displayed, the snapshot is created.
    aliyun ecs DescribeSnapshots --RegionId cn-hangzhou --InstanceId i-bp1afnc98r8k69******

Example of calling ECS API operations by using OpenAPI Explorer

  1. Obtain the instance ID and the disk ID by calling the DescribeInstances operation.
  2. Create a snapshot based on the disk ID by calling the CreateSnapshot operation.
  3. Query the snapshot creation progress by calling the DescribeSnapshots operation.
    If both "SnapshotId"="s-bp1afnc98r8kjh******" and "Status":"accomplished" are displayed, the snapshot is created.

Example of calling ECS API operations by using the Java SDK

You must specify the values of the following parameters in the sample code:

  • <AccessKey>: Enter your AccessKey ID. For information about how to obtain your AccessKey ID, see Create an AccessKey pair.
  • <AccessSecret>: Enter your AccessKey secret.
  • <RegionId>: Enter the region ID of the ECS instance. For more information about the valid values of the parameter, see Regions and zones or DescribeRegions.
  • <DiskId>: Enter the disk ID. For more information about the valid values of the parameter, see DescribeDisks.
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.ecs.model.v20140526.CreateSnapshotRequest;
import com.aliyuncs.ecs.model.v20140526.CreateSnapshotResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;

/* pom.xml
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>3.0.9</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-ecs</artifactId>
    <version>4.10.1</version>
</dependency>
*/  

public class CreateSnapshotExample {

    private String accessKeyId = "<AccessKey>";
    private String accessSecret = "<AccessSecret>";

    /**
     * The region ID of the disk
     */
    private String regionId = "<RegionId>";

    /**
     * The ID of the disk for which you want to create a snapshot
     */
    private String diskId = "<DiskId>";

    public void createSnapshot() {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CreateSnapshotRequest request = new CreateSnapshotRequest();
        request.setRegionId(regionId);
        request.setDiskId(diskId);
        try {
            CreateSnapshotResponse response = client.getAcsResponse(request);
            logInfo(response.getSnapshotId());
        } catch (ServerException e) {
            logInfo(String.format("Fail. Something with your connection with Aliyun go incorrect. ErrorCode: %s",
                e.getErrCode()));
        } catch (ClientException e) {
            logInfo(String.format("Fail. Business error. ErrorCode: %s, RequestId: %s",
                e.getErrCode(), e.getRequestId()));
        }
    }

    private static void logInfo(String message) {
        System.out.println(message);
    }

    public static void main(String[] args) {
        new CreateSnapshotExample().createSnapshot();
    }
}