All Products
Search
Document Center

Elastic Compute Service:Query ECS instances

Last Updated:Sep 14, 2023

This topic describes how to use Alibaba Cloud ECS SDK for Java to call the DescribeInstances operation to query Elastic Compute Service (ECS) instances.

Prerequisites

  • At least one ECS instance is created. For more information, see Batch create ECS instances.

  • Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured in the code runtime environment. For more information, see Configure credentials.

Sample code

The following sample code provides an example on how to query the pay-as-you-go ECS instances that reside in virtual private clouds (VPCs) and use the pay-by-traffic billing method for network usage.

// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.sample;

import com.aliyun.tea.*;
import com.aliyun.darabonba.env.EnvClient;

public class Sample {

    /**
     main.dara Use your AccessKey ID and AccessKey secret to initialize the client.
     main.dara @param accessKeyId
     main.dara @param accessKeySecret
     main.dara @return Client
     main.dara @throws Exception
     */
    public static com.aliyun.ecs20140526.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // Required. Specify your AccessKey ID.
                .setAccessKeyId(accessKeyId)
                // Required. Specify your AccessKey secret.
                .setAccessKeySecret(accessKeySecret);
        // Specify the endpoint that you want to access.
        config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ecs20140526.Client(config);
    }

    /**
    * Use Security Token Service (STS) tokens to initialize the client. This method is recommended. 
    * @param accessKeyId
    * @param accessKeySecret
    * @param securityToken
    * @return Client
    * @throws Exception
    */
    public static com.aliyun.ecs20140526.Client createClientWithSTS(String accessKeyId, String accessKeySecret, String securityToken) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // Required. Specify your AccessKey ID.
                .setAccessKeyId(accessKeyId)
                // Required. Specify your AccessKey secret.
                .setAccessKeySecret(accessKeySecret)
                // Required. Specify your security token.
                .setSecurityToken(securityToken)
                // Required. Specify to use the STS token.
                .setType("sts");
        // Specify the endpoint that you want to access.
        config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ecs20140526.Client(config);
    }

    public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(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 disclosed and security issues may occur in all resources that belong to your Alibaba Cloud account. The following sample code is provided for reference only. We recommend that you use STS tokens, which provide higher security.
        com.aliyun.ecs20140526.Client client = Sample.createClient(EnvClient.getEnv("ALIBABA_CLOUD_ACCESS_KEY_ID"), EnvClient.getEnv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        com.aliyun.ecs20140526.models.DescribeInstancesRequest describeInstancesRequest = new com.aliyun.ecs20140526.models.DescribeInstancesRequest()
                .setRegionId("cn-hangzhou")
                .setInternetChargeType("PayByTraffic")
                .setInstanceChargeType("PostPaid");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        com.aliyun.ecs20140526.models.DescribeInstancesResponse resp = client.describeInstancesWithOptions(describeInstancesRequest, runtime);
        com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(resp));
    }
}