private static void PopulateLaunchSpecificationSecurityGroupNames(LaunchSpecification launchSpecification)
 {
     if (launchSpecification != null)
     {
         var groupNames = new List<string>();
         foreach (GroupIdentifier group in launchSpecification.AllSecurityGroups)
         {
             groupNames.Add(group.GroupName);
         }
         launchSpecification.SecurityGroups = groupNames;
     }
 }
 /// <summary>
 /// Sets the additional launch instance information.
 /// </summary>
 /// <param name="launchSpecification">Specifies additional launch instance
 /// information.</param>
 /// <returns>this instance</returns>
 public SpotInstanceRequest WithLaunchSpecification(LaunchSpecification launchSpecification)
 {
     this.launchSpecificationField = launchSpecification;
     return this;
 }
Example #3
0
 /// <summary>
 /// Sets the LaunchSpecification property
 /// </summary>
 /// <param name="launchSpecification">Specifies additional launch instance information.</param>
 /// <returns>this instance</returns>
 public RequestSpotInstancesRequest WithLaunchSpecification(LaunchSpecification launchSpecification)
 {
     this.launchSpecificationField = launchSpecification;
     return(this);
 }
Example #4
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="SubnetId">Id of the VPC subnet 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> RequestVPCSpotInstances(string SubnetId, 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 the network interface object (to connect with the VPC)
            var NetworkInterface = new InstanceNetworkInterfaceSpecification ()
            {
                DeviceIndex              = 0,
                SubnetId                 = SubnetId,
                Groups                   = SecurityGroups,
                AssociatePublicIpAddress = true
            };
            List<InstanceNetworkInterfaceSpecification> NetworkInterfaces = new List<InstanceNetworkInterfaceSpecification> () { NetworkInterface };

            // Create the launch specification
            LaunchSpecification launchSpecification = new LaunchSpecification ()
            {
                ImageId           = AMI_ID,
                InstanceType      = InstanceType,
                KeyName           = KeyPairName,
                SecurityGroups    = SecurityGroups,
                NetworkInterfaces = NetworkInterfaces,
                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;
        }
Example #5
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;
        }
Example #6
0
        private async Task BidForInstanceAsync(CancellationToken? cancellationToken = null)
        {
            CancellationToken token = cancellationToken.HasValue ? cancellationToken.Value : new CancellationToken();

            if (!this.Specification.SpotBidPrice.HasValue)
                throw new ArgumentNullException("specification.SpotBidPrice");

            this.Logger.Log("Bidding for new instance. Price: ${0}, AMI: {1}, size: {2}", this.Specification.SpotBidPrice.ToString(), this.Specification.Ami, this.Specification.Size.Name);
            var launchSpecification = new LaunchSpecification()
            {
                ImageId = this.Specification.Ami,
                InstanceType = this.Specification.Size.Key,
                KeyName = this.privateKeyPair.KeyName,
                SecurityGroups = new List<string>() { this.SecurityGroupName },
            };
            if (!string.IsNullOrWhiteSpace(this.Specification.AvailabilityZone))
            {
                launchSpecification.Placement = new SpotPlacement() { AvailabilityZone = this.Specification.AvailabilityZone };
            }

            var spotResponse = await this.Client.RequestSpotInstancesAsync(new RequestSpotInstancesRequest()
            {
                InstanceCount = 1,
                SpotPrice = this.Specification.SpotBidPrice.ToString(),
                LaunchSpecification = launchSpecification,
            });
            this.bidRequestId = spotResponse.SpotInstanceRequests[0].SpotInstanceRequestId;

            this.Logger.Log("Bid ID {0} created. Waiting for spot bid request to be fulfilled", this.bidRequestId);

            this.Logger.Log("This normally takes at least a few minutes");
            this.InstanceId = await this.UntilBidActiveAsync(this.bidRequestId, token);

            this.Logger.Log("New instance created. Instance ID: {0}", this.InstanceId);

            await this.SetupInstanceAsync(token);
        }