//snippet-sourcedescription:[DescribeInstances.java demonstrates how to get information about all the EC2 instances associated with an AWS account.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[ec2]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[]
//snippet-sourceauthor:[soo-aws]
/*
 * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.example.ec2;
// snippet-start:[ec2.java2.describe_instances.complete]
// snippet-start:[ec2.java2.describe_instances.import]

import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest;
import software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse;
import software.amazon.awssdk.services.ec2.model.Instance;
import software.amazon.awssdk.services.ec2.model.Reservation;

// snippet-end:[ec2.java2.describe_instances.import]

/**
 * Describes all EC2 instances associated with an AWS account
 */
public class DescribeInstances {
    public static void main(String[] args) {
        Ec2Client ec2 = Ec2Client.create();
        boolean done = false;

        // snippet-start:[ec2.java2.describe_instances.main]
        String nextToken = null;
        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.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);
        // snippet-end:[ec2.java2.describe_instances.main]        
    }
}

// snippet-end:[ec2.java2.describe_instances.complete]

@@ -298,16 +298,26 @@
 cedate:%5B
+11/02/2020
 %5D%0D%0A//sni
@@ -340,14 +340,15 @@
 r:%5Bs
-oo-aws
+cmacdon
 %5D%0D%0A/
@@ -374,10 +374,10 @@
 0-20
-19
+20
  Ama
@@ -1029,16 +1029,18 @@
 plete%5D%0D%0A
+%0D%0A
 // snipp
@@ -1087,18 +1087,16 @@
 mport%5D%0D%0A
-%0D%0A
 import s
@@ -1418,16 +1418,78 @@
 ation;%0D%0A
+import software.amazon.awssdk.services.ec2.model.Ec2Exception;
 %0D%0A// sni
@@ -1641,24 +1641,26 @@
 nstances %7B%0D%0A
+%0D%0A
     public s
@@ -1867,24 +1867,44 @@
  null;%0D%0A
+%0D%0A        try %7B%0D%0A%0D%0A 
         
 do %7B%0D%0A  
@@ -1899,14 +1899,21 @@
     
+   
 do %7B%0D%0A
+    
     
@@ -2039,32 +2039,36 @@
 );%0D%0A            
+   
+ 
 DescribeInstance
@@ -2131,24 +2131,28 @@
             
+    
 for (Reserva
@@ -2190,32 +2190,36 @@
 ervations()) %7B%0D%0A
+    
                 
@@ -2952,16 +2952,20 @@
         
+    
 nextToke
@@ -2991,20 +2991,20 @@
 ken();%0D%0A
-%0D%0A%0D%0A
+    
         
@@ -3032,16 +3032,97 @@
  null);%0D
+%0A%0D%0A        %7D catch (Ec2Exception e) %7B%0D%0A            e.getStackTrace();%0D%0A        %7D%0D
 %0A       
@@ -3192,18 +3192,16 @@
   %7D%0D%0A%7D%0D%0A
-%0D%0A
 // snipp

Update DescribeInstances.java