Provides an EC2 Spot Fleet Request resource. This allows a fleet of Spot instances to be requested on the Spot market.
.. warning:: NOTE AWS strongly discourages the use of the legacy APIs called by this resource. We recommend using the EC2 Fleet or Auto Scaling Group resources instead.
# Request a Spot fleet
resource "aws_spot_fleet_request" "cheap_compute" {
iam_fleet_role = "arn:aws:iam::12345678:role/spot-fleet"
spot_price = "0.03"
allocation_strategy = "diversified"
target_capacity = 6
valid_until = "2019-11-04T20:44:20Z"
launch_specification {
instance_type = "m4.10xlarge"
ami = "ami-1234"
spot_price = "2.793"
placement_tenancy = "dedicated"
iam_instance_profile_arn = aws_iam_instance_profile.example.arn
}
launch_specification {
instance_type = "m4.4xlarge"
ami = "ami-5678"
key_name = "my-key"
spot_price = "1.117"
iam_instance_profile_arn = aws_iam_instance_profile.example.arn
availability_zone = "us-west-1a"
subnet_id = "subnet-1234"
weighted_capacity = 35
root_block_device {
volume_size = "300"
volume_type = "gp2"
}
tags = {
Name = "spot-fleet-example"
}
}
}
resource "aws_launch_template" "foo" {
name = "launch-template"
image_id = "ami-516b9131"
instance_type = "m1.small"
key_name = "some-key"
}
resource "aws_spot_fleet_request" "foo" {
iam_fleet_role = "arn:aws:iam::12345678:role/spot-fleet"
spot_price = "0.005"
target_capacity = 2
valid_until = "2019-11-04T20:44:20Z"
launch_template_config {
launch_template_specification {
id = aws_launch_template.foo.id
version = aws_launch_template.foo.latest_version
}
}
depends_on = [aws_iam_policy_attachment.test-attach]
}
~>
NOTE:** Terraform does not support the functionality where multiple subnet_id
or availability_zone
parameters can be specified in the same launch configuration block. If you want to specify multiple values, then separate launch configuration blocks should be used or launch template overrides should be configured, one per subnet:
resource "aws_spot_fleet_request" "foo" {
iam_fleet_role = "arn:aws:iam::12345678:role/spot-fleet"
spot_price = "0.005"
target_capacity = 2
valid_until = "2019-11-04T20:44:20Z"
launch_specification {
instance_type = "m1.small"
ami = "ami-d06a90b0"
key_name = "my-key"
availability_zone = "us-west-2a"
}
launch_specification {
instance_type = "m5.large"
ami = "ami-d06a90b0"
key_name = "my-key"
availability_zone = "us-west-2a"
}
}
variable "subnets" {}
resource "aws_spot_fleet_request" "example" {
iam_fleet_role = "arn:aws:iam::12345678:role/spot-fleet"
target_capacity = 3
valid_until = "2019-11-04T20:44:20Z"
allocation_strategy = "lowestPrice"
fleet_type = "request"
wait_for_fulfillment = "true"
terminate_instances_with_expiration = "true"
dynamic "launch_specification" {
for_each = [for s in var.subnets : {
subnet_id = s[1]
}]
content {
ami = "ami-1234"
instance_type = "m4.4xlarge"
subnet_id = launch_specification.value.subnet_id
vpc_security_group_ids = "sg-123456"
root_block_device {
volume_size = "8"
volume_type = "gp2"
delete_on_termination = "true"
}
tags = {
Name = "Spot Node"
tag_builder = "builder"
}
}
}
}
data "aws_subnets" "example" {
filter {
name = "vpc-id"
values = [var.vpc_id]
}
}
resource "aws_launch_template" "foo" {
name = "launch-template"
image_id = "ami-516b9131"
instance_type = "m1.small"
key_name = "some-key"
}
resource "aws_spot_fleet_request" "foo" {
iam_fleet_role = "arn:aws:iam::12345678:role/spot-fleet"
spot_price = "0.005"
target_capacity = 2
valid_until = "2019-11-04T20:44:20Z"
launch_template_config {
launch_template_specification {
id = aws_launch_template.foo.id
version = aws_launch_template.foo.latest_version
}
overrides {
subnet_id = data.aws_subnets.example.ids[0]
}
overrides {
subnet_id = data.aws_subnets.example.ids[1]
}
overrides {
subnet_id = data.aws_subnets.example.ids[2]
}
}
depends_on = [aws_iam_policy_attachment.test-attach]
}
Most of these arguments directly correspond to the official API.
iam_fleet_role
- (Required) Grants the Spot fleet permission to terminate
Spot instances on your behalf when you cancel its Spot fleet request using
CancelSpotFleetRequests or when the Spot fleet request expires, if you set
terminateInstancesWithExpiration.context
- (Optional) Reserved.replace_unhealthy_instances
- (Optional) Indicates whether Spot fleet should replace unhealthy instances. Default false
.launch_specification
- (Optional) Used to define the launch configuration of the
spot-fleet request. Can be specified multiple times to define different bids
across different markets and instance types. Conflicts with launch_template_config
. At least one of launch_specification
or launch_template_config
is required.
Note: This takes in similar but not
identical inputs as aws_instance
. There are limitations on
what you can specify. See the list of officially supported inputs in the
reference documentation. Any normal aws_instance
parameter that corresponds to those inputs may be used and it have
a additional parameter iam_instance_profile_arn
takes aws_iam_instance_profile
attribute arn
as input.
launch_template_config
- (Optional) Launch template configuration block. See Launch Template Configs below for more details. Conflicts with launch_specification
. At least one of launch_specification
or launch_template_config
is required.
spot_maintenance_strategies
- (Optional) Nested argument containing maintenance strategies for managing your Spot Instances that are at an elevated risk of being interrupted. Defined below.spot_price
- (Optional; Default: On-demand price) The maximum bid price per unit hour.wait_for_fulfillment
- (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.target_capacity
- The number of units to request. You can choose to set the
target capacity in terms of instances or a performance characteristic that is
important to your application workload, such as vCPUs, memory, or I/O.target_capacity_unit_type
- (Optional) The unit for the target capacity. This can only be done with instance_requirements
definedallocation_strategy
- Indicates how to allocate the target capacity across
the Spot pools specified by the Spot fleet request. Valid values: lowestPrice
, diversified
, capacityOptimized
, capacityOptimizedPrioritized
, and priceCapacityOptimized
. The default is
lowestPrice
.instance_pools_to_use_count
- (Optional; Default: 1)
The number of Spot pools across which to allocate your target Spot capacity.
Valid only when allocation_strategy
is set to lowestPrice
. Spot Fleet selects
the cheapest Spot pools and evenly allocates your target Spot capacity across
the number of Spot pools that you specify.excess_capacity_termination_policy
- Indicates whether running Spot
instances should be terminated if the target capacity of the Spot fleet
request is decreased below the current size of the Spot fleet.terminate_instances_with_expiration
- (Optional) Indicates whether running Spot
instances should be terminated when the Spot fleet request expires.terminate_instances_on_delete
- (Optional) Indicates whether running Spot
instances should be terminated when the resource is deleted (and the Spot fleet request cancelled).
If no value is specified, the value of the terminate_instances_with_expiration
argument is used.instance_interruption_behaviour
- (Optional) Indicates whether a Spot
instance stops or terminates when it is interrupted. Default is
terminate
.fleet_type
- (Optional) The type of fleet request. Indicates whether the Spot Fleet only requests the target
capacity or also attempts to maintain it. Default is maintain
.valid_until
- (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.valid_from
- (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.load_balancers
(Optional) A list of elastic load balancer names to add to the Spot fleet.target_group_arns
(Optional) A list of aws_alb_target_group
ARNs, for use with Application Load Balancing.on_demand_allocation_strategy
- The order of the launch template overrides to use in fulfilling On-Demand capacity. the possible values are: lowestPrice
and prioritized
. the default is lowestPrice
.on_demand_max_total_price
- The maximum amount per hour for On-Demand Instances that you're willing to pay. When the maximum amount you're willing to pay is reached, the fleet stops launching instances even if it hasn’t met the target capacity.on_demand_target_capacity
- The number of On-Demand units to request. If the request type is maintain
, you can specify a target capacity of 0 and add capacity later.tags
- (Optional) A map of tags to assign to the resource. If configured with a provider default_tags
configuration block present, tags with matching keys will overwrite those defined at the provider-level.The launch_template_config
block supports the following:
launch_template_specification
- (Required) Launch template specification. See Launch Template Specification below for more details.overrides
- (Optional) One or more override configurations. See Overrides below for more details.id
- The ID of the launch template. Conflicts with name
.name
- The name of the launch template. Conflicts with id
.version
- (Optional) Template version. Unlike the autoscaling equivalent, does not support $Latest
or $Default
, so use the launch_template resource's attribute, e.g., "${aws_launch_template.foo.latest_version}"
. It will use the default version if omitted.
Note: The specified launch template can specify only a subset of the
inputs of aws_launch_template
. There are limitations on
what you can specify as spot fleet does not support all the attributes that are supported by autoscaling groups. AWS documentation is currently sparse, but at least instance_initiated_shutdown_behavior
is confirmed unsupported.
capacity_rebalance
- (Optional) Nested argument containing the capacity rebalance for your fleet request. Defined below.replacement_strategy
- (Optional) The replacement strategy to use. Only available for spot fleets with fleet_type
set to maintain
. Valid values: launch
.availability_zone
- (Optional) The availability zone in which to place the request.instance_requirements
- (Optional) The instance requirements. See below.instance_type
- (Optional) The type of instance to request.priority
- (Optional) The priority for the launch template override. The lower the number, the higher the priority. If no number is set, the launch template override has the lowest priority.spot_price
- (Optional) The maximum spot bid for this override request.subnet_id
- (Optional) The subnet in which to launch the requested instance.weighted_capacity
- (Optional) The capacity added to the fleet by a fulfilled request.This configuration block supports the following:
accelerator_count
- (Optional) Block describing the minimum and maximum number of accelerators (GPUs, FPGAs, or AWS Inferentia chips). Default is no minimum or maximum.
min
- (Optional) Minimum.max
- (Optional) Maximum. Set to 0
to exclude instance types with accelerators.accelerator_manufacturers
- (Optional) List of accelerator manufacturer names. Default is any manufacturer. Valid names:
* amazon-web-services
* amd
* nvidia
* xilinx
accelerator_names
- (Optional) List of accelerator names. Default is any acclerator. Valid names:
* a100 - NVIDIA A100 GPUs
* v100 - NVIDIA V100 GPUs
* k80 - NVIDIA K80 GPUs
* t4 - NVIDIA T4 GPUs
* m60 - NVIDIA M60 GPUs
* radeon-pro-v520 - AMD Radeon Pro V520 GPUs
* vu9p - Xilinx VU9P FPGAs
accelerator_total_memory_mib
- (Optional) Block describing the minimum and maximum total memory of the accelerators. Default is no minimum or maximum.
min
- (Optional) Minimum.max
- (Optional) Maximum.accelerator_types
- (Optional) List of accelerator types. Default is any accelerator type. Valid types:
* fpga
* gpu
* inference
allowed_instance_types
- (Optional) List of instance types to apply your specified attributes against. All other instance types are ignored, even if they match your specified attributes. You can use strings with one or more wild cards, represented by an asterisk (*), to allow an instance type, size, or generation. The following are examples: m5.8xlarge
, c5*.*
, m5a.*
, r*
, *3*
. For example, if you specify c5*
, you are allowing the entire C5 instance family, which includes all C5a and C5n instance types. If you specify m5a.*
, you are allowing all the M5a instance types, but not the M5n instance types. Maximum of 400 entries in the list; each entry is limited to 30 characters. Default is all instance types.
~> NOTE: If you specify allowed_instance_types
, you can't specify excluded_instance_types
.
bare_metal
- (Optional) Indicate whether bare metal instace types should be included
, excluded
, or required
. Default is excluded
.
baseline_ebs_bandwidth_mbps
- (Optional) Block describing the minimum and maximum baseline EBS bandwidth, in Mbps. Default is no minimum or maximum.
min
- (Optional) Minimum.max
- (Optional) Maximum.burstable_performance
- (Optional) Indicate whether burstable performance instance types should be included
, excluded
, or required
. Default is excluded
.cpu_manufacturers
(Optional) List of CPU manufacturer names. Default is any manufacturer.
~> NOTE: Don't confuse the CPU hardware manufacturer with the CPU hardware architecture. Instances will be launched with a compatible CPU architecture based on the Amazon Machine Image (AMI) that you specify in your launch template.
Valid names:
* amazon-web-services
* amd
* intel
excluded_instance_types
- (Optional) List of instance types to exclude. You can use strings with one or more wild cards, represented by an asterisk (*), to exclude an instance type, size, or generation. The following are examples: m5.8xlarge
, c5*.*
, m5a.*
, r*
, *3*
. For example, if you specify c5*
, you are excluding the entire C5 instance family, which includes all C5a and C5n instance types. If you specify m5a.*
, you are excluding all the M5a instance types, but not the M5n instance types. Maximum of 400 entries in the list; each entry is limited to 30 characters. Default is no excluded instance types.
~> NOTE: If you specify excluded_instance_types
, you can't specify allowed_instance_types
.
instance_generations
- (Optional) List of instance generation names. Default is any generation.
Valid names:
* current - Recommended for best performance.
* previous - For existing applications optimized for older instance types.
local_storage
- (Optional) Indicate whether instance types with local storage volumes are included
, excluded
, or required
. Default is included
.local_storage_types
- (Optional) List of local storage type names. Default any storage type. Value names:
* hdd - hard disk drive
* ssd - solid state drive
memory_gib_per_vcpu
- (Optional) Block describing the minimum and maximum amount of memory (GiB) per vCPU. Default is no minimum or maximum.
min
- (Optional) Minimum. May be a decimal number, e.g. 0.5
.max
- (Optional) Maximum. May be a decimal number, e.g. 0.5
.memory_mib
- (Optional) Block describing the minimum and maximum amount of memory (MiB). Default is no maximum.
min
- (Optional) Minimum.max
- (Optional) Maximum.network_bandwidth_gbps
- (Optional) Block describing the minimum and maximum amount of network bandwidth, in gigabits per second (Gbps). Default is no minimum or maximum.
min
- (Optional) Minimum.max
- (Optional) Maximum.network_interface_count
- (Optional) Block describing the minimum and maximum number of network interfaces. Default is no minimum or maximum.
min
- (Optional) Minimum.max
- (Optional) Maximum.on_demand_max_price_percentage_over_lowest_price
- (Optional) The price protection threshold for On-Demand Instances. This is the maximum you’ll pay for an On-Demand Instance, expressed as a percentage higher than the cheapest M, C, or R instance type with your specified attributes. When Amazon EC2 Auto Scaling selects instance types with your attributes, we will exclude instance types whose price is higher than your threshold. The parameter accepts an integer, which Amazon EC2 Auto Scaling interprets as a percentage. To turn off price protection, specify a high value, such as 999999. Default is 20.
If you set DesiredCapacityType to vcpu or memory-mib, the price protection threshold is applied based on the per vCPU or per memory price instead of the per instance price.
require_hibernate_support
- (Optional) Indicate whether instance types must support On-Demand Instance Hibernation, either true
or false
. Default is false
.spot_max_price_percentage_over_lowest_price
- (Optional) The price protection threshold for Spot Instances. This is the maximum you’ll pay for a Spot Instance, expressed as a percentage higher than the cheapest M, C, or R instance type with your specified attributes. When Amazon EC2 Auto Scaling selects instance types with your attributes, we will exclude instance types whose price is higher than your threshold. The parameter accepts an integer, which Amazon EC2 Auto Scaling interprets as a percentage. To turn off price protection, specify a high value, such as 999999. Default is 100.
If you set DesiredCapacityType to vcpu or memory-mib, the price protection threshold is applied based on the per vCPU or per memory price instead of the per instance price.
total_local_storage_gb
- (Optional) Block describing the minimum and maximum total local storage (GB). Default is no minimum or maximum.
min
- (Optional) Minimum. May be a decimal number, e.g. 0.5
.max
- (Optional) Maximum. May be a decimal number, e.g. 0.5
.vcpu_count
- (Optional) Block describing the minimum and maximum number of vCPUs. Default is no maximum.
min
- (Optional) Minimum.max
- (Optional) Maximum.This resource exports the following attributes in addition to the arguments above:
id
- The Spot fleet request IDspot_request_state
- The state of the Spot fleet request.tags_all
- A map of tags assigned to the resource, including those inherited from the provider default_tags
configuration block.create
- (Default 10m
)delete
- (Default 15m
)In Terraform v1.5.0 and later, use an import
block to import Spot Fleet Requests using id
. For example:
import {
to = aws_spot_fleet_request.fleet
id = "sfr-005e9ec8-5546-4c31-b317-31a62325411e"
}
Using terraform import
, import Spot Fleet Requests using id
. For example:
% terraform import aws_spot_fleet_request.fleet sfr-005e9ec8-5546-4c31-b317-31a62325411e