Search in sources :

Example 1 with Ec2Exception

use of software.amazon.awssdk.services.ec2.model.Ec2Exception in project aws-doc-sdk-examples by awsdocs.

the class TerminateInstance method terminateEC2.

// snippet-start:[ec2.java2.terminate_instance]
public static void terminateEC2(Ec2Client ec2, String instanceID) {
    try {
        TerminateInstancesRequest ti = TerminateInstancesRequest.builder().instanceIds(instanceID).build();
        TerminateInstancesResponse response = ec2.terminateInstances(ti);
        List<InstanceStateChange> list = response.terminatingInstances();
        for (int i = 0; i < list.size(); i++) {
            InstanceStateChange sc = (list.get(i));
            System.out.println("The ID of the terminated instance is " + sc.instanceId());
        }
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) TerminateInstancesResponse(software.amazon.awssdk.services.ec2.model.TerminateInstancesResponse) TerminateInstancesRequest(software.amazon.awssdk.services.ec2.model.TerminateInstancesRequest) InstanceStateChange(software.amazon.awssdk.services.ec2.model.InstanceStateChange)

Example 2 with Ec2Exception

use of software.amazon.awssdk.services.ec2.model.Ec2Exception in project aws-doc-sdk-examples by awsdocs.

the class CreateKeyPair method createEC2KeyPair.

// snippet-start:[ec2.java2.create_key_pair.main]
public static void createEC2KeyPair(Ec2Client ec2, String keyName) {
    try {
        CreateKeyPairRequest request = CreateKeyPairRequest.builder().keyName(keyName).build();
        ec2.createKeyPair(request);
        System.out.printf("Successfully created key pair named %s", keyName);
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) CreateKeyPairRequest(software.amazon.awssdk.services.ec2.model.CreateKeyPairRequest)

Example 3 with Ec2Exception

use of software.amazon.awssdk.services.ec2.model.Ec2Exception in project aws-doc-sdk-examples by awsdocs.

the class DescribeAccount method describeEC2Account.

// snippet-start:[ec2.java2.describe_account.main]
public static void describeEC2Account(Ec2Client ec2) {
    try {
        DescribeAccountAttributesResponse accountResults = ec2.describeAccountAttributes();
        List<AccountAttribute> accountList = accountResults.accountAttributes();
        for (ListIterator iter = accountList.listIterator(); iter.hasNext(); ) {
            AccountAttribute attribute = (AccountAttribute) iter.next();
            System.out.print("\n The name of the attribute is " + attribute.attributeName());
            List<AccountAttributeValue> values = attribute.attributeValues();
            for (ListIterator iterVals = values.listIterator(); iterVals.hasNext(); ) {
                AccountAttributeValue myValue = (AccountAttributeValue) iterVals.next();
                System.out.print("\n The value of the attribute is " + myValue.attributeValue());
            }
        }
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : AccountAttribute(software.amazon.awssdk.services.ec2.model.AccountAttribute) DescribeAccountAttributesResponse(software.amazon.awssdk.services.ec2.model.DescribeAccountAttributesResponse) AccountAttributeValue(software.amazon.awssdk.services.ec2.model.AccountAttributeValue) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) ListIterator(java.util.ListIterator)

Example 4 with Ec2Exception

use of software.amazon.awssdk.services.ec2.model.Ec2Exception in project aws-doc-sdk-examples by awsdocs.

the class DescribeReservedInstances method describeReservedEC2Instances.

// snippet-start:[ec2.java2.describe_reserved_instances.main]
public static void describeReservedEC2Instances(Ec2Client ec2, String instanceID) {
    try {
        DescribeReservedInstancesRequest request = DescribeReservedInstancesRequest.builder().reservedInstancesIds(instanceID).build();
        DescribeReservedInstancesResponse response = ec2.describeReservedInstances(request);
        for (ReservedInstances instance : response.reservedInstances()) {
            System.out.printf("Found a Reserved Instance with id %s, " + "in AZ %s, " + "type %s, " + "state %s " + "and monitoring state %s", instance.reservedInstancesId(), instance.availabilityZone(), instance.instanceType(), instance.state().name());
        }
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) DescribeReservedInstancesRequest(software.amazon.awssdk.services.ec2.model.DescribeReservedInstancesRequest) ReservedInstances(software.amazon.awssdk.services.ec2.model.ReservedInstances) DescribeReservedInstancesResponse(software.amazon.awssdk.services.ec2.model.DescribeReservedInstancesResponse)

Example 5 with Ec2Exception

use of software.amazon.awssdk.services.ec2.model.Ec2Exception in project aws-doc-sdk-examples by awsdocs.

the class DescribeVPCs method describeEC2Vpcs.

// snippet-start:[ec2.java2.describe_vpc.main]
public static void describeEC2Vpcs(Ec2Client ec2, String vpcId) {
    try {
        DescribeVpcsRequest request = DescribeVpcsRequest.builder().vpcIds(vpcId).build();
        DescribeVpcsResponse response = ec2.describeVpcs(request);
        for (Vpc vpc : response.vpcs()) {
            System.out.printf("Found VPC with id %s, " + "vpc state %s " + "and tennancy %s", vpc.vpcId(), vpc.stateAsString(), vpc.instanceTenancyAsString());
        }
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : DescribeVpcsRequest(software.amazon.awssdk.services.ec2.model.DescribeVpcsRequest) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) Vpc(software.amazon.awssdk.services.ec2.model.Vpc) DescribeVpcsResponse(software.amazon.awssdk.services.ec2.model.DescribeVpcsResponse)

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