Search in sources :

Example 1 with RunInstancesRequest

use of software.amazon.awssdk.services.ec2.model.RunInstancesRequest in project airavata by apache.

the class AmazonUtil method launchInstance.

/**
 * Launch a new EC2 instance
 *
 * @param AMI_ID
 * @param type
 * @param number
 * @return list of newly launched instances
 */
public static List<Instance> launchInstance(String AMI_ID, String type, Integer number) {
    List<Instance> resultList = new ArrayList<Instance>();
    RunInstancesRequest request = new RunInstancesRequest(AMI_ID, number, number);
    request.setInstanceType(type);
    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 2 with RunInstancesRequest

use of software.amazon.awssdk.services.ec2.model.RunInstancesRequest in project photon-model by vmware.

the class TestAWSSetupUtils method provisionAWSEBSVMWithEC2Client.

public static String provisionAWSEBSVMWithEC2Client(VerificationHost host, AmazonEC2Client client, String ami, String subnetId, String securityGroupId, BlockDeviceMapping blockDeviceMapping) {
    RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withSubnetId(subnetId).withImageId(ami).withInstanceType(instanceType).withMinCount(1).withMaxCount(1).withSecurityGroupIds(securityGroupId).withBlockDeviceMappings(blockDeviceMapping);
    // handler invoked once the EC2 runInstancesAsync commands completes
    RunInstancesResult result = null;
    try {
        result = client.runInstances(runInstancesRequest);
    } catch (Exception e) {
        host.log(Level.SEVERE, "Error encountered in provisioning machine on AWS", Utils.toString(e));
    }
    assertNotNull(result);
    assertNotNull(result.getReservation());
    assertNotNull(result.getReservation().getInstances());
    assertEquals(1, result.getReservation().getInstances().size());
    String instanceId = result.getReservation().getInstances().get(0).getInstanceId();
    tagResources(client, Arrays.asList(instanceId), TAG_KEY_FOR_TEST_RESOURCES, TAG_VALUE_FOR_TEST_RESOURCES + TAG_INSTANCE);
    return instanceId;
}
Also used : RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) UnknownHostException(java.net.UnknownHostException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with RunInstancesRequest

use of software.amazon.awssdk.services.ec2.model.RunInstancesRequest in project photon-model by vmware.

the class TestAWSSetupUtils method provisionAWSVMWithEC2Client.

public static String provisionAWSVMWithEC2Client(VerificationHost host, AmazonEC2Client client, String ami, String subnetId, String securityGroupId) {
    RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withSubnetId(subnetId).withImageId(ami).withInstanceType(instanceType).withMinCount(1).withMaxCount(1).withSecurityGroupIds(securityGroupId);
    // handler invoked once the EC2 runInstancesAsync commands completes
    RunInstancesResult result = null;
    try {
        result = client.runInstances(runInstancesRequest);
    } catch (Exception e) {
        host.log(Level.SEVERE, "Error encountered in provisioning machine on AWS", Utils.toString(e));
    }
    assertNotNull(result);
    assertNotNull(result.getReservation());
    assertNotNull(result.getReservation().getInstances());
    assertEquals(1, result.getReservation().getInstances().size());
    String instanceId = result.getReservation().getInstances().get(0).getInstanceId();
    tagResources(client, Arrays.asList(instanceId), TAG_KEY_FOR_TEST_RESOURCES, TAG_VALUE_FOR_TEST_RESOURCES + TAG_INSTANCE);
    return instanceId;
}
Also used : RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) UnknownHostException(java.net.UnknownHostException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with RunInstancesRequest

use of software.amazon.awssdk.services.ec2.model.RunInstancesRequest 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 5 with RunInstancesRequest

use of software.amazon.awssdk.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 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)

Aggregations

RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)12 RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)10 Instance (com.amazonaws.services.ec2.model.Instance)5 Placement (com.amazonaws.services.ec2.model.Placement)4 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)3 ArrayList (java.util.ArrayList)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)2 InstanceNetworkInterfaceSpecification (com.amazonaws.services.ec2.model.InstanceNetworkInterfaceSpecification)2 Function (com.google.common.base.Function)2 UnknownHostException (java.net.UnknownHostException)2 TimeoutException (java.util.concurrent.TimeoutException)2 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)1 AuthorizeSecurityGroupIngressRequest (com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest)1 BlockDeviceMapping (com.amazonaws.services.ec2.model.BlockDeviceMapping)1 CreateKeyPairRequest (com.amazonaws.services.ec2.model.CreateKeyPairRequest)1 CreateKeyPairResult (com.amazonaws.services.ec2.model.CreateKeyPairResult)1 CreateSecurityGroupRequest (com.amazonaws.services.ec2.model.CreateSecurityGroupRequest)1 CreateTagsRequest (com.amazonaws.services.ec2.model.CreateTagsRequest)1 CreateTagsResult (com.amazonaws.services.ec2.model.CreateTagsResult)1