use of software.amazon.awssdk.services.ec2.model.CreateSecurityGroupResponse 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 "";
}
Aggregations