/// <summary> /// Creates a Spot Instance request. Spot Instances are instances that Amazon EC2 launches /// when the bid price that you specify exceeds the current Spot Price. Amazon EC2 periodically /// sets the Spot Price based on available Spot Instance capacity and current Spot Instance /// requests. For more information, see <a href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-requests.html">Spot /// Instance Requests</a> in the <i>Amazon Elastic Compute Cloud User Guide</i>. /// </summary> /// <param name="request">Container for the necessary parameters to execute the RequestSpotInstances service method.</param> /// /// <returns>The response from the RequestSpotInstances service method, as returned by EC2.</returns> public RequestSpotInstancesResponse RequestSpotInstances(RequestSpotInstancesRequest request) { var marshaller = new RequestSpotInstancesRequestMarshaller(); var unmarshaller = RequestSpotInstancesResponseUnmarshaller.Instance; return Invoke<RequestSpotInstancesRequest,RequestSpotInstancesResponse>(request, marshaller, unmarshaller); }
/// <summary> /// Initiates the asynchronous execution of the RequestSpotInstances operation. /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the RequestSpotInstances operation.</param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// <returns>The task object representing the asynchronous operation.</returns> public Task<RequestSpotInstancesResponse> RequestSpotInstancesAsync(RequestSpotInstancesRequest request, System.Threading.CancellationToken cancellationToken = default(CancellationToken)) { var marshaller = new RequestSpotInstancesRequestMarshaller(); var unmarshaller = RequestSpotInstancesResponseUnmarshaller.Instance; return InvokeAsync<RequestSpotInstancesRequest,RequestSpotInstancesResponse>(request, marshaller, unmarshaller, cancellationToken); }
IAsyncResult invokeRequestSpotInstances(RequestSpotInstancesRequest requestSpotInstancesRequest, AsyncCallback callback, object state, bool synchronized) { IRequest irequest = new RequestSpotInstancesRequestMarshaller().Marshall(requestSpotInstancesRequest); var unmarshaller = RequestSpotInstancesResponseUnmarshaller.GetInstance(); AsyncResult result = new AsyncResult(irequest, callback, state, synchronized, signer, unmarshaller); Invoke(result); return result; }
/// <summary> /// Initiates the asynchronous execution of the RequestSpotInstances operation. /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the RequestSpotInstances operation on AmazonEC2Client.</param> /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param> /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> /// /// <returns>An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndRequestSpotInstances /// operation.</returns> public IAsyncResult BeginRequestSpotInstances(RequestSpotInstancesRequest request, AsyncCallback callback, object state) { var marshaller = new RequestSpotInstancesRequestMarshaller(); var unmarshaller = RequestSpotInstancesResponseUnmarshaller.Instance; return BeginInvoke<RequestSpotInstancesRequest>(request, marshaller, unmarshaller, callback, state); }
/// <summary> /// <para>Creates a Spot Instance request. Spot Instances are instances that Amazon EC2 starts on your behalf when the maximum price that you /// specify exceeds the current Spot Price. Amazon EC2 periodically sets the Spot Price based on available Spot Instance capacity and current /// Spot Instance requests. For more information about Spot Instances, see <a /// href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-spot-instances.html" >Spot Instances</a> in the <i>Amazon Elastic Compute /// Cloud User Guide</i> .</para> /// </summary> /// /// <param name="requestSpotInstancesRequest">Container for the necessary parameters to execute the RequestSpotInstances service method on /// AmazonEC2.</param> /// /// <returns>The response from the RequestSpotInstances service method, as returned by AmazonEC2.</returns> /// public RequestSpotInstancesResponse RequestSpotInstances(RequestSpotInstancesRequest requestSpotInstancesRequest) { IAsyncResult asyncResult = invokeRequestSpotInstances(requestSpotInstancesRequest, null, null, true); return EndRequestSpotInstances(asyncResult); }
/// <summary> /// Initiates the asynchronous execution of the RequestSpotInstances operation. /// <seealso cref="Amazon.EC2.IAmazonEC2.RequestSpotInstances"/> /// </summary> /// /// <param name="requestSpotInstancesRequest">Container for the necessary parameters to execute the RequestSpotInstances operation on /// AmazonEC2.</param> /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param> /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> /// /// <returns>An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking /// EndRequestSpotInstances operation.</returns> public IAsyncResult BeginRequestSpotInstances(RequestSpotInstancesRequest requestSpotInstancesRequest, AsyncCallback callback, object state) { return invokeRequestSpotInstances(requestSpotInstancesRequest, callback, state, false); }
internal RequestSpotInstancesResponse RequestSpotInstances(RequestSpotInstancesRequest request) { var task = RequestSpotInstancesAsync(request); try { return task.Result; } catch(AggregateException e) { ExceptionDispatchInfo.Capture(e.InnerException).Throw(); return null; } }
/// <summary> /// <para>Creates a Spot Instance request. Spot Instances are instances that Amazon EC2 starts on your behalf when the maximum price that you /// specify exceeds the current Spot Price. Amazon EC2 periodically sets the Spot Price based on available Spot Instance capacity and current /// Spot Instance requests. For more information about Spot Instances, see <a href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-spot-instances.html">Spot Instances</a> in the <i>Amazon Elastic Compute /// Cloud User Guide</i> .</para> /// </summary> /// /// <param name="requestSpotInstancesRequest">Container for the necessary parameters to execute the RequestSpotInstances service method on /// AmazonEC2.</param> /// /// <returns>The response from the RequestSpotInstances service method, as returned by AmazonEC2.</returns> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> public Task<RequestSpotInstancesResponse> RequestSpotInstancesAsync(RequestSpotInstancesRequest requestSpotInstancesRequest, CancellationToken cancellationToken = default(CancellationToken)) { var marshaller = new RequestSpotInstancesRequestMarshaller(); var unmarshaller = RequestSpotInstancesResponseUnmarshaller.GetInstance(); return Invoke<IRequest, RequestSpotInstancesRequest, RequestSpotInstancesResponse>(requestSpotInstancesRequest, marshaller, unmarshaller, signer, cancellationToken); }
/// <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; }
/// <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; }
/// <summary> /// Initiates the asynchronous execution of the RequestSpotInstances operation. /// <seealso cref="Amazon.EC2.IAmazonEC2.RequestSpotInstances"/> /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the RequestSpotInstances operation.</param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// <returns>The task object representing the asynchronous operation.</returns> public async Task<RequestSpotInstancesResponse> RequestSpotInstancesAsync(RequestSpotInstancesRequest request, CancellationToken cancellationToken = default(CancellationToken)) { var marshaller = new RequestSpotInstancesRequestMarshaller(); var unmarshaller = RequestSpotInstancesResponseUnmarshaller.GetInstance(); var response = await Invoke<IRequest, RequestSpotInstancesRequest, RequestSpotInstancesResponse>(request, marshaller, unmarshaller, signer, cancellationToken) .ConfigureAwait(continueOnCapturedContext: false); return response; }
/// <summary> /// <para> Creates a Spot Instance request. </para> <para> Spot Instances are instances that Amazon EC2 starts on your behalf when the maximum /// price that you specify exceeds the current Spot Price. Amazon EC2 periodically sets the Spot Price based on available Spot Instance capacity /// and current spot instance requests. </para> <para> For conceptual information about Spot Instances, refer to the Amazon Elastic Compute /// Cloud Developer Guide or Amazon Elastic Compute Cloud User Guide. </para> /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the RequestSpotInstances service method on /// AmazonEC2.</param> /// /// <returns>The response from the RequestSpotInstances service method, as returned by AmazonEC2.</returns> public RequestSpotInstancesResponse RequestSpotInstances(RequestSpotInstancesRequest request) { var task = RequestSpotInstancesAsync(request); try { return task.Result; } catch(AggregateException e) { throw e.InnerException; } }
/// <summary> /// Create a new instance using the Amazon spot market. /// </summary> protected override void AmazonExecute() { try { // Set the Amazon instance request times //// var localValidFrom = DateTime.Now.ToAmazonDateTime(); //// var localValidUntil = DateTime.Now.AddMinutes(this.ValidDurationMinutes.Get(this.ActivityContext)).ToAmazonDateTime(); var request = new RequestSpotInstancesRequest { //// AvailabilityZoneGroup = , InstanceCount = this.InstanceCount.Get(this.ActivityContext), //// LaunchGroup = , LaunchSpecification = new LaunchSpecification() { ImageId = this.ImageId.Get(this.ActivityContext), InstanceType = this.InstanceType.Get(this.ActivityContext), SecurityGroup = new List<string>() { this.SecurityGroupName.Get(this.ActivityContext) } }, SpotPrice = this.SpotPrice.Get(this.ActivityContext).ToString(), Type = this.RequestType.Get(this.ActivityContext), //// ValidFrom = localValidFrom, //// ValidUntil = localValidUntil }; try { var response = EC2Client.RequestSpotInstances(request); this.SpotRequests.Set(this.ActivityContext, response.RequestSpotInstancesResult.SpotInstanceRequest); } catch (EndpointNotFoundException ex) { this.LogBuildMessage(ex.Message); } } catch (Exception ex) { this.LogBuildError("Amazon error: " + ex.Message + " Stack Trace: " + ex.StackTrace); if (ex.InnerException != null) { this.LogBuildError("Inner exception: " + ex.InnerException.Message); } } }