Search in sources :

Example 21 with Instance

use of com.amazonaws.services.ec2.model.Instance 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)

Example 22 with Instance

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

the class AWSEC2 method stopInstance.

/**
   * Stop an instance
   *
   * @param ec2
   * @param createdInstanceId
   */
public static void stopInstance(AmazonEC2 ec2, String createdInstanceId) {
    System.out.println("Stopping Instance:" + createdInstanceId);
    List<String> instanceIds = new LinkedList<>();
    instanceIds.add(createdInstanceId);
    StopInstancesRequest stopIR = new StopInstancesRequest(instanceIds);
    ec2.stopInstances(stopIR);
}
Also used : StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) LinkedList(java.util.LinkedList)

Example 23 with Instance

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

the class AWSEC2 method findInstance.

/**
   * Find an instance
   *
   * @param ec2
   * @param createdInstanceId
   * @return the name of the instance or null
   */
public static Instance findInstance(AmazonEC2 ec2, String createdInstanceId) {
    DescribeInstancesResult describeInstancesResult = ec2.describeInstances();
    List<Reservation> reservations = describeInstancesResult.getReservations();
    Set<Instance> instances = new HashSet<>();
    // add all instances to a Set.
    for (Reservation reservation : reservations) {
        instances.addAll(reservation.getInstances());
    }
    for (Instance instance : instances) {
        if (createdInstanceId.equals(instance.getInstanceId())) {
            return instance;
        }
    }
    return null;
}
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 24 with Instance

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

the class AWSEC2 method addInstanceTags.

/**
   * Adds keys and values from tagmap to the tags of the given instance.
   *
   * @param ec2
   * @param createdInstanceId
   * @param tagmap
   */
public static void addInstanceTags(AmazonEC2 ec2, String createdInstanceId, Map<String, String> tagmap) {
    List<String> resources = new LinkedList<>();
    resources.add(createdInstanceId);
    List<Tag> tags = new LinkedList<>();
    for (Entry<String, String> entry : tagmap.entrySet()) {
        Tag nameTag = new Tag(entry.getKey(), entry.getValue());
        tags.add(nameTag);
    }
    CreateTagsRequest ctr = new CreateTagsRequest(resources, tags);
    ec2.createTags(ctr);
}
Also used : CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) Tag(com.amazonaws.services.ec2.model.Tag) LinkedList(java.util.LinkedList)

Example 25 with Instance

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

the class AWSEC2 method startInstance.

/**
   * Start an instance
   *
   * @param ec2
   * @param createdInstanceId
   */
public static void startInstance(AmazonEC2 ec2, String createdInstanceId) {
    System.out.println("Starting Instance:" + createdInstanceId);
    List<String> instanceIds = new LinkedList<>();
    instanceIds.add(createdInstanceId);
    StartInstancesRequest startIR = new StartInstancesRequest(instanceIds);
    ec2.startInstances(startIR);
}
Also used : StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest) LinkedList(java.util.LinkedList)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)33 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)20 Reservation (com.amazonaws.services.ec2.model.Reservation)15 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)12 RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)11 Test (org.junit.Test)10 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)7 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)7 Filter (com.amazonaws.services.ec2.model.Filter)7 Tag (com.amazonaws.services.ec2.model.Tag)7 AWSClient (com.netflix.simianarmy.client.aws.AWSClient)7 Exchange (org.apache.camel.Exchange)7 Processor (org.apache.camel.Processor)7 AmazonServiceException (com.amazonaws.AmazonServiceException)6 ArrayList (java.util.ArrayList)6 LinkedList (java.util.LinkedList)6 AWSCredentials (com.amazonaws.auth.AWSCredentials)5 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)5 AutoScalingInstanceDetails (com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails)5 RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)5