@@ -0,0 +1,3119 @@
+/**%0A * Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.%0A *%0A * This file is licensed under the Apache License, Version 2.0 (the %22License%22).%0A * You may not use this file except in compliance with the License. A copy of%0A * the License is located at%0A *%0A * http://aws.amazon.com/apache2.0/%0A *%0A * This file is distributed on an %22AS IS%22 BASIS, WITHOUT WARRANTIES OR%0A * CONDITIONS OF ANY KIND, either express or implied. See the License for the%0A * specific language governing permissions and limitations under the License.%0A *%0A */%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// snippet-start:%5Bec2.java2.running_instances.complete%5D%0Apackage com.example.ec2;%0A%0A// snippet-start:%5Bec2.java2.running_instances.import%5D%0Aimport software.amazon.awssdk.services.ec2.Ec2Client;%0Aimport software.amazon.awssdk.services.ec2.model.*;%0A%0A// snippet-end:%5Bec2.java2.running_instances.import%5D%0A%0A/**%0A * Locates all running EC2 instances using a Filter%0A */%0Apublic class FindRunningInstances %7B%0A    public static void main(String%5B%5D args) %7B%0A        Ec2Client ec2 = Ec2Client.create();%0A        boolean done = false;%0A%0A        // snippet-start:%5Bec2.java2.running_instances.main%5D%0A        String nextToken = null;%0A        do %7B%0A%0A            // Create a Filter to find all running instances%0A            Filter filter =  Filter.builder()%0A                    .name(%22instance-state-name%22)%0A                    .values(%22running%22)%0A                    .build();%0A%0A            //Create a DescribeInstancesRequest%0A            DescribeInstancesRequest request =  DescribeInstancesRequest.builder()%0A                    .filters(filter)%0A                    .build();%0A%0A            // Find the running instances%0A            DescribeInstancesResponse response = ec2.describeInstances(request);%0A%0A            for (Reservation reservation : response.reservations()) %7B%0A                for (Instance instance : reservation.instances()) %7B%0A                    System.out.printf(%0A                            %22Found reservation with id %25s, %22 +%0A                                    %22AMI %25s, %22 +%0A                                    %22type %25s, %22 +%0A                                    %22state %25s %22 +%0A                                    %22and monitoring state %25s%22,%0A                            instance.instanceId(),%0A                            instance.imageId(),%0A                            instance.instanceType(),%0A                            instance.state().name(),%0A                            instance.monitoring().state());%0A                    System.out.println(%22%22);%0A                %7D%0A            %7D%0A            nextToken = response.nextToken();%0A%0A%0A        %7D while (nextToken != null);%0A        // snippet-end:%5Bec2.java2.running_instances.main%5D%0A    %7D%0A%7D%0A// snippet-end:%5Bec2.java2.running_instances.complete%5D%0A

Create FindRunningInstances.java