Skip to content

Resource: awsSpotInstanceRequest

Provides an EC2 Spot Instance Request resource. This allows instances to be requested on the spot market.

By default Terraform creates Spot Instance Requests with a persistent type, which means that for the duration of their lifetime, AWS will launch an instance with the configured details if and when the spot market will accept the requested price.

On destruction, Terraform will make an attempt to terminate the associated Spot Instance if there is one present.

Spot Instances requests with a oneTime type will close the spot request when the instance is terminated either by the request being below the current spot price availability or by a user.

\~> NOTE: Because their behavior depends on the live status of the spot market, Spot Instance Requests have a unique lifecycle that makes them behave differently than other Terraform resources. Most importantly: there is no guarantee that a Spot Instance exists to fulfill the request at any given point in time. See the AWS Spot Instance documentation for more information.

Example Usage

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
new aws.spotInstanceRequest.SpotInstanceRequest(this, "cheap_worker", {
  ami: "ami-1234",
  instanceType: "c4.xlarge",
  spotPrice: "0.03",
  tags: {
    Name: "CheapWorker",
  },
});

Argument Reference

Spot Instance Requests support all the same arguments as awsInstance, with the addition of:

  • spotPrice - (Optional; Default: On-demand price) The maximum price to request on the spot market.
  • waitForFulfillment - (Optional; Default: false) If set, Terraform will wait for the Spot Request to be fulfilled, and will throw an error if the timeout of 10m is reached.
  • spotType - (Optional; Default: persistent) If set to oneTime, after the instance is terminated, the spot request will be closed.
  • launchGroup - (Optional) A launch group is a group of spot instances that launch together and terminate together. If left empty instances are launched and terminated individually.
  • blockDurationMinutes - (Optional) The required duration for the Spot instances, in minutes. This value must be a multiple of 60 (60, 120, 180, 240, 300, or 360). The duration period starts as soon as your Spot instance receives its instance ID. At the end of the duration period, Amazon EC2 marks the Spot instance for termination and provides a Spot instance termination notice, which gives the instance a two-minute warning before it terminates. Note that you can't specify an Availability Zone group or a launch group if you specify a duration.
  • instanceInterruptionBehavior - (Optional) Indicates Spot instance behavior when it is interrupted. Valid values are terminate, stop, or hibernate. Default value is terminate.
  • validUntil - (Optional) The end date and time of the request, in UTC RFC3339 format(for example, YYYY-MM-DDTHH:MM:SSZ). At this point, no new Spot instance requests are placed or enabled to fulfill the request. The default end date is 7 days from the current date.
  • validFrom - (Optional) The start date and time of the request, in UTC RFC3339 format(for example, YYYY-MM-DDTHH:MM:SSZ). The default is to start fulfilling the request immediately.
  • tags - (Optional) A map of tags to assign to the Spot Instance Request. These tags are not automatically applied to the launched Instance. If configured with a provider defaultTags configuration block present, tags with matching keys will overwrite those defined at the provider-level.

Attributes Reference

In addition to all arguments above, the following attributes are exported:

  • id - The Spot Instance Request ID.

These attributes are exported, but they are expected to change over time and so should only be used for informational purposes, not for resource dependencies:

  • spotBidStatus - The current bid status of the Spot Instance Request.
  • spotRequestState The current request state of the Spot Instance Request.
  • spotInstanceId - The Instance ID (if any) that is currently fulfilling the Spot Instance request.
  • publicDns - The public DNS name assigned to the instance. For EC2-VPC, this is only available if you've enabled DNS hostnames for your VPC
  • publicIp - The public IP address assigned to the instance, if applicable.
  • privateDns - The private DNS name assigned to the instance. Can only be used inside the Amazon EC2, and only available if you've enabled DNS hostnames for your VPC
  • privateIp - The private IP address assigned to the instance
  • tagsAll - A map of tags assigned to the resource, including those inherited from the provider defaultTags configuration block.

Timeouts

Configuration options:

  • create - (Default 10M)
  • delete - (Default 20M)