Search in sources :

Example 11 with Ec2Exception

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);
    }
}
Also used : RebootInstancesRequest(software.amazon.awssdk.services.ec2.model.RebootInstancesRequest) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception)

Example 12 with Ec2Exception

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 "";
}
Also used : AssociateAddressRequest(software.amazon.awssdk.services.ec2.model.AssociateAddressRequest) AssociateAddressResponse(software.amazon.awssdk.services.ec2.model.AssociateAddressResponse) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) AllocateAddressResponse(software.amazon.awssdk.services.ec2.model.AllocateAddressResponse) AllocateAddressRequest(software.amazon.awssdk.services.ec2.model.AllocateAddressRequest)

Example 13 with Ec2Exception

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 "";
}
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)

Example 14 with Ec2Exception

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);
    }
}
Also used : DeleteSecurityGroupRequest(software.amazon.awssdk.services.ec2.model.DeleteSecurityGroupRequest) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception)

Example 15 with Ec2Exception

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);
    }
}
Also used : ReleaseAddressResponse(software.amazon.awssdk.services.ec2.model.ReleaseAddressResponse) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) ReleaseAddressRequest(software.amazon.awssdk.services.ec2.model.ReleaseAddressRequest)

Aggregations

Ec2Exception (software.amazon.awssdk.services.ec2.model.Ec2Exception)16 DescribeInstancesRequest (software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest)2 DescribeInstancesResponse (software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse)2 Instance (software.amazon.awssdk.services.ec2.model.Instance)2 Reservation (software.amazon.awssdk.services.ec2.model.Reservation)2 ListIterator (java.util.ListIterator)1 AccountAttribute (software.amazon.awssdk.services.ec2.model.AccountAttribute)1 AccountAttributeValue (software.amazon.awssdk.services.ec2.model.AccountAttributeValue)1 AllocateAddressRequest (software.amazon.awssdk.services.ec2.model.AllocateAddressRequest)1 AllocateAddressResponse (software.amazon.awssdk.services.ec2.model.AllocateAddressResponse)1 AssociateAddressRequest (software.amazon.awssdk.services.ec2.model.AssociateAddressRequest)1 AssociateAddressResponse (software.amazon.awssdk.services.ec2.model.AssociateAddressResponse)1 AuthorizeSecurityGroupIngressRequest (software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressRequest)1 AuthorizeSecurityGroupIngressResponse (software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressResponse)1 AvailabilityZone (software.amazon.awssdk.services.ec2.model.AvailabilityZone)1 CreateKeyPairRequest (software.amazon.awssdk.services.ec2.model.CreateKeyPairRequest)1 CreateSecurityGroupRequest (software.amazon.awssdk.services.ec2.model.CreateSecurityGroupRequest)1 CreateSecurityGroupResponse (software.amazon.awssdk.services.ec2.model.CreateSecurityGroupResponse)1 CreateTagsRequest (software.amazon.awssdk.services.ec2.model.CreateTagsRequest)1 DeleteKeyPairRequest (software.amazon.awssdk.services.ec2.model.DeleteKeyPairRequest)1