Search in sources :

Example 61 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project aws-doc-sdk-examples by awsdocs.

the class AllocateAddress method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply an instance id\n" + "Ex: AllocateAddress <instance_id>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String instance_id = args[0];
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    AllocateAddressRequest allocate_request = new AllocateAddressRequest().withDomain(DomainType.Vpc);
    AllocateAddressResult allocate_response = ec2.allocateAddress(allocate_request);
    String allocation_id = allocate_response.getAllocationId();
    AssociateAddressRequest associate_request = new AssociateAddressRequest().withInstanceId(instance_id).withAllocationId(allocation_id);
    AssociateAddressResult associate_response = ec2.associateAddress(associate_request);
    System.out.printf("Successfully associated Elastic IP address %s " + "with instance %s", associate_response.getAssociationId(), instance_id);
}
Also used : AssociateAddressResult(com.amazonaws.services.ec2.model.AssociateAddressResult) AssociateAddressRequest(com.amazonaws.services.ec2.model.AssociateAddressRequest) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AllocateAddressRequest(com.amazonaws.services.ec2.model.AllocateAddressRequest) AllocateAddressResult(com.amazonaws.services.ec2.model.AllocateAddressResult)

Example 62 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project aws-doc-sdk-examples by awsdocs.

the class ReleaseAddress method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply an allocation ID.\n" + "Ex: ReleaseAddress <allocation_id>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String alloc_id = args[0];
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    ReleaseAddressRequest request = new ReleaseAddressRequest().withAllocationId(alloc_id);
    ReleaseAddressResult response = ec2.releaseAddress(request);
    System.out.printf("Successfully released elastic IP address %s", alloc_id);
}
Also used : ReleaseAddressResult(com.amazonaws.services.ec2.model.ReleaseAddressResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) ReleaseAddressRequest(com.amazonaws.services.ec2.model.ReleaseAddressRequest)

Example 63 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project aws-doc-sdk-examples by awsdocs.

the class StartStopInstance method stopInstance.

public static void stopInstance(String instance_id) {
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    DryRunSupportedRequest<StopInstancesRequest> dry_request = () -> {
        StopInstancesRequest request = new StopInstancesRequest().withInstanceIds(instance_id);
        return request.getDryRunRequest();
    };
    DryRunResult dry_response = ec2.dryRun(dry_request);
    if (!dry_response.isSuccessful()) {
        System.out.printf("Failed dry run to stop instance %s", instance_id);
        throw dry_response.getDryRunResponse();
    }
    StopInstancesRequest request = new StopInstancesRequest().withInstanceIds(instance_id);
    ec2.stopInstances(request);
    System.out.printf("Successfully stop instance %s", instance_id);
}
Also used : StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DryRunResult(com.amazonaws.services.ec2.model.DryRunResult)

Example 64 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project aws-doc-sdk-examples by awsdocs.

the class DescribeRegionsAndZones method main.

public static void main(String[] args) {
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    DescribeRegionsResult regions_response = ec2.describeRegions();
    for (Region region : regions_response.getRegions()) {
        System.out.printf("Found region %s " + "with endpoint %s", region.getRegionName(), region.getEndpoint());
    }
    DescribeAvailabilityZonesResult zones_response = ec2.describeAvailabilityZones();
    for (AvailabilityZone zone : zones_response.getAvailabilityZones()) {
        System.out.printf("Found availability zone %s " + "with status %s " + "in region %s", zone.getZoneName(), zone.getState(), zone.getRegionName());
    }
}
Also used : DescribeRegionsResult(com.amazonaws.services.ec2.model.DescribeRegionsResult) Region(com.amazonaws.services.ec2.model.Region) AvailabilityZone(com.amazonaws.services.ec2.model.AvailabilityZone) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeAvailabilityZonesResult(com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult)

Example 65 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project aws-doc-sdk-examples by awsdocs.

the class MonitorInstance method monitorInstance.

public static void monitorInstance(String instance_id) {
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    DryRunSupportedRequest<MonitorInstancesRequest> dry_request = () -> {
        MonitorInstancesRequest request = new MonitorInstancesRequest().withInstanceIds(instance_id);
        return request.getDryRunRequest();
    };
    DryRunResult dry_response = ec2.dryRun(dry_request);
    if (!dry_response.isSuccessful()) {
        System.out.printf("Failed dry run to enable monitoring on instance %s", instance_id);
        throw dry_response.getDryRunResponse();
    }
    MonitorInstancesRequest request = new MonitorInstancesRequest().withInstanceIds(instance_id);
    ec2.monitorInstances(request);
    System.out.printf("Successfully enabled monitoring for instance %s", instance_id);
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) MonitorInstancesRequest(com.amazonaws.services.ec2.model.MonitorInstancesRequest) DryRunResult(com.amazonaws.services.ec2.model.DryRunResult)

Aggregations

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)42 Instance (com.amazonaws.services.ec2.model.Instance)12 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)9 AWSCredentials (com.amazonaws.auth.AWSCredentials)5 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)5 Address (com.amazonaws.services.ec2.model.Address)5 InstanceInfo (com.netflix.appinfo.InstanceInfo)5 File (java.io.File)5 LinkedList (java.util.LinkedList)5 DescribeAddressesResult (com.amazonaws.services.ec2.model.DescribeAddressesResult)4 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)4 DryRunResult (com.amazonaws.services.ec2.model.DryRunResult)4 Reservation (com.amazonaws.services.ec2.model.Reservation)4 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)4 Tag (com.amazonaws.services.ec2.model.Tag)4 AmazonInfo (com.netflix.appinfo.AmazonInfo)4 AssociateAddressRequest (com.amazonaws.services.ec2.model.AssociateAddressRequest)3 AvailabilityZone (com.amazonaws.services.ec2.model.AvailabilityZone)3 CreateTagsRequest (com.amazonaws.services.ec2.model.CreateTagsRequest)3 DescribeAvailabilityZonesResult (com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult)3