User guide
There are other options you can use to configure your Spot Requests.To learn more, see
RequestSpotInstances in the AWS SDK for .NET.
Running this code will launch a new Spot Instance Request.
Note
You will be charged for any Spot Instances that are actually launched, so make sure that you
cancel any requests and terminate any instances you launch to reduce any associated fees.
Step 4: Determining the State of Your Spot Request
Next, we want to create code to wait until the Spot Request reaches the "active" state before proceeding
to the last step. To determine the state of our Spot Request, we poll the describeSpotInstanceRequests
method for the state of the Spot Request ID we want to monitor.
The request ID created in Step 2 is embedded in the result of our requestSpotInstances request.
The following example code gathers request IDs from the requestSpotInstances result and uses
them to populate the SpotInstanceRequestId member of a describeRequest object. We will use
this object in the next part of the sample.
1 // Call the RequestSpotInstance API.
RequestSpotInstancesResponse requestResult = ec2.RequestSpotInstances(re
questRequest);
// Create the describeRequest object with all of the request ids
5 // to monitor (e.g. that we started).
DescribeSpotInstanceRequestsRequest describeRequest = new DescribeSpotIn
stanceRequestsRequest();
foreach (SpotInstanceRequest spotInstanceRequest in requestResult.Request
SpotInstancesResult.SpotInstanceRequest)
{
describeRequest.SpotInstanceRequestId.Add(spotInstanceRequest.SpotIn
stanceRequestId);
10 }
1 // Create a variable that will track whether there are any
// requests still in the open state.
bool anyOpen;
5 // Create a list to store any instances that were activated.
List<String> instanceIds = new List<String>();
do
{
10 // Initialize the anyOpen variable to false, which assumes there
// are no requests open unless we find one that is still open.
anyOpen = false;
instanceIds.Clear();
15 try
{
// Retrieve all of the requests we want to monitor.
DescribeSpotInstanceRequestsResponse describeResponse = ec2.De
scribeSpotInstanceRequests(describeRequest);
20 // Look through each request and determine if they are all in
// the active state.
Version v2.0.0
84
AWS SDK for .NET Developer Guide
Tutorial: Amazon EC2 Spot Instances