@@ -0,0 +1,2971 @@
+//snippet-sourcedescription:%5BTerminateInstance.java demonstrates how to terminate an Amazon EC2 instance.%5D%0A//snippet-keyword:%5BSDK for Java 2.0%5D%0A//snippet-keyword:%5BCode Sample%5D%0A//snippet-service:%5BAmazon EC2%5D%0A//snippet-sourcetype:%5Bfull-example%5D%0A//snippet-sourcedate:%5B2/12/2020%5D%0A//snippet-sourceauthor:%5Bscmacdon%5D%0A%0A/*%0A * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22).%0A * You may not use this file except in compliance with the License.%0A * A copy of the License is located at%0A *%0A *  http://aws.amazon.com/apache2.0%0A *%0A * or in the %22license%22 file accompanying this file. This file is distributed%0A * on an %22AS IS%22 BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either%0A * express or implied. See the License for the specific language governing%0A * permissions and limitations under the License.%0A */%0Apackage com.example.ec2;%0A%0A// snippet-start:%5Bec2.java2.terminate_instance.import%5D%0Aimport software.amazon.awssdk.regions.Region;%0Aimport software.amazon.awssdk.services.ec2.Ec2Client;%0Aimport software.amazon.awssdk.services.ec2.model.TerminateInstancesRequest;%0Aimport software.amazon.awssdk.services.ec2.model.TerminateInstancesResponse;%0Aimport software.amazon.awssdk.services.ec2.model.InstanceStateChange;%0Aimport software.amazon.awssdk.services.ec2.model.Ec2Exception;%0Aimport java.util.List;%0A// snippet-end:%5Bec2.java2.terminate_instance.import%5D%0A%0Apublic class TerminateInstance %7B%0A%0A    public static void main(String%5B%5D args) %7B%0A        final String USAGE =%0A                %22To run this example, supply an instance id %5Cn%22 +%0A                         %22Ex: TerminateInstance %3Cinstance-id%3E%5Cn%22;%0A%0A        if (args.length %3C 1) %7B%0A            System.out.println(USAGE);%0A            System.exit(1);%0A        %7D%0A%0A        String instanceID = args%5B0%5D;%0A%0A        // Create an Ec2Client object%0A        Region region = Region.US_WEST_2;%0A        Ec2Client ec2 = Ec2Client.builder()%0A                .region(region)%0A                .build();%0A%0A        terminateEC2(ec2, instanceID) ;%0A    %7D%0A%0A    // snippet-start:%5Bec2.java2.terminate_instance%5D%0A    public static void terminateEC2( Ec2Client ec2, String instanceID) %7B%0A%0A            try%7B%0A                TerminateInstancesRequest ti = TerminateInstancesRequest.builder()%0A                    .instanceIds(instanceID)%0A                    .build();%0A%0A                TerminateInstancesResponse response = ec2.terminateInstances(ti);%0A%0A                List%3CInstanceStateChange%3E list = response.terminatingInstances();%0A%0A                for (int i = 0; i %3C list.size(); i++) %7B%0A                    InstanceStateChange sc = (list.get(i));%0A                    System.out.println(%22The ID of the terminated instance is %22+sc.instanceId());%0A                %7D%0A            %7D catch (Ec2Exception e) %7B%0A                System.err.println(e.awsErrorDetails().errorMessage());%0A                System.exit(1);%0A            %7D%0A         %7D%0A    // snippet-end:%5Bec2.java2.terminate_instance%5D%0A    %7D%0A

Create TerminateInstance.java