use of com.amazonaws.services.ec2.model.RunInstancesRequest 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());
}
}
use of com.amazonaws.services.ec2.model.RunInstancesRequest in project h2o-2 by h2oai.
the class EC2 method resize.
//@formatter:on
/**
* Create or terminate EC2 instances. Uses their Name tag to find existing ones.
*/
public Cloud resize() throws Exception {
AmazonEC2Client ec2 = new AmazonEC2Client(new PersistS3.H2OAWSCredentialsProviderChain());
ec2.setEndpoint("ec2." + region + ".amazonaws.com");
DescribeInstancesResult describeInstancesResult = ec2.describeInstances();
List<Reservation> reservations = describeInstancesResult.getReservations();
List<Instance> instances = new ArrayList<Instance>();
for (Reservation reservation : reservations) {
for (Instance instance : reservation.getInstances()) {
String ip = ip(instance);
if (ip != null) {
String name = null;
if (instance.getTags().size() > 0)
name = instance.getTags().get(0).getValue();
if (NAME.equals(name))
instances.add(instance);
}
}
}
System.out.println("Found " + instances.size() + " EC2 instances for user " + USER);
if (instances.size() > boxes) {
for (int i = 0; i < instances.size() - boxes; i++) {
// TODO terminate?
}
} else if (instances.size() < boxes) {
int launchCount = boxes - instances.size();
System.out.println("Creating " + launchCount + " EC2 instances.");
if (confirm) {
System.out.println("Please confirm [y/n]");
String s = Utils.readConsole();
if (s == null || !s.equalsIgnoreCase("y"))
throw new Exception("Aborted");
}
CreatePlacementGroupRequest group = new CreatePlacementGroupRequest();
group.withGroupName(USER);
group.withStrategy(PlacementStrategy.Cluster);
try {
ec2.createPlacementGroup(group);
} catch (AmazonServiceException ex) {
if (!"InvalidPlacementGroup.Duplicate".equals(ex.getErrorCode()))
throw ex;
}
RunInstancesRequest run = new RunInstancesRequest();
run.withInstanceType(type);
run.withImageId(image);
run.withMinCount(launchCount).withMaxCount(launchCount);
run.withSecurityGroupIds(securityGroup);
Placement placement = new Placement();
placement.setGroupName(USER);
run.withPlacement(placement);
BlockDeviceMapping map = new BlockDeviceMapping();
map.setDeviceName("/dev/sdb");
map.setVirtualName("ephemeral0");
run.withBlockDeviceMappings(map);
run.withUserData(new String(Base64.encodeBase64(cloudConfig.getBytes())));
RunInstancesResult runRes = ec2.runInstances(run);
ArrayList<String> ids = new ArrayList<String>();
for (Instance instance : runRes.getReservation().getInstances()) ids.add(instance.getInstanceId());
List<Instance> created = wait(ec2, ids);
System.out.println("Created " + created.size() + " EC2 instances.");
instances.addAll(created);
}
String[] pub = new String[boxes];
String[] prv = new String[boxes];
for (int i = 0; i < boxes; i++) {
pub[i] = instances.get(i).getPublicIpAddress();
prv[i] = instances.get(i).getPrivateIpAddress();
}
System.out.println("EC2 public IPs: " + Utils.join(' ', pub));
System.out.println("EC2 private IPs: " + Utils.join(' ', prv));
Cloud cloud = new Cloud();
cloud.publicIPs.addAll(Arrays.asList(pub));
cloud.privateIPs.addAll(Arrays.asList(prv));
return cloud;
}
use of com.amazonaws.services.ec2.model.RunInstancesRequest in project aws-doc-sdk-examples by awsdocs.
the class CreateInstance method main.
public static void main(String[] args) {
final String USAGE = "To run this example, supply an instance name and AMI image id\n" + "Ex: CreateInstance <instance-name> <ami-image-id>\n";
if (args.length != 2) {
System.out.println(USAGE);
System.exit(1);
}
String name = args[0];
String ami_id = args[1];
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
RunInstancesRequest run_request = new RunInstancesRequest().withImageId(ami_id).withInstanceType(InstanceType.T1Micro).withMaxCount(1).withMinCount(1);
RunInstancesResult run_response = ec2.runInstances(run_request);
String instance_id = run_response.getReservation().getReservationId();
Tag tag = new Tag().withKey("Name").withValue(name);
CreateTagsRequest tag_request = new CreateTagsRequest().withTags(tag);
CreateTagsResult tag_response = ec2.createTags(tag_request);
System.out.printf("Successfully started EC2 instance %s based on AMI %s", instance_id, ami_id);
}
use of com.amazonaws.services.ec2.model.RunInstancesRequest in project druid by druid-io.
the class EC2AutoScaler method provision.
@Override
public AutoScalingData provision() {
try {
final EC2NodeData workerConfig = envConfig.getNodeData();
final String userDataBase64;
if (envConfig.getUserData() == null) {
userDataBase64 = null;
} else {
if (config.getWorkerVersion() == null) {
userDataBase64 = envConfig.getUserData().getUserDataBase64();
} else {
userDataBase64 = envConfig.getUserData().withVersion(config.getWorkerVersion()).getUserDataBase64();
}
}
RunInstancesRequest request = new RunInstancesRequest(workerConfig.getAmiId(), workerConfig.getMinInstances(), workerConfig.getMaxInstances()).withInstanceType(workerConfig.getInstanceType()).withPlacement(new Placement(envConfig.getAvailabilityZone())).withKeyName(workerConfig.getKeyName()).withIamInstanceProfile(workerConfig.getIamProfile() == null ? null : workerConfig.getIamProfile().toIamInstanceProfileSpecification()).withUserData(userDataBase64);
// leaving it null uses the EC2 default.
if (workerConfig.getAssociatePublicIpAddress() != null) {
request.withNetworkInterfaces(new InstanceNetworkInterfaceSpecification().withAssociatePublicIpAddress(workerConfig.getAssociatePublicIpAddress()).withSubnetId(workerConfig.getSubnetId()).withGroups(workerConfig.getSecurityGroupIds()).withDeviceIndex(0));
} else {
request.withSecurityGroupIds(workerConfig.getSecurityGroupIds()).withSubnetId(workerConfig.getSubnetId());
}
final RunInstancesResult result = amazonEC2Client.runInstances(request);
final List<String> instanceIds = Lists.transform(result.getReservation().getInstances(), new Function<Instance, String>() {
@Override
public String apply(Instance input) {
return input.getInstanceId();
}
});
log.info("Created instances: %s", instanceIds);
return new AutoScalingData(Lists.transform(result.getReservation().getInstances(), new Function<Instance, String>() {
@Override
public String apply(Instance input) {
return input.getInstanceId();
}
}));
} catch (Exception e) {
log.error(e, "Unable to provision any EC2 instances.");
}
return null;
}
use of com.amazonaws.services.ec2.model.RunInstancesRequest in project camel by apache.
the class EC2Producer method createAndRunInstance.
private void createAndRunInstance(AmazonEC2Client ec2Client, Exchange exchange) {
String ami;
InstanceType instanceType;
int minCount;
int maxCount;
boolean monitoring;
String kernelId;
boolean ebsOptimized;
Collection securityGroups;
String keyName;
String clientToken;
Placement placement;
RunInstancesRequest request = new RunInstancesRequest();
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.IMAGE_ID))) {
ami = exchange.getIn().getHeader(EC2Constants.IMAGE_ID, String.class);
request.withImageId(ami);
} else {
throw new IllegalArgumentException("AMI must be specified");
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE))) {
instanceType = exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE, InstanceType.class);
request.withInstanceType(instanceType.toString());
} else {
throw new IllegalArgumentException("Instance Type must be specified");
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT))) {
minCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT, Integer.class);
request.withMinCount(minCount);
} else {
throw new IllegalArgumentException("Min instances count must be specified");
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT))) {
maxCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT, Integer.class);
request.withMaxCount(maxCount);
} else {
throw new IllegalArgumentException("Max instances count must be specified");
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING))) {
monitoring = exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING, Boolean.class);
request.withMonitoring(monitoring);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID))) {
kernelId = exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID, String.class);
request.withKernelId(kernelId);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED))) {
ebsOptimized = exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED, Boolean.class);
request.withEbsOptimized(ebsOptimized);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_SECURITY_GROUPS))) {
securityGroups = exchange.getIn().getHeader(EC2Constants.INSTANCE_SECURITY_GROUPS, Collection.class);
request.withSecurityGroups(securityGroups);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_KEY_PAIR))) {
keyName = exchange.getIn().getHeader(EC2Constants.INSTANCES_KEY_PAIR, String.class);
request.withKeyName(keyName);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_CLIENT_TOKEN))) {
clientToken = exchange.getIn().getHeader(EC2Constants.INSTANCES_CLIENT_TOKEN, String.class);
request.withClientToken(clientToken);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_PLACEMENT))) {
placement = exchange.getIn().getHeader(EC2Constants.INSTANCES_PLACEMENT, Placement.class);
request.withPlacement(placement);
}
RunInstancesResult result;
try {
result = ec2Client.runInstances(request);
} catch (AmazonServiceException ase) {
LOG.trace("Run Instances command returned the error code {}", ase.getErrorCode());
throw ase;
}
LOG.trace("Creating and running instances with ami [{}] and instance type {}", ami, instanceType.toString());
Message message = getMessageForResponse(exchange);
message.setBody(result);
}
Aggregations