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");
}
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);
}
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;
}
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);
}
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);
}
Aggregations