This topic describes how to use Alibaba Cloud SDK for Java to call DescribeInstances operation. By doing so, you can obtain ECS instance configurations such as Internet IP address and instance ID.

Prerequisites

Before you begin, make sure that you have completed the following:
  • To use Alibaba Cloud SDK for Java, you must have an Alibaba Cloud account and an AccessKey pair. You can create and view your AccessKey pair on the AccessKey Management page in the Alibaba Cloud Console.
  • You have installed Alibaba Cloud SDK for Java. For SDK version details, visit Alibaba Cloud SDK.
    <dependencies>
        <! -- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.3</version>
        </dependency>
        <! -- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-ecs-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ecs</artifactId>
            <version>4.17.4</version>
        </dependency>
    </dependencies>

Sample code

This topic uses the following sample code as an example:
 import com.aliyuncs.profile.DefaultProfile;
 import com.aliyuncs.DefaultAcsClient;
 import com.aliyuncs.IAcsClient;
 import com.aliyuncs.exceptions.ClientException;
 import com.aliyuncs.exceptions.ServerException;
 import com.aliyuncs.ecs.model.v20140526.*;
 public class DescribeInstances {
     public static void main(String[] args) {        
        // Initialize request parameters
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>", // Your zone ID
                "<your-access-key-id>", // Your AccessKey ID
                "<your-access-key-secret>") // Your AccessKey Secret
         IAcsClient client = new DefaultAcsClient(profile);
         // Create an API request and set its parameters.
         DescribeInstancesRequest request = new DescribeInstancesRequest();
         request.setPageSize(10);
         request.setConnectTimeout(5000); // Set the connection timeout to 5000 milliseconds
         request.setReadTimeout(5000); // Set the read timeout to 5000 milliseconds.
         // Initiate the request and handle the response or exception.
         DescribeInstancesResponse response;
         try {
             response = client.getAcsResponse(request);
             for (DescribeInstancesResponse.Instance instance:response.getInstances()) {
                 System.out.println(instance.getImageId());
                 System.out.println(instance.getInstanceId());
                 System.out.println(instance.getPublicIpAddress());
             }
         } catch (ServerException e) {
             e.printStackTrace();
         } catch (ClientException e) {
             e.printStackTrace();
         }             
     }
 }