Menu
Amazon Elastic Compute Cloud
User Guide for Linux Instances

Spot Fleet Requests

To use a Spot fleet, you create a Spot fleet request that includes the target capacity, one or more launch specifications for the instances, and the bid price that you are willing to pay. Amazon EC2 attempts to maintain your Spot fleet's target capacity as Spot prices change. For more information, see How Spot Fleet Works.

Each launch specification includes the information that Amazon EC2 needs to launch an instance—such as an AMI, an instance type, a subnet or Availability Zone, and one or more security groups.

A Spot fleet request remains active until it expires or you cancel it. When you cancel a Spot fleet request, you must specify that canceling your Spot fleet request terminates the Spot instances in your Spot fleet.

Spot Fleet Request States

A Spot fleet request can be in one of the following states:

  • submitted—The Spot fleet request is being evaluated and Amazon EC2 is preparing to launch the target number of Spot instances.

  • active—The Spot fleet has been validated and Amazon EC2 is attempting to maintain the target number of running Spot instances. The request remains in this state until it is modified or canceled.

  • modifying—The Spot fleet request is being modified. The request remains in this state until the modification is fully processed or the Spot fleet is canceled.

  • cancelled_running—The Spot fleet is canceled and will not launch additional Spot instances, but its existing Spot instances continue to run until they are interrupted or terminated. The request remains in this state until all instances are interrupted or terminated.

  • cancelled_terminating—The Spot fleet is canceled and its Spot instances are terminating. The request remains in this state until all instances are terminated.

  • cancelled—The Spot fleet is canceled and has no running Spot instances. The Spot fleet request is deleted two days after its instances were terminated.

The following illustration represents the transitions between the request states. Note that if you exceed your Spot fleet limits, the request is canceled immediately.

Spot fleet request states

Spot Fleet Prerequisites

Before you begin, complete the following:

  1. Create an IAM role that grants the Spot fleet permission to bid on, launch, and terminate instances on your behalf. You specify this role, by Amazon Resource Name (ARN), when you create a Spot fleet request.

    1. Open the Identity and Access Management (IAM) console at https://console.aws.amazon.com/iam/.

    2. In the navigation pane, choose Roles, and then choose Create New Role.

    3. On the Set Role Name page, enter a name for the role and then choose Next Step.

    4. On the Select Role Type page, choose Select next to Amazon EC2 Spot Fleet Role.

    5. On the Attach Policy page, select the AmazonEC2SpotFleetRole policy, and then choose Next Step.

    6. On the Review page, choose Create Role.

  2. Confirm that the user account that you will use to create the Spot fleet request has the required Amazon EC2 permissions:

    1. Open the Identity and Access Management (IAM) console at https://console.aws.amazon.com/iam/.

    2. In the navigation pane, choose Policies, and then choose Create Policy.

    3. On the Create Policy page, choose Select next to Create Your Own Policy.

    4. On the Review Policy page, enter a policy name and copy the following text into the Policy Document section.

      {
          "Version": "2012-10-17",
          "Statement": [
              {
                  "Effect": "Allow",
                  "Action": [
                      "ec2:*"
                  ],
                  "Resource": "*"
              },
              {
                  "Effect": "Allow",
                  "Action": [
                    "iam:PassRole",
                    "iam:ListRoles",
                    "iam:ListInstanceProfiles"
                  ],
                  "Resource": "*"
              }
          ]
      }

      The ec2:* enables an IAM user to call all Amazon EC2 API actions. To limit the user to specific API actions, specify those actions instead.

      The iam:PassRole action enables the user to specify the Spot fleet role in a Spot fleet request. The iam:ListRoles action enables the user to enumerate existing roles. The iam:ListInstanceProfiles action enables the user to enumerate existing instance profiles. The Amazon EC2 console uses iam:ListRoles to populate the IAM role list and iam:ListInstanceProfiles to populate the IAM instance profile list. To enable the user to create roles or instance profiles using the console, you must add the following actions: iam:CreateRole, iam:CreateInstanceProfile, and iam:AddRoleToInstanceProfile.

    5. Choose Create Policy.

    6. In the navigation pane, choose Users, and then choose the user who will submit the Spot fleet request.

    7. On the Permissions tab, choose Attach Policy.

    8. On the Attach Policy page, select the policy you created above, and choose Attach Policy.

Planning a Spot Fleet Request

Before you create a Spot fleet request, review Spot Best Practices. Use these best practices when you plan your Spot fleet request so that you can provision the type of instances you want at the lowest possible price. We also recommend that you do the following:

  • Determine the instance types that meet your application requirements.

  • Determine the target capacity for your Spot fleet request. You can set target capacity in instances or in custom units. For more information, see Spot Fleet Instance Weighting.

  • Determine your bid price per instance hour. Bidding lower can further reduce costs, while bidding higher can reduce the probability of interruption.

  • Determine your bid price per unit, if you are using instance weighting. To calculate the bid price per unit, divide the bid price per instance hour by the number of units (or weight) that this instance represents. (If you are not using instance weighting, the default bid price per unit is the bid price per instance hour.)

  • Review the possible options for your Spot fleet request. For more information, see the request-spot-fleet command in the AWS Command Line Interface Reference. For additional examples, see Spot Fleet Example Configurations.

Creating a Spot Fleet Request

When you create a Spot fleet request, you must specify information about the Spot instances to launch, such as the instance type and the Spot price.

To create a Spot fleet request using the console

  1. Open the Spot console at https://console.aws.amazon.com/ec2spot.

  2. If you are new to Spot, you see a welcome page; click Get started. Otherwise, click Request Spot Instances.

  3. On the Find instance types page, do the following:

    1. For AMI, choose one of the basic Amazon Machine Images (AMI) provided by AWS, or choose Use custom AMI to specify your own AMI.

    2. For Capacity unit, choose the units for your fleet. You can choose instances or performance characteristics that are important to your application workload, such as vCPUs, memory, and storage.

    3. For Target capacity, enter the number of units to request.

    4. For Bid price, enter your bid price. You can specify your bid price per unit hour or as a percentage of the price for On-Demand instances. The Spot fleet launches the requested instances if your bid price exceeds the Spot price for the instances.

    5. Under Select instance types, select one or more instance types. If the instance type that you need is not displayed, click view more. Then, you can filter the list to instance types that have the minimum hardware specifications that you need (vCPUs, memory, and storage).

      By default, the bid price for each instance type is determined by the bid price for the Spot fleet request. You'll see a warning if your bid price is too low for a particular instance type. If needed, you can override the bid price for each instance type that you selected.

      The Weighted capacity column represents the capacity units that each instance type would contribute to your fleet. The default values in the table are based on the capacity unit that you selected. For example, if your capacity unit is instances, the default weight for each instance type is 1. If your capacity unit is vCPUs, the default weight for each instance type is its number of vCPUs. If you prefer particular instance types, you can change their weights. For more information, see Spot Fleet Instance Weighting.

    6. Click Next.

  4. On the Configure page, do the following:

    1. For IAM role, specify the IAM role that you created to grant the Spot fleet permission to bid on, launch, and terminate instances on your behalf.

    2. For Allocation strategy, choose the strategy that meets your needs. For more information, see Spot Fleet Allocation Strategy.

    3. For Network, choose a VPC from the list, or choose Create new VPC to go the Amazon VPC console and create one. When you have finished, return to the wizard and click Refresh to load your VPC in the list. If you select a nondefault VPC, select auto-assign at launch from Public IP to assign a public IP address to your instances.

    4. For Security groups, choose one or more security groups.

    5. (Optional) If you have a preference for particular Availability Zones for your instances, from Availability Zones, choose Select, and then choose the Availability Zones. If your VPC is a nondefault VPC, you must select one subnet for each Availability Zone. If your VPC is a default VPC, we select the default subnet for the Availability Zone.

    6. (Optional) If you need to connect to your instance, specify your key pair in Key pair.

    7. (Optional) By default, the Spot fleet request remains in effect until it is fulfilled or you cancel it. To create a request that is valid only during a specific time period, under Advanced, under Request timeframe, specify Valid from and Valid to.

    8. Specify the remaining options you need, and then choose Review.

  5. On the Review page, verify the configuration of your Spot fleet request. To make changes, click Previous. To download a copy of the launch configuration for use with the AWS CLI, click JSON config. To create the Spot fleet request, click Launch.

To create a Spot fleet request using the AWS CLI

Use the following request-spot-fleet command to create a Spot fleet request:

aws ec2 request-spot-fleet --spot-fleet-request-config file://config.json

For example configuration files, see Spot Fleet Example Configurations.

The following is example output:

{
    "SpotFleetRequestId": "sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE"
} 

Monitoring Your Spot Fleet

The Spot fleet launches Spot instances when the Spot price is below your bid. The Spot instances run until either the bid price is no longer higher than the Spot price, or you terminate them yourself.

To monitor your Spot fleet using the console

  1. Open the Spot console at https://console.aws.amazon.com/ec2spot/home/fleet.

  2. Select your Spot fleet request. The configuration details are available in the Description tab.

  3. To list the Spot instances for the Spot fleet, select the Instances tab.

  4. To view the history for the Spot fleet, select the History tab.

To monitor your Spot fleet using the AWS CLI

Use the following describe-spot-fleet-requests command to describe your Spot fleet requests:

aws ec2 describe-spot-fleet-requests

Use the following describe-spot-fleet-instances command to describe the Spot instances for the specified Spot fleet:

aws ec2 describe-spot-fleet-instances --spot-fleet-request-id sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE

Use the following describe-spot-fleet-request-history command to describe the history for the specified Spot fleet request:

aws ec2 describe-spot-fleet-request-history --spot-fleet-request-id sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE --start-time 2015-05-18T00:00:00Z

Modifying a Spot Fleet Request

You can modify an active Spot fleet request to complete the following tasks:

  • Increase the target capacity

  • Decrease the target capacity

When you increase the target capacity, the Spot fleet launches the additional Spot instances according to the allocation strategy for its Spot fleet request. If the allocation strategy is lowestPrice, the Spot fleet launches the instances from the lowest-priced Spot instance pool in the Spot fleet request. If the allocation strategy is diversified, the Spot fleet distributes the instances across the pools in the Spot fleet request.

When you decrease the target capacity, the Spot fleet cancels any open bids that exceed the new target capacity. You can request that the Spot fleet terminate Spot instances until the size of the fleet reaches the new target capacity. If the allocation strategy is lowestPrice, the Spot fleet terminates the instances with the highest price per unit. If the allocation strategy is diversified, the Spot fleet terminates instances across the pools. Alternatively, you can request that the Spot fleet keep the fleet at its current size, but not replace any Spot instances that are interrupted or that you terminate manually.

To modify a Spot fleet request using the console

  1. Open the Spot console at https://console.aws.amazon.com/ec2spot/home/fleet.

  2. Select your Spot fleet request.

  3. Click Actions, and then click Modify target capacity.

  4. In Modify target capacity, do the following:

    1. Enter the new target capacity.

    2. (Optional) If you are decreasing the target capacity but want to keep the fleet at its current size, deselect Terminate instances.

    3. Click Submit.

To modify a Spot fleet request using the AWS CLI

Use the following modify-spot-fleet-request command to update the target capacity of the specified Spot fleet request:

aws ec2 modify-spot-fleet-request --spot-fleet-request-id sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE --target-capacity 20

You can modify the previous command as follows to decrease the target capacity of the specified Spot fleet without terminating any Spot instances as a result:

aws ec2 modify-spot-fleet-request --spot-fleet-request-id sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE --target-capacity 10 --excess-capacity-termination-policy NoTermination

Canceling a Spot Fleet Request

When you are finished using your Spot fleet, you can cancel the Spot fleet request. This cancels all Spot requests associated with the Spot fleet, so that no new Spot instances are launched for your Spot fleet. You must specify whether the Spot fleet should terminate its Spot instances. If you terminate the instances, the Spot fleet request enters the cancelled_terminating state. Otherwise, the Spot fleet request enters the cancelled_running state and the instances continue to run until they are interrupted or you terminate them manually.

To cancel a Spot fleet request using the console

  1. Open the Spot console at https://console.aws.amazon.com/ec2spot/home/fleet.

  2. Select your Spot fleet request.

  3. Click Actions, and then click Cancel spot request.

  4. In Cancel spot request, verify that you want to cancel the Spot fleet. To keep the fleet at its current size, deselect Terminate instances. When you are ready, click Confirm.

To cancel a Spot fleet request using the AWS CLI

Use the following cancel-spot-fleet-requests command to cancel the specified Spot fleet request and terminate the instances:

aws ec2 cancel-spot-fleet-requests --spot-fleet-request-ids sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE --terminate-instances

The following is example output:

{
    "SuccessfulFleetRequests": [
        {
            "SpotFleetRequestId": "sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE",
            "CurrentSpotFleetRequestState": "cancelled_terminating",
            "PreviousSpotFleetRequestState": "active"
        }
    ],
    "UnsuccessfulFleetRequests": []
}

You can modify the previous command as follows to cancel the specified Spot fleet request without terminating the instances:

aws ec2 cancel-spot-fleet-requests --spot-fleet-request-ids sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE --no-terminate-instances

The following is example output:

{
    "SuccessfulFleetRequests": [
        {
            "SpotFleetRequestId": "sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE",
            "CurrentSpotFleetRequestState": "cancelled_running",
            "PreviousSpotFleetRequestState": "active"
        }
    ],
    "UnsuccessfulFleetRequests": []
}