use of com.amazonaws.services.ec2.model.CreateSecurityGroupResult in project aws-doc-sdk-examples by awsdocs.
the class CreateSecurityGroup method main.
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 group_name = args[0];
String group_desc = args[1];
String vpc_id = args[2];
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
CreateSecurityGroupRequest create_request = new CreateSecurityGroupRequest().withGroupName(group_name).withDescription(group_desc).withVpcId(vpc_id);
CreateSecurityGroupResult create_response = ec2.createSecurityGroup(create_request);
System.out.printf("Successfully created security group named %s", group_name);
IpRange ip_range = new IpRange().withCidrIp("0.0.0.0/0");
IpPermission ip_perm = new IpPermission().withIpProtocol("tcp").withToPort(80).withFromPort(80).withIpv4Ranges(ip_range);
IpPermission ip_perm2 = new IpPermission().withIpProtocol("tcp").withToPort(22).withFromPort(22).withIpv4Ranges(ip_range);
AuthorizeSecurityGroupIngressRequest auth_request = new AuthorizeSecurityGroupIngressRequest().withGroupName(group_name).withIpPermissions(ip_perm, ip_perm2);
AuthorizeSecurityGroupIngressResult auth_response = ec2.authorizeSecurityGroupIngress(auth_request);
System.out.printf("Successfully added ingress policy to security group %s", group_name);
}
use of com.amazonaws.services.ec2.model.CreateSecurityGroupResult in project SimianArmy by Netflix.
the class AWSClient method createSecurityGroup.
/**
* {@inheritDoc}
*/
public String createSecurityGroup(String instanceId, String name, String description) {
String vpcId = getVpcId(instanceId);
AmazonEC2 ec2Client = ec2Client();
CreateSecurityGroupRequest request = new CreateSecurityGroupRequest();
request.setGroupName(name);
request.setDescription(description);
request.setVpcId(vpcId);
LOGGER.info(String.format("Creating EC2 security group %s.", name));
CreateSecurityGroupResult result = ec2Client.createSecurityGroup(request);
return result.getGroupId();
}
use of com.amazonaws.services.ec2.model.CreateSecurityGroupResult in project Synapse-Stack-Builder by Sage-Bionetworks.
the class EC2SecuritySetup method createSecurityGroup.
/**
* Create a security group. If the group already exists
* @param ec2Client
* @param request
*/
void createSecurityGroup(CreateSecurityGroupRequest request) {
try {
// First create the EC2 group
log.info("Creating Security Group: " + request.getGroupName() + "...");
CreateSecurityGroupResult result = ec2Client.createSecurityGroup(request);
} catch (AmazonServiceException e) {
if (ERROR_CODE_INVALID_GROUP_DUPLICATE.equals(e.getErrorCode())) {
// This group already exists
log.info("Security Group: " + request.getGroupName() + " already exits");
} else {
throw e;
}
}
}
use of com.amazonaws.services.ec2.model.CreateSecurityGroupResult in project photon-model by vmware.
the class AWSSecurityGroupClient method createSecurityGroupAsync.
public DeferredResult<String> createSecurityGroupAsync(String name, String description, String vpcId) {
CreateSecurityGroupRequest req = new CreateSecurityGroupRequest().withDescription(description).withGroupName(name);
// set vpc for the security group if provided
if (vpcId != null) {
req = req.withVpcId(vpcId);
}
String message = "Create AWS Security Group with name [" + name + "] on VPC [" + vpcId + "].";
AWSDeferredResultAsyncHandler<CreateSecurityGroupRequest, CreateSecurityGroupResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message);
this.client.createSecurityGroupAsync(req, handler);
return handler.toDeferredResult().thenApply(CreateSecurityGroupResult::getGroupId);
}
use of com.amazonaws.services.ec2.model.CreateSecurityGroupResult in project photon-model by vmware.
the class AWSSecurityGroupClient method createSecurityGroup.
public String createSecurityGroup(String name, String description, String vpcId) {
CreateSecurityGroupRequest req = new CreateSecurityGroupRequest().withDescription(description).withGroupName(name);
// set vpc for the security group if provided
if (vpcId != null) {
req = req.withVpcId(vpcId);
}
CreateSecurityGroupResult result = this.client.createSecurityGroup(req);
return result.getGroupId();
}
Aggregations