Java Code Examples for com.amazonaws.services.ec2.AmazonEC2Client#runInstances()

The following examples show how to use com.amazonaws.services.ec2.AmazonEC2Client#runInstances() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RunInstancesExample.java    From aws-mock with MIT License 6 votes vote down vote up
/**
 * Run some new ec2 instances.
 *
 * @param imageId
 *            AMI for running new instances from
 * @param runCount
 *            count of instances to run
 * @return a list of started instances
 */
public static List<Instance> runInstances(final String imageId, final int runCount) {
    // pass any credentials as aws-mock does not authenticate them at all
    AWSCredentials credentials = new BasicAWSCredentials("foo", "bar");
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);

    // the mock endpoint for ec2 which runs on your computer
    String ec2Endpoint = "http://localhost:8000/aws-mock/ec2-endpoint/";
    amazonEC2Client.setEndpoint(ec2Endpoint);

    // instance type
    String instanceType = "m1.large";

    // run 10 instances
    RunInstancesRequest request = new RunInstancesRequest();
    final int minRunCount = runCount;
    final int maxRunCount = runCount;
    request.withImageId(imageId).withInstanceType(instanceType)
            .withMinCount(minRunCount).withMaxCount(maxRunCount);
    RunInstancesResult result = amazonEC2Client.runInstances(request);

    return result.getReservation().getInstances();
}
 
Example 2
Source File: SimpleInstanceService.java    From sequenceiq-samples with Apache License 2.0 5 votes vote down vote up
private RunInstancesResult runInstances(AmazonEC2Client client, String amiId, String keyName, String securityGroup, String gist) {
	RunInstancesRequest runInstancesRequest = new RunInstancesRequest(amiId, 1, 1);
	runInstancesRequest.withInstanceType("t1.micro");
	runInstancesRequest.withKeyName(keyName);
	runInstancesRequest.withUserData(new String(Base64.encodeBase64(readGist(gist).getBytes())));
	runInstancesRequest.withSecurityGroups(securityGroup);
	RunInstancesResult runInstancesResult = client.runInstances(runInstancesRequest);
	return runInstancesResult;
}
 
Example 3
Source File: Ec2Util.java    From s3-bucket-loader with Apache License 2.0 4 votes vote down vote up
public List<Instance> launchEc2Instances(AmazonEC2Client ec2Client, Properties props) throws Exception {
	
	Integer totalExpectedWorkers = Integer.valueOf(props.getProperty("master.workers.total"));
	
	// disk size
	Collection<BlockDeviceMapping> blockDevices = new ArrayList<BlockDeviceMapping>();
	blockDevices.add(
			new BlockDeviceMapping()
				.withDeviceName(props.getProperty("master.workers.ec2.disk.deviceName"))
				.withEbs(new EbsBlockDevice()
						.withVolumeType(VolumeType.valueOf(props.getProperty("master.workers.ec2.disk.volumeType")))
						.withDeleteOnTermination(true)
					    .withVolumeSize(Integer.valueOf(props.getProperty("master.workers.ec2.disk.size.gigabytes")))));
	
	// create our run request for the total workers we expect
	RunInstancesRequest runInstancesRequest = new RunInstancesRequest();
	runInstancesRequest.withImageId(props.getProperty("master.workers.ec2.ami.id"))
				        .withInstanceType(props.getProperty("master.workers.ec2.instanceType"))
				        .withMinCount(totalExpectedWorkers)
				        .withMaxCount(totalExpectedWorkers)
				        .withBlockDeviceMappings(blockDevices)
				        .withKeyName(props.getProperty("master.workers.ec2.keyName"))
				        .withSecurityGroupIds(props.getProperty("master.workers.ec2.securityGroupId"))
				        .withInstanceInitiatedShutdownBehavior(ShutdownBehavior.valueOf(props.getProperty("master.workers.ec2.shutdownBehavior")))
				        .withSubnetId(props.getProperty("master.workers.ec2.subnetId"))
				        .withUserData(Base64.encodeAsString(readFile(props.getProperty("master.workers.ec2.userDataFile")).getBytes()));
	
	// launch
	logger.debug("Launching " + totalExpectedWorkers + " EC2 instances, " +
						"it may take few minutes for workers to come up...: \n" +
						"\tamiId:" + runInstancesRequest.getImageId() +"\n"+
						"\tsecGrpId:" + runInstancesRequest.getSecurityGroupIds().get(0) +"\n"+
						"\tsubnetId:" + runInstancesRequest.getSubnetId() +"\n"+
						"\tinstanceType:" + runInstancesRequest.getInstanceType() +"\n"+
						"\tshutdownBehavior:" + runInstancesRequest.getInstanceInitiatedShutdownBehavior() +"\n"+
						"\tkeyName:" + runInstancesRequest.getKeyName() 
						);


	// as the instances come up, assuming the "userData" above launches the worker we will be good
	// they will auto register w/ us the master 
	RunInstancesResult result = ec2Client.runInstances(runInstancesRequest);
	Reservation reservation = result.getReservation();
	return reservation.getInstances();
}