//snippet-sourcedescription:[CreateSecurityGroup.java demonstrates how to create an Amazon EC2 security group.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[Amazon EC2]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[2/11/2020]
//snippet-sourceauthor:[scmacdon]

/*
 * Copyright 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.create_security_group.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.CreateSecurityGroupRequest;
import software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressRequest;
import software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressResponse;
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
import software.amazon.awssdk.services.ec2.model.IpPermission;
import software.amazon.awssdk.services.ec2.model.IpRange;
// snippet-end:[ec2.java2.create_security_group.import]

/**
 * Creates an Amazon EC2 security group
 */
public class CreateSecurityGroup {

    public static void main(String[] args) {
        final String USAGE =
                "To run this example, supply a group name, group description, and VPC ID.\n" +
                        "Ex: CreateSecurityGroup <group-name> <group-description> <vpc-id>\n";

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

        String groupName = args[0];
        String groupDesc = args[1];
        String vpcId = args[2];

        // Create an Ec2Client object
        Region region = Region.US_WEST_2;
        Ec2Client ec2 = Ec2Client.builder()
                .region(region)
                .build();

        createEC2SecurityGroup(ec2, groupName, groupDesc, vpcId);
    }

    // snippet-start:[ec2.java2.create_security_group.main]
    public static void createEC2SecurityGroup( Ec2Client ec2,String groupName, String groupDesc, String vpcId) {
        try {

            CreateSecurityGroupRequest createRequest = CreateSecurityGroupRequest.builder()
                .groupName(groupName)
                .description(groupDesc)
                .vpcId(vpcId)
                .build();

            ec2.createSecurityGroup(createRequest);

            System.out.printf(
                "Successfully created security group named %s",
                groupName);

            IpRange ipRange = IpRange.builder()
                .cidrIp("0.0.0.0/0").build();

            IpPermission ipPerm = IpPermission.builder()
                .ipProtocol("tcp")
                .toPort(80)
                .fromPort(80)
                .ipRanges(ipRange)
                .build();

            IpPermission ipPerm2 = IpPermission.builder()
                .ipProtocol("tcp")
                .toPort(22)
                .fromPort(22)
                .ipRanges(ipRange)
                .build();

            AuthorizeSecurityGroupIngressRequest authRequest =
                AuthorizeSecurityGroupIngressRequest.builder()
                        .groupName(groupName)
                        .ipPermissions(ipPerm, ipPerm2)
                        .build();

            AuthorizeSecurityGroupIngressResponse authResponse =
            ec2.authorizeSecurityGroupIngress(authRequest);

            // snippet-end:[ec2.java2.create_security_group.main]
            System.out.printf(
                "Successfully added ingress policy to security group %s",
                groupName);

        } catch (Ec2Exception e) {
            e.getStackTrace();
        }
    }
}

@@ -1474,24 +1474,103 @@
 ermission;%0D%0A
+import software.amazon.awssdk.services.ec2.model.CreateSecurityGroupResponse;%0D%0A
 import softw
@@ -1690,31 +1690,24 @@
  Creates an 
-Amazon 
 EC2 security
@@ -1712,16 +1712,17 @@
 ty group
+.
 %0D%0A */%0D%0Ap
@@ -1914,21 +1914,19 @@
 tion
-,
  and 
-VPC ID.
+vpc id
 %5Cn%22 
@@ -2260,17 +2260,16 @@
       //
- 
 Create a
@@ -2445,16 +2445,28 @@
 %0A       
+ String id =
  createE
@@ -2513,24 +2513,146 @@
 c, vpcId);%0D%0A
+        System.out.printf(%0D%0A                %22Successfully created security group with this ID %25s%22,%0D%0A                id);%0D%0A
     %7D%0D%0A%0D%0A   
@@ -2727,20 +2727,22 @@
  static 
-void
+String
  createE
@@ -3082,24 +3082,58 @@
 %0A           
+ CreateSecurityGroupResponse resp=
  ec2.createS
@@ -3168,136 +3168,8 @@
 %0D%0A%0D%0A
-            System.out.printf(%0D%0A                %22Successfully created security group named %25s%22,%0D%0A                groupName);%0D%0A%0D%0A
     
@@ -4300,24 +4300,62 @@
 upName);%0D%0A%0D%0A
+            return resp.groupId();%0D%0A%0D%0A
         %7D ca
@@ -4394,37 +4394,123 @@
     
-e.getStackTrace();%0D%0A        %7D
+System.err.println(e.awsErrorDetails().errorMessage());%0D%0A            System.exit(1);%0D%0A        %7D%0D%0A        return %22%22;
 %0D%0A  
@@ -4517,8 +4517,10 @@
   %7D%0D%0A%7D%0D%0A
+%0D%0A

Update CreateSecurityGroup.java