Search in sources :

Example 1 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project deeplearning4j by deeplearning4j.

the class Ec2BoxCreator method create.

/**
     * Create the instances
     */
public void create() {
    RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId(amiId).withInstanceType(size).withKeyName(keyPair).withMinCount(1).withSecurityGroupIds(securityGroupId).withMaxCount(numBoxes);
    AmazonEC2 ec2 = getEc2();
    ec2.setRegion(com.amazonaws.regions.Region.getRegion(regions));
    List<Instance> boxes = ec2.runInstances(runInstancesRequest).getReservation().getInstances();
    if (boxesCreated == null) {
        boxesCreated = new ArrayList<>();
        for (Instance i : boxes) boxesCreated.add(i.getInstanceId());
        log.info("Boxes created " + boxesCreated);
    } else {
        blowupBoxes();
        boxesCreated.clear();
        for (Instance i : boxes) boxesCreated.add(i.getInstanceId());
    }
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2)

Example 2 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project elasticsearch by elastic.

the class AwsEc2ServiceImpl method client.

@Override
public synchronized AmazonEC2 client() {
    if (client != null) {
        return client;
    }
    this.client = new AmazonEC2Client(buildCredentials(logger, settings), buildConfiguration(logger, settings));
    String endpoint = findEndpoint(logger, settings);
    if (endpoint != null) {
        client.setEndpoint(endpoint);
    }
    return this.client;
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client)

Example 3 with AmazonEC2

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

the class AWSEC2 method describeInstances.

/**
   * Describe Current Instances
   *
   * @param ec2
   */
public static void describeInstances(AmazonEC2 ec2) {
    StringBuilder output = new StringBuilder();
    String prefix = currentTab + "Current Instances: ";
    DescribeInstancesResult describeInstancesResult = ec2.describeInstances();
    List<Reservation> reservations = describeInstancesResult.getReservations();
    Set<Instance> instances = new HashSet<Instance>();
    // add all instances to a Set.
    for (Reservation reservation : reservations) {
        instances.addAll(reservation.getInstances());
    }
    prefix = prefix.concat(" [" + instances.size() + " total] ");
    for (Instance ins : instances) {
        // instance state
        output.append(prefix);
        prefix = ", ";
        output.append(ins.getPublicDnsName());
        output.append(" (");
        output.append(ins.getInstanceId());
        output.append(") ");
        output.append(ins.getState().getName());
    }
    System.out.println(output);
}
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 4 with AmazonEC2

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

the class AWSEC2 method createAndAttachVolume.

/**
   * Creates a volume and attaches and mounts it on the instance at the specified mount point.
   *
   * @param ec2
   * @param instanceId
   * @param mountPoint
   * @return the id of the volume
   */
public static String createAndAttachVolume(AmazonEC2 ec2, String instanceId, String mountPoint) {
    // ATTACH A VOLUME
    Instance instance = findInstance(ec2, instanceId);
    String zone = instance.getPlacement().getAvailabilityZone();
    CreateVolumeRequest newVolumeRequest = new CreateVolumeRequest();
    //1.0GB
    newVolumeRequest.setSize(1);
    // set its available zone, it may change.
    newVolumeRequest.setAvailabilityZone(zone);
    CreateVolumeResult volumeResult = ec2.createVolume(newVolumeRequest);
    Volume v1 = volumeResult.getVolume();
    String volumeID = v1.getVolumeId();
    //begin to attach the volume to instance
    AttachVolumeRequest avr = new AttachVolumeRequest();
    avr.withInstanceId(instanceId);
    avr.withVolumeId(volumeID);
    //mount it
    avr.withDevice(mountPoint);
    ec2.attachVolume(avr);
    System.out.println("EBS volume has been attached and the volume ID is: " + volumeID);
    return (volumeID);
}
Also used : CreateVolumeRequest(com.amazonaws.services.ec2.model.CreateVolumeRequest) Instance(com.amazonaws.services.ec2.model.Instance) Volume(com.amazonaws.services.ec2.model.Volume) CreateVolumeResult(com.amazonaws.services.ec2.model.CreateVolumeResult) AttachVolumeRequest(com.amazonaws.services.ec2.model.AttachVolumeRequest)

Example 5 with AmazonEC2

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

the class AWSEC2 method main.

/**
   *
   * @param args
   * @throws Exception
   */
public static void main(String[] args) throws Exception {
    AWSCredentials credentials = new PropertiesCredentials(AWSEC2.class.getResourceAsStream(System.getProperty("user.home") + FILESEPARATOR + ".aws" + FILESEPARATOR + "credentials"));
    //Create Amazon Client object
    AmazonEC2 ec2 = new AmazonEC2Client(credentials);
    RegionRecord region = RegionRecord.US_EAST_1;
    String keyName = "aws";
    String installScript = "#!/bin/bash\n" + "cd /home/ec2-user\n" + "yum --quiet --assumeyes update\n";
    HashMap<String, String> tags = new HashMap<>();
    tags.put("runset", new Date().toString());
    createAndInitInstance(ec2, region, AMIRecord.getAMI(AMIRecordType.Amazon_Linux_AMI_2013_03_1, region), "Test Instance", keyName, DEFAULT_SECURITY_GROUP_NAME, installScript, tags, "23.21.120.250");
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) HashMap(java.util.HashMap) PropertiesCredentials(com.amazonaws.auth.PropertiesCredentials) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AWSCredentials(com.amazonaws.auth.AWSCredentials) Date(java.util.Date)

Aggregations

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)65 Filter (com.amazonaws.services.ec2.model.Filter)25 Instance (com.amazonaws.services.ec2.model.Instance)20 DescribeImagesRequest (com.amazonaws.services.ec2.model.DescribeImagesRequest)17 DescribeImagesResult (com.amazonaws.services.ec2.model.DescribeImagesResult)15 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)12 Reservation (com.amazonaws.services.ec2.model.Reservation)12 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)10 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)9 DescribeAddressesResult (com.amazonaws.services.ec2.model.DescribeAddressesResult)8 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)8 Tag (com.amazonaws.services.ec2.model.Tag)8 AWSCredentials (com.amazonaws.auth.AWSCredentials)6 DescribeAddressesRequest (com.amazonaws.services.ec2.model.DescribeAddressesRequest)6 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)5 DescribeSecurityGroupsResult (com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)5 DisassociateAddressRequest (com.amazonaws.services.ec2.model.DisassociateAddressRequest)5 ReleaseAddressRequest (com.amazonaws.services.ec2.model.ReleaseAddressRequest)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4