User guide

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>();
// Add all of the request ids to the hashset, so we can
120 // determine when they hit the active state.
for (SpotInstanceRequest requestResponse : requestResponses) {
System.out.println("Created Spot Request: "+requestResponse.getSpot
InstanceRequestId());
spotInstanceRequestIds.add(requestResponse.getSpotInstanceRequestId());
}
125
//==========================================================//
//============= Tag the Spot Requests ======================//
//==========================================================//
Version v1.0.0
58
AWS SDK for Java Developer Guide
Tutorial: Advanced Amazon EC2 Spot Request
Management