Resource: aws_spot_fleet_request

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.

Example Usage

Using launch specifications

# 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"
    }
  }
}

Using launch templates

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:

Using multiple launch specifications

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"
      }
    }
  }
}

Using multiple launch configurations

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]
}

Argument Reference

Most of these arguments directly correspond to the official API.

Launch Template Configs

The launch_template_config block supports the following:

Launch Template Specification

spot_maintenance_strategies

capacity_rebalance

Overrides

Instance Requirements

This configuration block supports the following:

    Valid names:
      * amazon-web-services
      * amd
      * nvidia
      * xilinx
    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
    Valid types:
      * fpga
      * gpu
      * inference
    Valid names:
      * amazon-web-services
      * amd
      * intel
    Valid names:
      * current  - Recommended for best performance.
      * previous - For existing applications optimized for older instance types.
    Value names:
      * hdd - hard disk drive
      * ssd - solid state drive

Attribute Reference

This resource exports the following attributes in addition to the arguments above:

Timeouts

Configuration options:

Import

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