Search in sources :

Example 1 with AuthorizeSecurityGroupIngressResponse

use of software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressResponse in project aws-doc-sdk-examples by awsdocs.

the class CreateSecurityGroup method createEC2SecurityGroup.

// snippet-start:[ec2.java2.create_security_group.main]
public static String createEC2SecurityGroup(Ec2Client ec2, String groupName, String groupDesc, String vpcId) {
    try {
        // snippet-start:[ec2.java2.create_security_group.create]
        CreateSecurityGroupRequest createRequest = CreateSecurityGroupRequest.builder().groupName(groupName).description(groupDesc).vpcId(vpcId).build();
        CreateSecurityGroupResponse resp = ec2.createSecurityGroup(createRequest);
        // snippet-end:[ec2.java2.create_security_group.create]
        // snippet-start:[ec2.java2.create_security_group.config]
        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);
        System.out.printf("Successfully added ingress policy to Security Group %s", groupName);
        return resp.groupId();
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
Also used : IpRange(software.amazon.awssdk.services.ec2.model.IpRange) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) AuthorizeSecurityGroupIngressRequest(software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressRequest) IpPermission(software.amazon.awssdk.services.ec2.model.IpPermission) AuthorizeSecurityGroupIngressResponse(software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressResponse) CreateSecurityGroupResponse(software.amazon.awssdk.services.ec2.model.CreateSecurityGroupResponse) CreateSecurityGroupRequest(software.amazon.awssdk.services.ec2.model.CreateSecurityGroupRequest)

Aggregations

AuthorizeSecurityGroupIngressRequest (software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressRequest)1 AuthorizeSecurityGroupIngressResponse (software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressResponse)1 CreateSecurityGroupRequest (software.amazon.awssdk.services.ec2.model.CreateSecurityGroupRequest)1 CreateSecurityGroupResponse (software.amazon.awssdk.services.ec2.model.CreateSecurityGroupResponse)1 Ec2Exception (software.amazon.awssdk.services.ec2.model.Ec2Exception)1 IpPermission (software.amazon.awssdk.services.ec2.model.IpPermission)1 IpRange (software.amazon.awssdk.services.ec2.model.IpRange)1