Ejemplo n.º 1
0
        // snippet-end:[ec2.dotnet.spot_instance_request_spot_instance]

        // snippet-start:[ec2.dotnet.spot_instance_get_spot_request_state]

        /* Gets the state of a spot instance request.
         * Takes two args:
         *   AmazonEC2Client ec2Client is the EC2 client through which information about the state of the spot instance is made
         *   string spotRequestId is the ID of the spot instance
         *
         * See https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/EC2/MEC2DescribeSpotInstanceRequests.html
         */
        private static SpotInstanceState GetSpotRequestState(
            AmazonEC2Client ec2Client,
            string spotRequestId)
        {
            // Create the describeRequest object with all of the request ids
            // to monitor (e.g. that we started).
            var request = new DescribeSpotInstanceRequestsRequest();

            request.SpotInstanceRequestIds.Add(spotRequestId);

            // Retrieve the request we want to monitor.
            var describeResponse = ec2Client.DescribeSpotInstanceRequestsAsync(request);

            SpotInstanceRequest req = describeResponse.Result.SpotInstanceRequests[0];

            return(req.State);
        }
Ejemplo n.º 2
0
    private async Task WaitAndTry(SpotInstanceRequest spotRequest)
    {
        for (int i = 1; i <= 6; i++)
        {
            await Task.Delay(10000);

            var getSpotInstanceRequest = SpotInstanceRequest.Get($"get-spot-instance-{i}", spotRequest.Id, null, new CustomResourceOptions {
                DependsOn = spotRequest
            });

            Console.WriteLine($"SpotInstanceId from GET request: {getSpotInstanceRequest.SpotInstanceId}");
            Console.WriteLine($"SpotInstanceId from GET request: {getSpotInstanceRequest.SpotInstanceId.ToString()}");
            Console.WriteLine($"SpotInstanceId from GET request: {getSpotInstanceRequest.SpotInstanceId.Apply(x => x)}");
            Console.WriteLine($"SpotInstanceId from GET request: {getSpotInstanceRequest.SpotInstanceId.Apply(x => x.ToString())}");
            Console.WriteLine($"SpotInstanceId from GET request: {getSpotInstanceRequest.SpotInstanceId.Apply(x => x).ToString()}");

            //If I could actually get the SpotInstanceId here, I would be able to invoke the AWS SDK here to add tags to that instance?
        }
    }
Ejemplo n.º 3
0
        public async Task <string> FunctionHandler(Dictionary <string, string> input, ILambdaContext context)
        {
            // More AMI IDs: aws.amazon.com/amazon-linux-2/release-notes/
            // us-east-2  HVM  EBS-Backed  64-bit  Amazon Linux 2
            string ami = "ami-09d9edae5eb90d556";
            string sg  = "default";
            // docs.aws.amazon.com/sdkfornet/v3/apidocs/items/EC2/TInstanceType.html
            InstanceType type  = InstanceType.T3aNano;
            string       price = "0.003";
            int          count = 1;
            var          requestSpotInstances = await RequestSpotInstance(ami, sg, type, price, count);

            var spotRequestId = requestSpotInstances.SpotInstanceRequests[0].SpotInstanceRequestId;

            Console.WriteLine(spotRequestId);

            string instanceId;

            while (true)
            {
                SpotInstanceRequest spotRequest = await GetSpotRequest(spotRequestId);

                Console.WriteLine(spotRequest.State);
                if (spotRequest.State == SpotInstanceState.Active)
                {
                    instanceId = spotRequest.InstanceId;
                    break;
                }
            }
            var cancelRequest    = CancelSpotRequest(spotRequestId);
            var terminateRequest = TerminateSpotInstance(instanceId);

            await Task.WhenAll(cancelRequest, terminateRequest);

            Console.WriteLine("Complete");
            return(spotRequestId);
        }
Ejemplo n.º 4
0
        /* Creates, cancels, and terminates a spot instance request
         * See Usage() for information about the command-line args
         */
        static void Main(string[] args)
        {
            // Values that aren't easy to pass on the command line
            RegionEndpoint region       = RegionEndpoint.USWest2;
            InstanceType   instanceType = InstanceType.T1Micro;

            // Default values for optional command-line args
            string securityGroupName = "default";
            string spotPrice         = "0.003";
            int    instanceCount     = 1;

            // Placeholder for the only required command-line arg
            string amiId = "";

            // Parse command-line args
            int i = 0;

            while (i < args.Length)
            {
                switch (args[i])
                {
                case "-s":
                    i++;
                    securityGroupName = args[i];
                    if (securityGroupName == "")
                    {
                        Console.WriteLine("The security group name cannot be blank");
                        Usage();
                        return;
                    }
                    break;

                case "-p":
                    i++;
                    spotPrice = args[i];
                    double price;
                    double.TryParse(spotPrice, out price);
                    if (price < 0.001)
                    {
                        Console.WriteLine("The spot price must be > 0.001");
                        Usage();
                        return;
                    }
                    break;

                case "-c":
                    i++;
                    int.TryParse(args[i], out instanceCount);
                    if (instanceCount < 1)
                    {
                        Console.WriteLine("The instance count must be > 0");
                        Usage();
                        return;
                    }
                    break;

                case "-h":
                    Usage();
                    return;

                default:
                    amiId = args[i];
                    break;
                }

                i++;
            }

            // Make sure we have an AMI
            if (amiId == "")
            {
                Console.WriteLine("You must supply an AMI");
                Usage();
                return;
            }

            AmazonEC2Client ec2Client = new AmazonEC2Client(region: region);

            Console.WriteLine("Creating spot instance request");

            SpotInstanceRequest req = RequestSpotInstance(ec2Client, amiId, securityGroupName, instanceType, spotPrice, instanceCount);

            string id = req.SpotInstanceRequestId;

            // Wait for it to become active
            Console.WriteLine("Waiting for spot instance request with ID " + id + " to become active");

            int wait      = 1;
            int totalTime = 0;

            while (true)
            {
                totalTime += wait;
                Console.Write(".");

                SpotInstanceState state = GetSpotRequestState(ec2Client, id);

                if (state == SpotInstanceState.Active)
                {
                    Console.WriteLine("");
                    break;
                }

                // wait a bit and try again
                Thread.Sleep(wait);

                // wait longer next time
                // 1, 2, 4, ...
                wait = wait * 2;
            }

            // Should be around 1000 (one second)
            Console.WriteLine("That took " + totalTime + " milliseconds");

            // Cancel the request
            Console.WriteLine("Canceling spot instance request");

            CancelSpotRequest(ec2Client, id);

            // Clean everything up
            Console.WriteLine("Terminating spot instance request");

            TerminateSpotInstance(ec2Client, id);

            Console.WriteLine("Done. Press enter to quit");

            Console.ReadLine();
        }