Describes Spot instance placement.
Example #1
0
        /// <summary>
        /// This function creates a spot instance request inside a VPC. It returns the request ID if successful, or sets the error
        /// code and message otherwise
        /// </summary>
        /// <param name="AvailabilityZone">Name of the Availability Zone where the instances will be launched</param>
        /// <param name="AMI_ID">Id of the AMI that will be used as a base for the instances</param>
        /// <param name="SecurityGroupId">The name of the security group to be assigned to the instance(s)</param>
        /// <param name="KeyPairName">The name of the keypair to be assigned to the instance(s)</param>
        /// <param name="InstanceType">The type of the instance(s)</param>
        /// <param name="InstancePrice">The max price to pay for the instance(s)</param>
        /// <param name="InstanceCount">The number of instances to be launched</param>
        /// <param name="UserData">The user-data script that will be run as the instance(s) is(are) initialized</param>
        /// <returns>The list of Request Ids if successful</returns>
        public List<string> RequestClassicSpotInstances(string AvailabilityZone, string AMI_ID, string SecurityGroupId, string KeyPairName, string InstanceType, double InstancePrice, int InstanceCount = 1, string UserData = "")
        {
            List<string> RequestIds = new List<string> ();

            // Initialize error values
            ErrorCode    = 0;
            ErrorMessage = "";

            // Create the list with security groups
            List<string> SecurityGroups = new List<string> () { SecurityGroupId };

            // Create placement object
            SpotPlacement spotPlacement = new SpotPlacement ()
            {
                AvailabilityZone = AvailabilityZone
            };

            // Create the launch specification
            LaunchSpecification launchSpecification = new LaunchSpecification ()
            {
                ImageId           = AMI_ID,
                InstanceType      = InstanceType,
                KeyName           = KeyPairName,
                SecurityGroups    = SecurityGroups,
                Placement         = spotPlacement,
                UserData          = Gadgets.Base64Encode (UserData)
            };

            // Create the request object
            RequestSpotInstancesRequest spotRequest = new RequestSpotInstancesRequest ()
            {
                SpotPrice           = InstancePrice.ToString (),
                InstanceCount       = InstanceCount,
                LaunchSpecification = launchSpecification
            };

            // Request the instances
            try
            {
                var spotResponse = EC2client.RequestSpotInstances (spotRequest);

                // Check response for errors
                if (spotResponse.HttpStatusCode != HttpStatusCode.OK)
                {
                    ErrorCode    = Convert.ToInt32 (spotResponse.HttpStatusCode);
                    ErrorMessage = "Http Error [" + spotResponse.HttpStatusCode.ToString () + "]";
                }
                else
                {
                    foreach (SpotInstanceRequest request in spotResponse.SpotInstanceRequests)
                    {
                        RequestIds.Add (request.SpotInstanceRequestId);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorCode    = -1;
                ErrorMessage = ex.Message + "::" + ex.InnerException;
            }

            return RequestIds;
        }