User guide

//================ Submitting a Request ========================//
//==============================================================//
// Retrieves the credentials from an AWSCredentials.properties file.
70 AWSCredentials credentials = null;
try {
credentials = new PropertiesCredentials(
GettingStartedApp.class.getResourceAsStream("AwsCredentials.proper
ties"));
} catch (IOException e1) {
75 System.out.println("Credentials were not properly entered into
AwsCredentials.properties.");
System.out.println(e1.getMessage());
System.exit(-1);
}
80 // Create the AmazonEC2Client object so we can
// call various APIs.
AmazonEC2 ec2 = new AmazonEC2Client(credentials);
// Initializes a Spot Instance Request
85 RequestSpotInstancesRequest requestRequest = new RequestSpotInstances
Request();
// Request 1 x t1.micro instance with a bid price of $0.03.
requestRequest.setSpotPrice("0.03");
requestRequest.setInstanceCount(Integer.valueOf(1));
90
// Set up the specifications of the launch. This includes
// the instance type (e.g. t1.micro) and the latest Amazon
// Linux AMI id available. Note, you should always use the
// latest Amazon Linux AMI id or another of your choosing.
95 LaunchSpecification launchSpecification = new LaunchSpecification();
launchSpecification.setImageId("ami-8c1fece5");
launchSpecification.setInstanceType("t1.micro");
// Add the security group to the request.
100 ArrayList<String> securityGroups = new ArrayList<String>();
securityGroups.add("GettingStartedGroup");
launchSpecification.setSecurityGroups(securityGroups);
// Add the launch specifications to the request.
105 requestRequest.setLaunchSpecification(launchSpecification);
//============================================================//
//======== Getting the Request ID from the Request ===========//
//============================================================//
110
// Call the RequestSpotInstance API.
RequestSpotInstancesResult requestResult = ec2.requestSpotInstances(re
questRequest);
List<SpotInstanceRequest> requestResponses = requestResult.getSpotIn
stanceRequests();
115 // Set up an arraylist to collect all of the request ids we want to
// watch hit the running state.
ArrayList<String> spotInstanceRequestIds = new ArrayList<String>();
Version v1.0.0
59
AWS SDK for Java Developer Guide
Tutorial: Advanced Amazon EC2 Spot Request
Management