/**
 * Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * This file is 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/
 *
 * 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.
 *
 */

// snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
// snippet-sourcedescription:[FindRunningInstances.java demonstrates how to use a Filter to find running instances]
// snippet-service:[ec2]
// snippet-keyword:[Java]
// snippet-keyword:[Code Sample]
// snippet-sourcetype:[full-example]
// snippet-sourcedate:[2020-01-10]
// snippet-sourceauthor:[AWS-scmacdon]

// snippet-start:[ec2.java2.running_instances.complete]
package com.example.ec2;

// snippet-start:[ec2.java2.running_instances.import]
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.Filter;
import software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest;
import software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse;
import software.amazon.awssdk.services.ec2.model.Reservation;
import software.amazon.awssdk.services.ec2.model.Instance;

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

/**
 * Locates all running EC2 instances using a Filter
 */
public class FindRunningInstances {
    public static void main(String[] args) {
        Ec2Client ec2 = Ec2Client.create();

        // snippet-start:[ec2.java2.running_instances.main]
        String nextToken = null;
        do {

            // Create a Filter to find all running instances
            Filter filter = Filter.builder()
                    .name("instance-state-name")
                    .values("running")
                    .build();

            //Create a DescribeInstancesRequest
            DescribeInstancesRequest request = DescribeInstancesRequest.builder()
                    .filters(filter)
                    .build();

            // Find the running instances
            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.running_instances.main]
    }
}
// snippet-end:[ec2.java2.running_instances.complete]

@@ -1,12 +1,414 @@
+// snippet-comment:%5BThese are tags for the AWS doc team's sample catalog. Do not remove.%5D%0A// snippet-sourcedescription:%5BFindRunningInstances.java demonstrates how to use a Filter to find running instances%5D%0A// snippet-service:%5Bec2%5D%0A// snippet-keyword:%5BJava%5D%0A// snippet-keyword:%5BCode Sample%5D%0A// snippet-sourcetype:%5Bfull-example%5D%0A// snippet-sourcedate:%5B2020-01-10%5D%0A// snippet-sourceauthor:%5BAWS-scmacdon%5D%0A%0A
 /**%0A * Copyr
@@ -950,410 +950,8 @@
 */%0A%0A
-// snippet-comment:%5BThese are tags for the AWS doc team's sample catalog. Do not remove.%5D%0A// snippet-sourcedescription:%5BFindRunningInstances.java demonstrates how to use a Filter to find running instances%5D%0A// snippet-service:%5Bec2%5D%0A// snippet-keyword:%5BJava%5D%0A// snippet-keyword:%5BCode Sample%5D%0A// snippet-sourcetype:%5Bfull-example%5D%0A// snippet-sourcedate:%5B2020-01-10%5D%0A// snippet-sourceauthor:%5BAWS-scmacdon%5D%0A%0A
 // s
@@ -1465,16 +1465,78 @@
 stance;%0A
+import software.amazon.awssdk.services.ec2.model.Ec2Exception;
 %0A// snip
@@ -1762,25 +1762,24 @@
 t.create();%0A
-%0A
         // s
@@ -1826,24 +1826,42 @@
 s.main%5D%0A
- 
+%0A       try %7B%0A%0A
        
+    
 String n
@@ -1877,24 +1877,26 @@
 = null;%0A
+%0A 
         
 do %7B%0A%0A  
@@ -1891,14 +1891,20 @@
     
+  
 do %7B%0A%0A
+    
     
@@ -1972,16 +1972,20 @@
         
+ 
+   
 Filter f
@@ -2120,32 +2120,36 @@
      .build();%0A%0A
+    
             //Cr
@@ -2184,32 +2184,36 @@
 est%0A            
+    
 DescribeInstance
@@ -2346,16 +2346,20 @@
         
+   
+ 
 // Find 
@@ -2372,32 +2372,36 @@
 nning instances%0A
+    
             Desc
@@ -2474,24 +2474,28 @@
             
+  
+  
 for (Reserva
@@ -2536,24 +2536,28 @@
 ations()) %7B%0A
+    
             
@@ -3314,17 +3314,20 @@
 ken();%0A%0A
-%0A
+    
         
@@ -3355,16 +3355,94 @@
  null);%0A
+%0A        %7D catch (Ec2Exception e) %7B%0A            e.getStackTrace();%0A        %7D%0A%0A
         

Update FindRunningInstances.java