//snippet-sourcedescription:[DeleteSecurityGroup.java demonstrates how to delete an EC2 security group.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[ec2]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[11/02/2020]
//snippet-sourceauthor:[scmacdon]
/*
 * Copyright 2010-2020 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.delete_security_group.complete]
// snippet-start:[ec2.java2.delete_security_group.import]
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.DeleteSecurityGroupRequest;
import software.amazon.awssdk.services.ec2.model.DeleteSecurityGroupResponse;
import software.amazon.awssdk.services.ec2.model.Ec2Exception;

// snippet-end:[ec2.java2.delete_security_group.import]
/**
 * Deletes an EC2 security group
 */
public class DeleteSecurityGroup {

    public static void main(String[] args) {
        final String USAGE =
                "To run this example, supply a security group id\n" +
                        "Ex: DeleteSecurityGroup <security-group-id>\n";

        if (args.length != 1) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String groupId = args[0];
        // snippet-start:[ec2.java2.delete_security_group.main]

        Ec2Client ec2 = Ec2Client.create();

        try {

            DeleteSecurityGroupRequest request = DeleteSecurityGroupRequest.builder()
                .groupId(groupId)
                .build();

            DeleteSecurityGroupResponse response = ec2.deleteSecurityGroup(request);

            System.out.printf(
                "Successfully deleted security group with id %s", groupId);

        } catch (Ec2Exception e) {
            e.getStackTrace();
        }
        // snippet-end:[ec2.java2.delete_security_group.main]
    }
}
// snippet-end:[ec2.java2.delete_security_group.complete]

@@ -73,24 +73,31 @@
 o delete an 
+Amazon 
 EC2 security
@@ -196,18 +196,25 @@
 ervice:%5B
-ec
+Amazon EC
 2%5D%0D%0A//sn
@@ -272,13 +272,12 @@
 te:%5B
+2/
 11
-/02
 /202
@@ -336,18 +336,8 @@
 ght 
-2010-2020 
 Amaz
@@ -934,67 +934,8 @@
 2;%0D%0A
-// snippet-start:%5Bec2.java2.delete_security_group.complete%5D
 %0D%0A//
@@ -991,16 +991,63 @@
 mport%5D%0D%0A
+import software.amazon.awssdk.regions.Region;%0D%0A
 import s
@@ -1314,18 +1314,16 @@
 ption;%0D%0A
-%0D%0A
 // snipp
@@ -1826,20 +1826,256 @@
 gs%5B0%5D;%0D%0A
+%0D%0A
     
+    //Create an Ec2Client object%0D%0A        Region region = Region.US_WEST_2;%0D%0A        Ec2Client ec2 = Ec2Client.builder()%0D%0A                .region(region)%0D%0A                .build();%0D%0A%0D%0A        deleteEC2SecGroup(ec2,groupId);%0D%0A    %7D%0D%0A%0D%0A
     // s
@@ -2131,53 +2131,80 @@
 n%5D%0D%0A
-%0D%0A
     
-    Ec2Client ec2 = Ec2Client.create();
+public static void deleteEC2SecGroup(Ec2Client ec2,String groupId) %7B
 %0D%0A%0D%0A
@@ -2214,26 +2214,24 @@
      try %7B%0D%0A
-%0D%0A
             
@@ -2620,24 +2620,90 @@
     
-e.getStackTrace(
+System.err.println(e.awsErrorDetails().errorMessage());%0D%0A            System.exit(1
 );%0D%0A
@@ -2790,63 +2790,4 @@
 %0A%7D%0D%0A
-// snippet-end:%5Bec2.java2.delete_security_group.complete%5D%0D%0A

Update DeleteSecurityGroup.java