Search in sources :

Example 11 with RunInstancesResult

use of com.amazonaws.services.ec2.model.RunInstancesResult in project airavata by apache.

the class AmazonUtil method launchInstance.

/**
 * Launch a new EC2 instance
 *
 * @param AMI_ID
 * @param type
 * @param number
 * @param keyname
 * @return list of newly launched instances
 */
public static List<Instance> launchInstance(String AMI_ID, String type, Integer number, String keyname) {
    List<Instance> resultList = new ArrayList<Instance>();
    RunInstancesRequest request = new RunInstancesRequest(AMI_ID, number, number);
    request.setInstanceType(type);
    request.setKeyName(keyname);
    RunInstancesResult result = getEC2Client().runInstances(request);
    resultList.addAll(result.getReservation().getInstances());
    return resultList;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) ArrayList(java.util.ArrayList) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest)

Example 12 with RunInstancesResult

use of com.amazonaws.services.ec2.model.RunInstancesResult 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 reservation_id = run_response.getReservation().getInstances().get(0).getInstanceId();
    Tag tag = new Tag().withKey("Name").withValue(name);
    CreateTagsRequest tag_request = new CreateTagsRequest().withResources(reservation_id).withTags(tag);
    CreateTagsResult tag_response = ec2.createTags(tag_request);
    System.out.printf("Successfully started EC2 instance %s based on AMI %s", reservation_id, ami_id);
}
Also used : CreateTagsResult(com.amazonaws.services.ec2.model.CreateTagsResult) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) Tag(com.amazonaws.services.ec2.model.Tag)

Example 13 with RunInstancesResult

use of com.amazonaws.services.ec2.model.RunInstancesResult in project druid by druid-io.

the class EC2AutoScalerTest method testScale.

@Test
public void testScale() {
    RunInstancesResult runInstancesResult = EasyMock.createMock(RunInstancesResult.class);
    EC2AutoScaler autoScaler = new EC2AutoScaler(0, 1, ENV_CONFIG, amazonEC2Client, managementConfig);
    EasyMock.expect(amazonEC2Client.runInstances(EasyMock.anyObject(RunInstancesRequest.class))).andReturn(runInstancesResult);
    EasyMock.expect(amazonEC2Client.describeInstances(EasyMock.anyObject(DescribeInstancesRequest.class))).andReturn(describeInstancesResult);
    EasyMock.expect(amazonEC2Client.terminateInstances(EasyMock.anyObject(TerminateInstancesRequest.class))).andReturn(null);
    EasyMock.replay(amazonEC2Client);
    EasyMock.expect(runInstancesResult.getReservation()).andReturn(reservation).atLeastOnce();
    EasyMock.replay(runInstancesResult);
    EasyMock.expect(describeInstancesResult.getReservations()).andReturn(Collections.singletonList(reservation)).atLeastOnce();
    EasyMock.replay(describeInstancesResult);
    EasyMock.expect(reservation.getInstances()).andReturn(Collections.singletonList(instance)).atLeastOnce();
    EasyMock.replay(reservation);
    AutoScalingData created = autoScaler.provision();
    Assert.assertEquals(created.getNodeIds().size(), 1);
    Assert.assertEquals("theInstance", created.getNodeIds().get(0));
    AutoScalingData deleted = autoScaler.terminate(Collections.singletonList("dummyIP"));
    Assert.assertEquals(deleted.getNodeIds().size(), 1);
    Assert.assertEquals(INSTANCE_ID, deleted.getNodeIds().get(0));
    EasyMock.verify(runInstancesResult);
}
Also used : AutoScalingData(org.apache.druid.indexing.overlord.autoscaling.AutoScalingData) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) Test(org.junit.Test)

Example 14 with RunInstancesResult

use of com.amazonaws.services.ec2.model.RunInstancesResult 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;
}
Also used : AutoScalingData(io.druid.indexing.overlord.autoscaling.AutoScalingData) Instance(com.amazonaws.services.ec2.model.Instance) InstanceNetworkInterfaceSpecification(com.amazonaws.services.ec2.model.InstanceNetworkInterfaceSpecification) Function(com.google.common.base.Function) Placement(com.amazonaws.services.ec2.model.Placement) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest)

Example 15 with RunInstancesResult

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

the class AWSEC2 method createInstanceAndWait.

/**
   * Create an Instance
   *
   * @param ec2
   * @param amiRecord
   * @param key
   * @param securityGroup
   * @return the instanceID string
   */
public static String createInstanceAndWait(AmazonEC2 ec2, AMIRecord amiRecord, String key, SecurityGroup securityGroup) {
    RunInstancesRequest runInstancesRequest;
    if (amiRecord.getVpcSubnet() != null) {
        System.out.println("subnet: " + amiRecord.getVpcSubnet() + " securityGroup: " + securityGroup.getGroupName());
        // new VPC
        runInstancesRequest = new RunInstancesRequest().withMinCount(1).withMaxCount(1).withImageId(amiRecord.getName()).withInstanceType(amiRecord.getInstanceType()).withKeyName(key).withSubnetId(amiRecord.getVpcSubnet()).withSecurityGroupIds(Arrays.asList(securityGroup.getGroupId()));
    } else {
        runInstancesRequest = new RunInstancesRequest(amiRecord.getName(), 1, 1);
        runInstancesRequest.setInstanceType(amiRecord.getInstanceType());
        runInstancesRequest.setSecurityGroups(new ArrayList<>(Arrays.asList(securityGroup.getGroupName())));
        runInstancesRequest.setKeyName(key);
    }
    RunInstancesResult runInstancesResult = ec2.runInstances(runInstancesRequest);
    Instance instance = runInstancesResult.getReservation().getInstances().get(0);
    String createdInstanceId = instance.getInstanceId();
    System.out.println("Waiting for instance " + amiRecord.getName() + " to start");
    long startTime = System.currentTimeMillis();
    do {
        ThreadUtils.sleep(1000);
        if (System.currentTimeMillis() - startTime > 90000) {
            // give it a minute and a half
            System.out.println(createdInstanceId + " timed out waiting for start.");
            return null;
        }
        // regrab the instance data from the server
        instance = findInstance(ec2, createdInstanceId);
        //System.out.print(instance.getState().getName());
        System.out.print(".");
    } while (instance != null && !instance.getState().getName().equals(InstanceStateRecord.RUNNING.getName()));
    System.out.println();
    return createdInstanceId;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest)

Aggregations

RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)19 RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)12 Test (org.junit.Test)8 Instance (com.amazonaws.services.ec2.model.Instance)6 Exchange (org.apache.camel.Exchange)5 Processor (org.apache.camel.Processor)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4 Placement (com.amazonaws.services.ec2.model.Placement)4 ArrayList (java.util.ArrayList)4 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)3 Collection (java.util.Collection)3 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)2 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)2 InstanceNetworkInterfaceSpecification (com.amazonaws.services.ec2.model.InstanceNetworkInterfaceSpecification)2 Tag (com.amazonaws.services.ec2.model.Tag)2 TerminateInstancesRequest (com.amazonaws.services.ec2.model.TerminateInstancesRequest)2 Function (com.google.common.base.Function)2 UnknownHostException (java.net.UnknownHostException)2 TimeoutException (java.util.concurrent.TimeoutException)2 AutoScalingData (org.apache.druid.indexing.overlord.autoscaling.AutoScalingData)2