use of software.amazon.awssdk.services.ec2.Ec2Client in project aws-doc-sdk-examples by awsdocs.
the class StartStopInstance method startInstance.
// snippet-start:[ec2.java2.start_stop_instance.start]
public static void startInstance(Ec2Client ec2, String instanceId) {
StartInstancesRequest request = StartInstancesRequest.builder().instanceIds(instanceId).build();
ec2.startInstances(request);
System.out.printf("Successfully started instance %s", instanceId);
}
use of software.amazon.awssdk.services.ec2.Ec2Client in project aws-doc-sdk-examples by awsdocs.
the class StartStopInstance method main.
public static void main(String[] args) {
final String USAGE = "\n" + "Usage:\n" + " <instanceId>\n\n" + "Where:\n" + " instanceId - an instance id value that you can obtain from the AWS Console. \n\n";
if (args.length != 1) {
System.out.println(USAGE);
System.exit(1);
}
String instanceId = args[0];
boolean start;
Region region = Region.US_WEST_2;
Ec2Client ec2 = Ec2Client.builder().region(region).build();
if (args[1].equals("start")) {
start = true;
} else {
start = false;
}
if (start) {
startInstance(ec2, instanceId);
} else {
stopInstance(ec2, instanceId);
}
ec2.close();
}
use of software.amazon.awssdk.services.ec2.Ec2Client in project aws-doc-sdk-examples by awsdocs.
the class AllocateAddress method getAllocateAddress.
// snippet-start:[ec2.java2.allocate_address.main]
public static String getAllocateAddress(Ec2Client ec2, String instanceId) {
try {
AllocateAddressRequest allocateRequest = AllocateAddressRequest.builder().domain(DomainType.VPC).build();
AllocateAddressResponse allocateResponse = ec2.allocateAddress(allocateRequest);
String allocationId = allocateResponse.allocationId();
AssociateAddressRequest associateRequest = AssociateAddressRequest.builder().instanceId(instanceId).allocationId(allocationId).build();
AssociateAddressResponse associateResponse = ec2.associateAddress(associateRequest);
return associateResponse.associationId();
} catch (Ec2Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
return "";
}
use of software.amazon.awssdk.services.ec2.Ec2Client in project aws-doc-sdk-examples by awsdocs.
the class CreateInstance method main.
public static void main(String[] args) {
final String USAGE = "\n" + "Usage:\n" + " <name> <amiId>\n\n" + "Where:\n" + " name - an instance name value that you can obtain from the AWS Console (for example, ami-xxxxxx5c8b987b1a0). \n\n" + " amiId - an Amazon Machine Image (AMI) value that you can obtain from the AWS Console (for example, i-xxxxxx2734106d0ab). \n\n";
if (args.length != 2) {
System.out.println(USAGE);
System.exit(1);
}
String name = args[0];
String amiId = args[1];
Region region = Region.US_WEST_2;
Ec2Client ec2 = Ec2Client.builder().region(region).build();
String instanceId = createEC2Instance(ec2, name, amiId);
System.out.println("The Amazon EC2 Instance ID is " + instanceId);
ec2.close();
}
use of software.amazon.awssdk.services.ec2.Ec2Client 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