use of software.amazon.awssdk.services.ec2.model.Ec2Exception in project aws-doc-sdk-examples by awsdocs.
the class RebootInstance method rebootEC2Instance.
// snippet-start:[ec2.java2.reboot_instance.main]
public static void rebootEC2Instance(Ec2Client ec2, String instanceId) {
try {
RebootInstancesRequest request = RebootInstancesRequest.builder().instanceIds(instanceId).build();
ec2.rebootInstances(request);
System.out.printf("Successfully rebooted instance %s", instanceId);
} catch (Ec2Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.ec2.model.Ec2Exception 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.model.Ec2Exception 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 "";
}
use of software.amazon.awssdk.services.ec2.model.Ec2Exception in project aws-doc-sdk-examples by awsdocs.
the class DeleteSecurityGroup method deleteEC2SecGroup.
// snippet-start:[ec2.java2.delete_security_group.main]
public static void deleteEC2SecGroup(Ec2Client ec2, String groupId) {
try {
DeleteSecurityGroupRequest request = DeleteSecurityGroupRequest.builder().groupId(groupId).build();
ec2.deleteSecurityGroup(request);
System.out.printf("Successfully deleted Security Group with id %s", groupId);
} catch (Ec2Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.ec2.model.Ec2Exception in project aws-doc-sdk-examples by awsdocs.
the class ReleaseAddress method releaseEC2Address.
// snippet-start:[ec2.java2.release_instance.main]
public static void releaseEC2Address(Ec2Client ec2, String allocId) {
try {
ReleaseAddressRequest request = ReleaseAddressRequest.builder().allocationId(allocId).build();
ReleaseAddressResponse response = ec2.releaseAddress(request);
System.out.printf("Successfully released elastic IP address %s", allocId);
} catch (Ec2Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
Aggregations