Search in sources :

Example 46 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project eureka by Netflix.

the class ElasticNetworkInterfaceBinder method getEC2Service.

private AmazonEC2 getEC2Service() {
    String aWSAccessId = serverConfig.getAWSAccessId();
    String aWSSecretKey = serverConfig.getAWSSecretKey();
    AmazonEC2 ec2Service;
    if (null != aWSAccessId && !"".equals(aWSAccessId) && null != aWSSecretKey && !"".equals(aWSSecretKey)) {
        ec2Service = new AmazonEC2Client(new BasicAWSCredentials(aWSAccessId, aWSSecretKey));
    } else {
        ec2Service = new AmazonEC2Client(new InstanceProfileCredentialsProvider());
    }
    String region = clientConfig.getRegion();
    region = region.trim().toLowerCase();
    ec2Service.setEndpoint("ec2." + region + ".amazonaws.com");
    return ec2Service;
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) InstanceProfileCredentialsProvider(com.amazonaws.auth.InstanceProfileCredentialsProvider) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials)

Example 47 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project GNS by MobilityFirst.

the class AWSEC2 method associateAddress.

/**
   *
   * @param ec2
   * @param ip
   * @param instance
   */
public static void associateAddress(AmazonEC2 ec2, String ip, Instance instance) {
    Address address;
    if ((address = findElasticIP(ec2, ip)) != null) {
        if (address.getDomain().equals("vpc")) {
            System.out.println("VPC Elastic IP:  " + ip);
            ec2.associateAddress(new AssociateAddressRequest().withInstanceId(instance.getInstanceId()).withAllocationId(address.getAllocationId()));
        } else {
            System.out.println("EC2 Classic Elastic IP:  " + ip);
            ec2.associateAddress(new AssociateAddressRequest(instance.getInstanceId(), ip));
        }
    }
}
Also used : Address(com.amazonaws.services.ec2.model.Address) AssociateAddressRequest(com.amazonaws.services.ec2.model.AssociateAddressRequest)

Example 48 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project GNS by MobilityFirst.

the class AWSEC2 method getInstances.

/**
   * Returns all the instances in this region.
   *
   * @param ec2
   * @return a set of instance instances
   */
public static Set<Instance> getInstances(AmazonEC2 ec2) {
    Set<Instance> instances = new HashSet<>();
    DescribeInstancesResult describeInstancesResult = ec2.describeInstances();
    List<Reservation> reservations = describeInstancesResult.getReservations();
    // add all instances to a Set.
    for (Reservation reservation : reservations) {
        instances.addAll(reservation.getInstances());
    }
    return instances;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) HashSet(java.util.HashSet)

Example 49 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project GNS by MobilityFirst.

the class AWSEC2 method terminateInstance.

/**
   * Terminate an instance
   *
   * @param ec2
   * @param createdInstanceId
   */
public static void terminateInstance(AmazonEC2 ec2, String createdInstanceId) {
    System.out.println("Terminating Instance:" + createdInstanceId);
    List<String> instanceIds = new LinkedList<>();
    instanceIds.add(createdInstanceId);
    TerminateInstancesRequest tir = new TerminateInstancesRequest(instanceIds);
    ec2.terminateInstances(tir);
}
Also used : LinkedList(java.util.LinkedList) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest)

Example 50 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project GNS by MobilityFirst.

the class AWSEC2 method getAvailabilityZones.

/**
   * Returns a list of strings of all the availability zones in the current region.
   *
   * @param ec2
   * @return a list of zone strings
   */
public static List<String> getAvailabilityZones(AmazonEC2 ec2) {
    ArrayList<String> result = new ArrayList<>();
    DescribeAvailabilityZonesResult availabilityZonesResult = ec2.describeAvailabilityZones();
    for (AvailabilityZone zone : availabilityZonesResult.getAvailabilityZones()) {
        result.add(zone.getZoneName());
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) AvailabilityZone(com.amazonaws.services.ec2.model.AvailabilityZone) DescribeAvailabilityZonesResult(com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult)

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