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

The following examples show how to use com.amazonaws.services.ec2.AmazonEC2#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: CreateInstance.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
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()
        .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);
}
 
Example 2
Source File: Ec2IaasHandler.java    From roboconf-platform with Apache License 2.0 5 votes vote down vote up
@Override
public String createMachine( TargetHandlerParameters parameters ) throws TargetException {

	this.logger.fine( "Creating a new machine on AWS." );

	// For IaaS, we only expect root instance names to be passed
	if( InstanceHelpers.countInstances( parameters.getScopedInstancePath()) > 1 )
		throw new TargetException( "Only root instances can be passed in arguments." );

	String rootInstanceName = InstanceHelpers.findRootInstancePath( parameters.getScopedInstancePath());

	// Deal with the creation
	String instanceId;
	try {
		AmazonEC2 ec2 = createEc2Client( parameters.getTargetProperties());
		String userData = UserDataHelpers.writeUserDataAsString(
				parameters.getMessagingProperties(),
				parameters.getDomain(),
				parameters.getApplicationName(),
				rootInstanceName );

		RunInstancesRequest runInstancesRequest = prepareEC2RequestNode( parameters.getTargetProperties(), userData );
		RunInstancesResult runInstanceResult = ec2.runInstances( runInstancesRequest );
		instanceId = runInstanceResult.getReservation().getInstances().get( 0 ).getInstanceId();

	} catch( Exception e ) {
		this.logger.severe( "An error occurred while creating a new machine on EC2. " + e.getMessage());
		throw new TargetException( e );
	}

	return instanceId;
}