Search in sources :

Example 11 with DescribeInstancesRequest

use of com.amazonaws.services.ec2.model.DescribeInstancesRequest in project aws-doc-sdk-examples by awsdocs.

the class DescribeInstances method describeEC2Instances.

// snippet-start:[ec2.java2.describe_instances.main]
public static void describeEC2Instances(Ec2Client ec2) {
    boolean done = false;
    String nextToken = null;
    try {
        do {
            DescribeInstancesRequest request = DescribeInstancesRequest.builder().maxResults(6).nextToken(nextToken).build();
            DescribeInstancesResponse response = ec2.describeInstances(request);
            for (Reservation reservation : response.reservations()) {
                for (Instance instance : reservation.instances()) {
                    System.out.println("Instance Id is " + instance.instanceId());
                    System.out.println("Image id is " + instance.imageId());
                    System.out.println("Instance type is " + instance.instanceType());
                    System.out.println("Instance state name is " + instance.state().name());
                    System.out.println("monitoring information is " + instance.monitoring().state());
                }
            }
            nextToken = response.nextToken();
        } while (nextToken != null);
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : Reservation(software.amazon.awssdk.services.ec2.model.Reservation) Instance(software.amazon.awssdk.services.ec2.model.Instance) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) DescribeInstancesRequest(software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest) DescribeInstancesResponse(software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse)

Example 12 with DescribeInstancesRequest

use of com.amazonaws.services.ec2.model.DescribeInstancesRequest in project aws-doc-sdk-examples by awsdocs.

the class FindRunningInstances method findRunningEC2Instances.

// snippet-start:[ec2.java2.running_instances.main]
public static void findRunningEC2Instances(Ec2Client ec2) {
    try {
        String nextToken = null;
        do {
            Filter filter = Filter.builder().name("instance-state-name").values("running").build();
            DescribeInstancesRequest request = DescribeInstancesRequest.builder().filters(filter).build();
            DescribeInstancesResponse response = ec2.describeInstances(request);
            for (Reservation reservation : response.reservations()) {
                for (Instance instance : reservation.instances()) {
                    System.out.printf("Found Reservation with id %s, " + "AMI %s, " + "type %s, " + "state %s " + "and monitoring state %s", instance.instanceId(), instance.imageId(), instance.instanceType(), instance.state().name(), instance.monitoring().state());
                    System.out.println("");
                }
            }
            nextToken = response.nextToken();
        } while (nextToken != null);
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : Reservation(software.amazon.awssdk.services.ec2.model.Reservation) Filter(software.amazon.awssdk.services.ec2.model.Filter) Instance(software.amazon.awssdk.services.ec2.model.Instance) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) DescribeInstancesRequest(software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest) DescribeInstancesResponse(software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse)

Example 13 with DescribeInstancesRequest

use of com.amazonaws.services.ec2.model.DescribeInstancesRequest in project aws-doc-sdk-examples by awsdocs.

the class DescribeInstances method main.

public static void main(String[] args) {
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    boolean done = false;
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    while (!done) {
        DescribeInstancesResult response = ec2.describeInstances(request);
        for (Reservation reservation : response.getReservations()) {
            for (Instance instance : reservation.getInstances()) {
                System.out.printf("Found instance with id %s, " + "AMI %s, " + "type %s, " + "state %s " + "and monitoring state %s", instance.getInstanceId(), instance.getImageId(), instance.getInstanceType(), instance.getState().getName(), instance.getMonitoring().getState());
            }
        }
        request.setNextToken(response.getNextToken());
        if (response.getNextToken() == null) {
            done = true;
        }
    }
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 14 with DescribeInstancesRequest

use of com.amazonaws.services.ec2.model.DescribeInstancesRequest in project aws-doc-sdk-examples by awsdocs.

the class FindRunningInstances method main.

public static void main(String[] args) {
    // snippet-start:[ec2.java1.running_instances.main]
    AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    try {
        // Create the Filter to use to find running instances
        Filter filter = new Filter("instance-state-name");
        filter.withValues("running");
        // Create a DescribeInstancesRequest
        DescribeInstancesRequest request = new DescribeInstancesRequest();
        request.withFilters(filter);
        // Find the running instances
        DescribeInstancesResult response = ec2.describeInstances(request);
        for (Reservation reservation : response.getReservations()) {
            for (Instance instance : reservation.getInstances()) {
                // Print out the results
                System.out.printf("Found reservation with id %s, " + "AMI %s, " + "type %s, " + "state %s " + "and monitoring state %s", instance.getInstanceId(), instance.getImageId(), instance.getInstanceType(), instance.getState().getName(), instance.getMonitoring().getState());
            }
        }
        System.out.print("Done");
    } catch (SdkClientException e) {
        e.getStackTrace();
    }
// snippet-end:[ec2.java1.running_instances.main]
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) SdkClientException(com.amazonaws.SdkClientException) Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 15 with DescribeInstancesRequest

use of com.amazonaws.services.ec2.model.DescribeInstancesRequest in project SimianArmy by Netflix.

the class AWSClient method describeInstances.

/**
 * Describe a set of specific instances.
 *
 * @param instanceIds the instance ids
 * @return the instances
 */
public List<Instance> describeInstances(String... instanceIds) {
    if (instanceIds == null || instanceIds.length == 0) {
        LOGGER.info(String.format("Getting all EC2 instances in region %s.", region));
    } else {
        LOGGER.info(String.format("Getting EC2 instances for %d ids in region %s.", instanceIds.length, region));
    }
    List<Instance> instances = new LinkedList<Instance>();
    AmazonEC2 ec2Client = ec2Client();
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    if (instanceIds != null) {
        request.withInstanceIds(Arrays.asList(instanceIds));
    }
    DescribeInstancesResult result = ec2Client.describeInstances(request);
    for (Reservation reservation : result.getReservations()) {
        instances.addAll(reservation.getInstances());
    }
    LOGGER.info(String.format("Got %d EC2 instances in region %s.", instances.size(), region));
    return instances;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2)

Aggregations

DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)32 Instance (com.amazonaws.services.ec2.model.Instance)24 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)22 Reservation (com.amazonaws.services.ec2.model.Reservation)21 Filter (com.amazonaws.services.ec2.model.Filter)19 ArrayList (java.util.ArrayList)14 List (java.util.List)9 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)7 Function (com.google.common.base.Function)6 AWSUtils.getAWSNonTerminatedInstancesFilter (com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getAWSNonTerminatedInstancesFilter)6 HashMap (java.util.HashMap)6 Tag (com.amazonaws.services.ec2.model.Tag)5 Map (java.util.Map)5 Volume (com.amazonaws.services.ec2.model.Volume)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3 FluentIterable (com.google.common.collect.FluentIterable)3 AmazonClientException (com.amazonaws.AmazonClientException)2 AmazonWebServiceRequest (com.amazonaws.AmazonWebServiceRequest)2 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)2 AsyncHandler (com.amazonaws.handlers.AsyncHandler)2