User guide
Table Of Contents
- AWS SDK for .NET
- Table of Contents
- AWS SDK for .NET Developer Guide
- Getting Started with the AWS SDK for .NET
- Programming with the AWS SDK for .NET
- AWS SDK for .NET Tutorials and Examples
- Managing ASP.NET Session State with Amazon DynamoDB
- Tutorial: Creating Amazon EC2 Instances with the AWS SDK for .NET
- Tutorial: Grant Access Using an IAM Role and the AWS SDK for .NET
- Tutorial: Amazon EC2 Spot Instances
- Creating and Using an Amazon SQS Queue with the AWS SDK for .NET
- Creating an Amazon Route 53 Hosted Zone and Adding Resource Record Sets
- Additional Resources
- Document History

To request a Spot Instance, you simply need to build your request with the parameters we have specified
so far. We start by creating a RequestSpotInstanceRequest object.The request object requires the
number of instances you want to start (2) and the bid price ($0.03). Additionally, you need to set the
LaunchSpecification for the request, which includes the instance type, AMI ID, and security group
you want to use. Once the request is populated, you call the requestSpotInstances method on the
AmazonEC2Client object. An example of how to request a Spot Instance is shown below.
1 RequestSpotInstancesRequest requestRequest = new RequestSpotInstances
Request();
requestRequest.SpotPrice = "0.03";
requestRequest.InstanceCount = 2;
5
LaunchSpecification launchSpecification = new LaunchSpecification();
launchSpecification.ImageId = "ami-fbf93092"; // latest Windows AMI as
of this writing
launchSpecification.InstanceType = "t1.micro";
10 launchSpecification.SecurityGroup.Add("GettingStartedGroup");
requestRequest.LaunchSpecification = launchSpecification;
RequestSpotInstancesResponse requestResult = ec2.RequestSpotInstances(re
questRequest);
There are other options you can use to configure your Spot Requests.To learn more, see RequestSpot-
Instances 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)
{
Version v2.0.0
55
AWS SDK for .NET Developer Guide
Step 4: Determining the State of Your Spot Request