Amazon AWS SDK Betriebsanweisung Seite 59

  • Herunterladen
  • Zu meinen Handbüchern hinzufügen
  • Drucken
  • Seite
    / 73
  • Inhaltsverzeichnis
  • LESEZEICHEN
  • Bewertet. / 5. Basierend auf Kundenbewertungen
Seitenansicht 58
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
Seitenansicht 58
1 2 ... 54 55 56 57 58 59 60 61 62 63 64 ... 72 73

Kommentare zu diesen Handbüchern

Keine Kommentare