Work with Spot Fleets - Amazon Elastic Compute Cloud
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Work with Spot Fleets

To start using a Spot Fleet, you create a Spot Fleet request that includes the target capacity, an optional On-Demand portion, one or more launch specifications for the instances, and the maximum price that you are willing to pay. The fleet request must include a launch specification that defines the information that the fleet needs to launch an instance, such as an AMI, instance type, subnet or Availability Zone, and one or more security groups.

If your fleet includes Spot Instances, Amazon EC2 can attempt to maintain your fleet target capacity as Spot prices change.

It is not possible to modify the target capacity of a one-time request after it's been submitted. To change the target capacity, cancel the request and submit a new one.

A Spot Fleet request remains active until it expires or you cancel it. When you cancel a fleet request, you can specify whether canceling the request terminates the Spot Instances in that 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 instances. If a request would exceed your Spot Fleet limits, it is canceled immediately.

  • 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. A one-time request cannot be modified, and this state does not apply to such Spot requests.

  • cancelled_running – The Spot Fleet is canceled and does not launch additional Spot Instances. 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.

Spot Fleet health checks

Spot Fleet checks the health status of the Spot Instances in the fleet every two minutes. The health status of an instance is either healthy or unhealthy.

Spot Fleet determines the health status of an instance by using the status checks provided by Amazon EC2. An instance is determined as unhealthy when the status of either the instance status check or the system status check is impaired for three consecutive health checks. For more information, see Status checks for your instances.

You can configure your fleet to replace unhealthy Spot Instances. After enabling health check replacement, a Spot Instance is replaced when it is reported as unhealthy. The fleet could go below its target capacity for up to a few minutes while an unhealthy Spot Instance is being replaced.

Requirements
  • Health check replacement is supported only for Spot Fleets that maintain a target capacity (fleets of type maintain), not for one-time Spot Fleets (fleets of type request).

  • Health check replacement is supported only for Spot Instances. This feature is not supported for On-Demand Instances.

  • You can configure your Spot Fleet to replace unhealthy instances only when you create it.

  • Users can use health check replacement only if they have permission to call the ec2:DescribeInstanceStatus action.

Console
To configure a Spot Fleet to replace unhealthy Spot Instances using the console
  1. Follow the steps for creating a Spot Fleet. For more information, see Create a Spot Fleet request using defined parameters (console).

  2. To configure the fleet to replace unhealthy Spot Instances, for Health check, choose Replace unhealthy instances. To enable this option, you must first choose Maintain target capacity.

Amazon CLI
To configure a Spot Fleet to replace unhealthy Spot Instances using the Amazon CLI
  1. Follow the steps for creating a Spot Fleet. For more information, see Create a Spot Fleet using the Amazon CLI.

  2. To configure the fleet to replace unhealthy Spot Instances, for ReplaceUnhealthyInstances, enter true.

Spot Fleet permissions

If your users will create or manage a Spot Fleet, you need to grant them the required permissions.

If you use the Amazon EC2 console to create a Spot Fleet, it creates two service-linked roles named AWSServiceRoleForEC2SpotFleet and AWSServiceRoleForEC2Spot, and a role named aws-ec2-spot-fleet-tagging-role that grant the Spot Fleet the permissions to request, launch, terminate, and tag resources on your behalf. If you use the Amazon CLI or an API, you must ensure that these roles exist.

Use the following instructions to grant the required permissions and create the roles.

Grant permission to users for Spot Fleet

If your users will create or manage a Spot Fleet, be sure to grant them the required permissions.

To create a policy for Spot Fleet
  1. Open the IAM console at https://console.amazonaws.cn/iam/.

  2. In the navigation pane, choose Policies, Create policy.

  3. On the Create policy page, choose JSON, and replace the text with the following.

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ec2:RunInstances", "ec2:CreateTags", "ec2:RequestSpotFleet", "ec2:ModifySpotFleetRequest", "ec2:CancelSpotFleetRequests", "ec2:DescribeSpotFleetRequests", "ec2:DescribeSpotFleetInstances", "ec2:DescribeSpotFleetRequestHistory" ], "Resource": "*" }, { "Effect": "Allow", "Action": "iam:PassRole", "Resource": "arn:aws:iam::*:role/aws-ec2-spot-fleet-tagging-role" }, { "Effect": "Allow", "Action": [ "iam:CreateServiceLinkedRole", "iam:ListRoles", "iam:ListInstanceProfiles" ], "Resource": "*" } ] }

    The preceding example policy grants a user the permissions required for most Spot Fleet use cases. To limit the user to specific API actions, specify only those API actions instead.

    Required EC2 and IAM APIs

    The following APIs must be included in the policy:

    • ec2:RunInstances – Required to launch instances in a Spot Fleet

    • ec2:CreateTags – Required to tag the Spot Fleet request, instances, or volumes

    • iam:PassRole – Required to specify the Spot Fleet role

    • iam:CreateServiceLinkedRole – Required to create the service-linked role

    • iam:ListRoles – Required to enumerate existing IAM roles

    • iam:ListInstanceProfiles – Required to enumerate existing instance profiles

    Important

    If you specify a role for the IAM instance profile in the launch specification or launch template, you must grant the user the permission to pass the role to the service. To do this, in the IAM policy include "arn:aws:iam::*:role/IamInstanceProfile-role" as a resource for the iam:PassRole action. For more information, see Granting a user permissions to pass a role to an Amazon service in the IAM User Guide.

    Spot Fleet APIs

    Add the following Spot Fleet API actions to your policy, as needed:

    • ec2:RequestSpotFleet

    • ec2:ModifySpotFleetRequest

    • ec2:CancelSpotFleetRequests

    • ec2:DescribeSpotFleetRequests

    • ec2:DescribeSpotFleetInstances

    • ec2:DescribeSpotFleetRequestHistory

    Optional IAM APIs

    (Optional) To enable a user to create roles or instance profiles using the IAM console, you must add the following actions to the policy:

    • iam:AddRoleToInstanceProfile

    • iam:AttachRolePolicy

    • iam:CreateInstanceProfile

    • iam:CreateRole

    • iam:GetRole

    • iam:ListPolicies

  4. Choose Review policy.

  5. On the Review policy page, enter a policy name and description, and choose Create policy.

  6. To provide access, add permissions to your users, groups, or roles:

Service-linked role for Spot Fleet

Amazon EC2 uses service-linked roles for the permissions that it requires to call other Amazon services on your behalf. A service-linked role is a unique type of IAM role that is linked directly to an Amazon service. Service-linked roles provide a secure way to delegate permissions to Amazon services because only the linked service can assume a service-linked role. For more information, see Using service-linked roles in the IAM User Guide.

Amazon EC2 uses the service-linked role named AWSServiceRoleForEC2SpotFleet to launch and manage instances on your behalf.

Important

If you specify an encrypted AMI or an encrypted Amazon EBS snapshot in your Spot Fleet, you must grant the AWSServiceRoleForEC2SpotFleet role permission to use the CMK so that Amazon EC2 can launch instances on your behalf. For more information, see Grant access to CMKs for use with encrypted AMIs and EBS snapshots.

Permissions granted by AWSServiceRoleForEC2SpotFleet

Amazon EC2 uses AWSServiceRoleForEC2SpotFleet to complete the following actions:

  • ec2:RequestSpotInstances - Request Spot Instances

  • ec2:RunInstances - Launch instances

  • ec2:TerminateInstances - Terminate instances

  • ec2:DescribeImages - Describe Amazon Machine Images (AMIs) for the instances

  • ec2:DescribeInstanceStatus - Describe the status of the instances

  • ec2:DescribeSubnets - Describe the subnets for the instances

  • ec2:CreateTags - Add tags to the Spot Fleet request, instances, and volumes

  • elasticloadbalancing:RegisterInstancesWithLoadBalancer - Add the specified instances to the specified load balancer

  • elasticloadbalancing:RegisterTargets - Register the specified targets with the specified target group

Create the service-linked role

Under most circumstances, you don't need to manually create a service-linked role. Amazon EC2 creates the AWSServiceRoleForEC2SpotFleet service-linked role the first time you create a Spot Fleet using the console.

If you had an active Spot Fleet request before October 2017, when Amazon EC2 began supporting this service-linked role, Amazon EC2 created the AWSServiceRoleForEC2SpotFleet role in your Amazon account. For more information, see A new role appeared in my Amazon account in the IAM User Guide.

If you use the Amazon CLI or an API to create a Spot Fleet, you must first ensure that this role exists.

To create AWSServiceRoleForEC2SpotFleet using the console
  1. Open the IAM console at https://console.amazonaws.cn/iam/.

  2. In the navigation pane, choose Roles.

  3. Choose Create role.

  4. On the Select trusted entity page, do the following:

    1. For Trusted entity type, choose Amazon service.

    2. Under Use case, for Service or use case, choose EC2.

    3. For Use case, choose EC2 - Spot Fleet.

    4. Choose Next.

  5. On the Add permissions page, choose Next.

  6. On the Name, review, and create page, choose Create role.

To create AWSServiceRoleForEC2SpotFleet using the Amazon CLI

Use the create-service-linked-role command as follows.

aws iam create-service-linked-role --aws-service-name spotfleet.amazonaws.com

If you no longer need to use Spot Fleet, we recommend that you delete the AWSServiceRoleForEC2SpotFleet role. After this role is deleted from your account, Amazon EC2 will create the role again if you request a Spot Fleet using the console. For more information, see Deleting a service-linked role in the IAM User Guide.

Grant access to CMKs for use with encrypted AMIs and EBS snapshots

If you specify an encrypted AMI or an encrypted Amazon EBS snapshot in your Spot Fleet request and you use a customer managed key for encryption, you must grant the AWSServiceRoleForEC2SpotFleet role permission to use the CMK so that Amazon EC2 can launch instances on your behalf. To do this, you must add a grant to the CMK, as shown in the following procedure.

When providing permissions, grants are an alternative to key policies. For more information, see Using Grants and Using Key Policies in Amazon KMS in the Amazon Key Management Service Developer Guide.

To grant the AWSServiceRoleForEC2SpotFleet role permissions to use the CMK
  • Use the create-grant command to add a grant to the CMK and to specify the principal (the AWSServiceRoleForEC2SpotFleet service-linked role) that is given permission to perform the operations that the grant permits. The CMK is specified by the key-id parameter and the ARN of the CMK. The principal is specified by the grantee-principal parameter and the ARN of the AWSServiceRoleForEC2SpotFleet service-linked role.

    aws kms create-grant \ --region us-east-1 \ --key-id arn:aws:kms:us-east-1:444455556666:key/1234abcd-12ab-34cd-56ef-1234567890ab \ --grantee-principal arn:aws:iam::111122223333:role/AWSServiceRoleForEC2SpotFleet \ --operations "Decrypt" "Encrypt" "GenerateDataKey" "GenerateDataKeyWithoutPlaintext" "CreateGrant" "DescribeKey" "ReEncryptFrom" "ReEncryptTo"

Service-linked role for Spot Instances

Amazon EC2 uses the service-linked role named AWSServiceRoleForEC2Spot to launch and manage Spot Instances on your behalf. For more information, see Service-linked role for Spot Instance requests.

IAM role for tagging a Spot Fleet

The aws-ec2-spot-fleet-tagging-role IAM role grants the Spot Fleet permission to tag the Spot Fleet request, instances, and volumes. For more information, see Tag a Spot Fleet.

Important

If you choose to tag instances in the fleet and you also choose to maintain target capacity (the Spot Fleet request is of type maintain), the differences in the permissions that are set for the user and the IamFleetRole might lead to inconsistent tagging behavior of instances in the fleet. If the IamFleetRole does not include the CreateTags permission, some of the instances launched by the fleet might not be tagged. While we are working to fix this inconsistency, to ensure that all instances launched by the fleet are tagged, we recommend that you use the aws-ec2-spot-fleet-tagging-role role for the IamFleetRole. Alternatively, to use an existing role, attach the AmazonEC2SpotFleetTaggingRole Amazon Managed Policy to the existing role. Otherwise, you need to manually add the CreateTags permission to your existing policy.

To create the IAM role for tagging a Spot Fleet
  1. Open the IAM console at https://console.amazonaws.cn/iam/.

  2. In the navigation pane, choose Roles.

  3. Choose Create role.

  4. On the Select trusted entity page, under Trusted entity type, choose Amazon service.

  5. Under Use case, from Use cases for other Amazon services, choose EC2, and then choose EC2 - Spot Fleet Tagging.

  6. Choose Next.

  7. On the Add permissions page, choose Next.

  8. On the Name, review, and create page, for Role name, enter a name for the role (for example, aws-ec2-spot-fleet-tagging-role).

  9. Review the information on the page, and then choose Create role.

Cross-service confused deputy prevention

The confused deputy problem is a security issue where an entity that doesn't have permission to perform an action can coerce a more-privileged entity to perform the action. We recommend that you use the aws:SourceArn and aws:SourceAccount global condition context keys in the aws-ec2-spot-fleet-tagging-role trust policy to limit the permissions that Spot Fleet gives another service to the resource.

To add the aws:SourceArn and aws:SourceAccount condition keys to the aws-ec2-spot-fleet-tagging-role trust policy
  1. Open the IAM console at https://console.amazonaws.cn/iam/.

  2. In the navigation pane, choose Roles.

  3. Find the aws-ec2-spot-fleet-tagging-role that you created previously and choose the link (not the check box).

  4. Under Summary, choose the Trust relationships tab, and then choose Edit trust policy.

  5. In the JSON statement, add a Condition element containing your aws:SourceAccount and aws:SourceArn global condition context keys to prevent the confused deputy problem, as follows:

    "Condition": { "ArnLike": { "aws:SourceArn": "arn:aws:ec2:us-east-1:account_id:spot-fleet-request/sfr-*" }, "StringEquals": { "aws:SourceAccount": "account_id" }
    Note

    If the aws:SourceArn value contains the account ID and you use both global condition context keys, the aws:SourceAccount value and the account in the aws:SourceArn value must use the same account ID when used in the same policy statement.

    The final trust policy will be as follows:

    { "Version": "2012-10-17", "Statement": { "Sid": "ConfusedDeputyPreventionExamplePolicy", "Effect": "Allow", "Principal": { "Service": "spotfleet.amazonaws.com" }, "Action": "sts:AssumeRole", "Condition": { "ArnLike": { "aws:SourceArn": "arn:aws-cn:ec2:us-east-1:account_id:spot-fleet-request/sfr-*" }, "StringEquals": { "aws:SourceAccount": "account_id" } } } }
  6. Choose Update policy.

The following table provides potential values for aws:SourceArn to limit the scope of the your aws-ec2-spot-fleet-tagging-role in varying degrees of specificity.

API operation Called service Scope aws:SourceArn
RequestSpotFleet Amazon STS (AssumeRole) Limit the AssumeRole capability on aws-ec2-spot-fleet-tagging-role to spot-fleet-requests in the specified account. arn:aws-cn:ec2:*:123456789012:spot-fleet-request/sfr-*
RequestSpotFleet Amazon STS (AssumeRole) Limit the AssumeRole capability on aws-ec2-spot-fleet-tagging-role to spot-fleet-requests in the specified account and specified Region. Note that this role will not be usable in other Regions. arn:aws-cn:ec2:us-east-1:123456789012:spot-fleet-request/sfr-*
RequestSpotFleet Amazon STS (AssumeRole) Limit the AssumeRole capability on aws-ec2-spot-fleet-tagging-role to only actions affecting the fleet sfr-11111111-1111-1111-1111-111111111111. Note that this role may not be usable for other Spot Fleets. Also, this role cannot be used to launch any new Spot Fleets through request-spot-fleet. arn:aws-cn:ec2:us-east-1:123456789012:spot-fleet-request/sfr-11111111-1111-1111-1111-111111111111

Create a Spot Fleet request

Using the Amazon Web Services Management Console, quickly create a Spot Fleet request by choosing only your application or task need and minimum compute specs. Amazon EC2 configures a fleet that best meets your needs and follows Spot best practice. For more information, see Quickly create a Spot Fleet request (console). Otherwise, you can modify any of the default settings. For more information, see Create a Spot Fleet request using defined parameters (console) and Create a Spot Fleet using the Amazon CLI.

Quickly create a Spot Fleet request (console)

Follow these steps to quickly create a Spot Fleet request.

To create a Spot Fleet request using the recommended settings (console)
  1. Open the Amazon EC2 console at https://console.amazonaws.cn/ec2/.

  2. In the navigation pane, choose Spot Requests.

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

  4. Under Launch parameters, choose Manually configure launch parameters.

  5. For AMI, choose an AMI.

  6. Under Target capacity, for Total target capacity, specify the number of units to request. For the type of unit, you can choose Instances, vCPUs, or Memory (MiB).

  7. For Your fleet request at a glance, review your fleet configuration, and choose Launch.

Create a Spot Fleet request using defined parameters (console)

You can create a Spot Fleet by using parameters that you define.

To create a Spot Fleet request using defined parameters (console)
  1. Open the Amazon EC2 console at https://console.amazonaws.cn/ec2/.

  2. In the navigation pane, choose Spot Requests.

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

  4. For Launch parameters, do the following:

    1. To define the launch parameters in the Spot console, choose Manually configure launch parameters.

    2. For AMI, choose one of the basic AMIs provided by Amazon, or choose Search for AMI to use an AMI from our user community, the Amazon Web Services Marketplace, or one of your own.

      Note

      If an AMI specified in the launch parameters is deregistered or disabled, no new instances can be launched from the AMI. For fleets that are set to maintain target capacity, the target capacity will not be maintained.

    3. (Optional) For Key pair name, choose an existing key pair or create a new one.

      [Existing key pair] Choose the key pair.

      [New key pair] Choose Create new key pair to go the Key Pairs page. When you are done, return to the Spot Requests page and refresh the list.

    4. (Optional) Expand Additional launch parameters, and do the following:

      1. (Optional) To enable Amazon EBS optimization, for EBS-optimized, select Launch EBS-optimized instances.

      2. (Optional) To add temporary block-level storage for your instances, for Instance store, choose Attach at launch.

      3. (Optional) To add storage, choose Add new volume, and specify additional instance store volumes or Amazon EBS volumes, depending on the instance type.

      4. (Optional) By default, basic monitoring is enabled for your instances. To enable detailed monitoring, for Monitoring, select Enable CloudWatch detailed monitoring.

      5. (Optional) To run a Dedicated Spot Instance, for Tenancy, choose Dedicated - run a dedicated instance.

      6. (Optional) For Security groups, choose one or more security groups or create a new one.

        [Existing security group] Choose one or more security groups.

        [New security group] Choose Create new security group to go the Security Groups page. When you are done, return to the Spot Requests and refresh the list.

      7. (Optional) To make your instances reachable from the internet, for Auto-assign IPv4 Public IP, choose Enable.

      8. (Optional) To launch your Spot Instances with an IAM role, for IAM instance profile, choose the role.

      9. (Optional) To run a start-up script, copy it to User data.

      10. (Optional) To add a tag, choose Create tag and enter the key and value for the tag, and choose Create. Repeat for each tag.

        For each tag, to tag the instances and the Spot Fleet request with the same tag, ensure that both Instances and Fleet are selected. To tag only the instances launched by the fleet, clear Fleet. To tag only the Spot Fleet request, clear Instances.

  5. For Additional request details, do the following:

    1. Review the additional request details. To make changes, clear Apply defaults.

    2. (Optional) For IAM fleet role, you can use the default role or choose a different role. To use the default role after changing the role, choose Use default role.

    3. (Optional) For Maximum price, you can use the default maximum price (the On-Demand price) or specify the maximum price you are willing to pay. If your maximum price is lower than the Spot price for the instance types that you selected, your Spot Instances are not launched.

    4. (Optional) To create a request that is valid only during a specific time period, edit Request valid from and Request valid until.

    5. (Optional) By default, we terminate your Spot Instances when the Spot Fleet request expires. To keep them running after your request expires, clear Terminate the instances when the request expires.

    6. (Optional) To register your Spot Instances with a load balancer, choose Receive traffic from one or more load balancers and choose one or more Classic Load Balancers or target groups.

  6. For Minimum compute unit, choose the minimum hardware specifications (vCPUs, memory, and storage) that you need for your application or task, either as specs or as an instance type.

    • For as specs, specify the required number of vCPUs and amount of memory.

    • For as an instance type, accept the default instance type, or choose Change instance type to choose a different instance type.

  7. For Target capacity, do the following:

    1. For Total target capacity, specify the number of units to request. For the type of unit, you can choose Instances, vCPUs, or Memory (MiB). To specify a target capacity of 0 so that you can add capacity later, choose Maintain target capacity.

    2. (Optional) For Include On-Demand base capacity, specify the number of On-Demand units to request. The number must be less than the Total target capacity. Amazon EC2 calculates the difference, and allocates the difference to Spot units to request.

      Important

      To specify optional On-Demand capacity, you must first choose a launch template.

    3. (Optional) By default, Amazon EC2 terminates Spot Instances when they are interrupted. To maintain the target capacity, select Maintain target capacity. You can then specify that Amazon EC2 terminates, stops, or hibernates Spot Instances when they are interrupted. To do so, choose the corresponding option from Interruption behavior.

      Note

      If an AMI specified in the launch parameters is deregistered or disabled, no new instances can be launched from the AMI. For fleets that are set to maintain target capacity, the target capacity will not be maintained.

    4. (Optional) To allow Spot Fleet to launch a replacement Spot Instance when an instance rebalance notification is emitted for an existing Spot Instance in the fleet, select Capacity rebalance, and then choose an instance replacement strategy. If you choose Launch before terminate, specify the delay (in seconds) before Spot Fleet terminates the old instances. For more information, see Capacity Rebalancing.

    5. (Optional) To control the amount you pay per hour for all the Spot Instances in your fleet, select Set maximum cost for Spot Instances and then enter the maximum total amount you're willing to pay per hour. When the maximum total amount is reached, Spot Fleet stops launching Spot Instances even if it hasn’t met the target capacity. For more information, see Control spending.

  8. For Network, do the following:

    1. For Network, choose an existing VPC or create a new one.

      [Existing VPC] Choose the VPC.

      [New VPC] Choose Create new VPC to go the Amazon VPC console. When you are done, return to the wizard and refresh the list.

    2. (Optional) For Availability Zone, let Amazon choose the Availability Zones for your Spot Instances, or specify one or more Availability Zones.

      If you have more than one subnet in an Availability Zone, choose the appropriate subnet from Subnet. To add subnets, choose Create new subnet to go to the Amazon VPC console. When you are done, return to the wizard and refresh the list.

  9. For Instance type requirements, you can either specify instance attributes and let Amazon EC2 identify the optimal instance types with these attributes, or you can specify a list of instances. For more information, see Attribute-based instance type selection for Spot Fleet.

    1. If you choose Specify instance attributes that match your compute requirements, specify your instance attributes as follows:

      1. For vCPUs, enter the desired minimum and maximum number of vCPUs. To specify no limit, select No minimum, No maximum, or both.

      2. For Memory (GiB), enter the desired minimum and maximum amount of memory. To specify no limit, select No minimum, No maximum, or both.

      3. (Optional) For Additional instance attributes, you can optionally specify one or more attributes to express your compute requirements in more detail. Each additional attribute adds a further constraint to your request. You can omit the additional attributes; when omitted, the default values are used. For a description of each attribute and their default values, see get-spot-placement-scores in the Amazon EC2 Command Line Reference.

      4. (Optional) To view the instance types with your specified attributes, expand Preview matching instance types. To exclude instance types from being used in your request, select the instances and then choose Exclude selected instance types.

    2. If you choose Manually select instance types, Spot Fleet provides a default list of instance types. To select more instance types, choose Add instance types, select the instance types to use in your request, and choose Select. To delete instance types, select the instance types and choose Delete.

  10. For Allocation strategy, choose the strategy that meets your needs. For more information, see Allocation strategies for Spot Instances.

  11. For Your fleet request at a glance, review your fleet configuration, and make any adjustments if necessary.

  12. (Optional) To download a copy of the launch configuration for use with the Amazon CLI, choose JSON config.

  13. Choose Launch.

    The Spot Fleet request type is fleet. When the request is fulfilled, requests of type instance are added, where the state is active and the status is fulfilled.

Create a Spot Fleet using the Amazon CLI

To create a Spot Fleet request using the Amazon CLI
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" }

Tag a Spot Fleet

To help categorize and manage your Spot Fleet requests, you can tag them with custom metadata. You can assign a tag to a Spot Fleet request when you create it, or afterward. You can assign tags using the Amazon EC2 console or a command line tool.

When you tag a Spot Fleet request, the instances and volumes that are launched by the Spot Fleet are not automatically tagged. You need to explicitly tag the instances and volumes launched by the Spot Fleet. You can choose to assign tags to only the Spot Fleet request, or to only the instances launched by the fleet, or to only the volumes attached to the instances launched by the fleet, or to all three.

Note

Volume tags are only supported for volumes that are attached to On-Demand Instances. You can't tag volumes that are attached to Spot Instances.

For more information about how tags work, see Tag your Amazon EC2 resources.

Prerequisite

Grant the user the permission to tag resources. For more information, see Example: Tag resources.

To grant a user the permission to tag resources

Create an IAM policy that includes the following:

  • The ec2:CreateTags action. This grants the user permission to create tags.

  • The ec2:RequestSpotFleet action. This grants the user permission to create a Spot Fleet request.

  • For Resource, you must specify "*". This allows users to tag all resource types.

{ "Version": "2012-10-17", "Statement": [ { "Sid": "TagSpotFleetRequest", "Effect": "Allow", "Action": [ "ec2:CreateTags", "ec2:RequestSpotFleet" ], "Resource": "*" } ] }
Important

We currently do not support resource-level permissions for the spot-fleet-request resource. If you specify spot-fleet-request as a resource, you will get an unauthorized exception when you try to tag the fleet. The following example illustrates how not to set the policy.

{ "Effect": "Allow", "Action": [ "ec2:CreateTags", "ec2:RequestSpotFleet" ], "Resource": "arn:aws:ec2:us-east-1:111122223333:spot-fleet-request/*" }

To provide access, add permissions to your users, groups, or roles:

Tag a new Spot Fleet

To tag a new Spot Fleet request using the console
  1. Follow the Create a Spot Fleet request using defined parameters (console) procedure.

  2. To add a tag, expand Additional configurations, choose Add new tag, and enter the key and value for the tag. Repeat for each tag.

    For each tag, you can tag the Spot Fleet request and the instances with the same tag. To tag both, ensure that both Instance tags and Fleet tags are selected. To tag only the Spot Fleet request, clear Instance tags. To tag only the instances launched by the fleet, clear Fleet tags.

  3. Complete the required fields to create a Spot Fleet request, and then choose Launch. For more information, see Create a Spot Fleet request using defined parameters (console).

To tag a new Spot Fleet request using the Amazon CLI

To tag a Spot Fleet request when you create it, configure the Spot Fleet request configuration as follows:

  • Specify the tags for the Spot Fleet request in SpotFleetRequestConfig.

  • For ResourceType, specify spot-fleet-request. If you specify another value, the fleet request will fail.

  • For Tags, specify the key-value pair. You can specify more than one key-value pair.

In the following example, the Spot Fleet request is tagged with two tags: Key=Environment and Value=Production, and Key=Cost-Center and Value=123.

{ "SpotFleetRequestConfig": { "AllocationStrategy": "lowestPrice", "ExcessCapacityTerminationPolicy": "default", "IamFleetRole": "arn:aws:iam::111122223333:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-0123456789EXAMPLE", "InstanceType": "c4.large" } ], "SpotPrice": "5", "TargetCapacity": 2, "TerminateInstancesWithExpiration": true, "Type": "maintain", "ReplaceUnhealthyInstances": true, "InstanceInterruptionBehavior": "terminate", "InstancePoolsToUseCount": 1, "TagSpecifications": [ { "ResourceType": "spot-fleet-request", "Tags": [ { "Key": "Environment", "Value":"Production" }, { "Key": "Cost-Center", "Value":"123" } ] } ] } }

Tag a new Spot Fleet and the instances and volumes that it launches

To tag a new Spot Fleet request and the instances and volumes that it launches using the Amazon CLI

To tag a Spot Fleet request when you create it, and to tag the instances and volumes when they are launched by the fleet, configure the Spot Fleet request configuration as follows:

Spot Fleet request tags:
  • Specify the tags for the Spot Fleet request in SpotFleetRequestConfig.

  • For ResourceType, specify spot-fleet-request. If you specify another value, the fleet request will fail.

  • For Tags, specify the key-value pair. You can specify more than one key-value pair.

Instance tags:
  • Specify the tags for the instances in LaunchSpecifications.

  • For ResourceType, specify instance. If you specify another value, the fleet request will fail.

  • For Tags, specify the key-value pair. You can specify more than one key-value pair.

    Alternatively, you can specify the tags for the instance in the launch template that is referenced in the Spot Fleet request.

Volume tags:
  • Specify the tags for the volumes in the launch template that is referenced in the Spot Fleet request. Volume tagging in LaunchSpecifications is not supported.

In the following example, the Spot Fleet request is tagged with two tags: Key=Environment and Value=Production, and Key=Cost-Center and Value=123. The instances that are launched by the fleet are tagged with one tag (which is the same as one of the tags for the Spot Fleet request): Key=Cost-Center and Value=123.

{ "SpotFleetRequestConfig": { "AllocationStrategy": "lowestPrice", "ExcessCapacityTerminationPolicy": "default", "IamFleetRole": "arn:aws:iam::111122223333:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-0123456789EXAMPLE", "InstanceType": "c4.large", "TagSpecifications": [ { "ResourceType": "instance", "Tags": [ { "Key": "Cost-Center", "Value": "123" } ] } ] } ], "SpotPrice": "5", "TargetCapacity": 2, "TerminateInstancesWithExpiration": true, "Type": "maintain", "ReplaceUnhealthyInstances": true, "InstanceInterruptionBehavior": "terminate", "InstancePoolsToUseCount": 1, "TagSpecifications": [ { "ResourceType": "spot-fleet-request", "Tags": [ { "Key": "Environment", "Value":"Production" }, { "Key": "Cost-Center", "Value":"123" } ] } ] } }
To tag instances launched by a Spot Fleet using the Amazon CLI

To tag instances when they are launched by the fleet, you can either specify the tags in the launch template that is referenced in the Spot Fleet request, or you can specify the tags in the Spot Fleet request configuration as follows:

  • Specify the tags for the instances in LaunchSpecifications.

  • For ResourceType, specify instance. If you specify another value, the fleet request will fail.

  • For Tags, specify the key-value pair. You can specify more than one key-value pair.

In the following example, the instances that are launched by the fleet are tagged with one tag: Key=Cost-Center and Value=123.

{ "SpotFleetRequestConfig": { "AllocationStrategy": "lowestPrice", "ExcessCapacityTerminationPolicy": "default", "IamFleetRole": "arn:aws:iam::111122223333:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-0123456789EXAMPLE", "InstanceType": "c4.large", "TagSpecifications": [ { "ResourceType": "instance", "Tags": [ { "Key": "Cost-Center", "Value": "123" } ] } ] } ], "SpotPrice": "5", "TargetCapacity": 2, "TerminateInstancesWithExpiration": true, "Type": "maintain", "ReplaceUnhealthyInstances": true, "InstanceInterruptionBehavior": "terminate", "InstancePoolsToUseCount": 1 } }
To tag volumes attached to On-Demand Instances launched by a Spot Fleet using the Amazon CLI

To tag volumes when they are created by the fleet, you must specify the tags in the launch template that is referenced in the Spot Fleet request.

Note

Volume tags are only supported for volumes that are attached to On-Demand Instances. You can't tag volumes that are attached to Spot Instances.

Volume tagging in LaunchSpecifications is not supported.

Tag an existing Spot Fleet

To tag an existing Spot Fleet request using the console

After you have created a Spot Fleet request, you can add tags to the fleet request using the console.

  1. Open the Amazon EC2 console at https://console.amazonaws.cn/ec2/.

  2. In the navigation pane, choose Spot Requests.

  3. Select your Spot Fleet request.

  4. Choose the Tags tab and choose Create Tag.

To tag an existing Spot Fleet request using the Amazon CLI

You can use the create-tags command to tag existing resources. In the following example, the existing Spot Fleet request is tagged with Key=purpose and Value=test.

aws ec2 create-tags \ --resources sfr-11112222-3333-4444-5555-66666EXAMPLE \ --tags Key=purpose,Value=test

View Spot Fleet request tags

To view Spot Fleet request tags using the console
  1. Open the Amazon EC2 console at https://console.amazonaws.cn/ec2/.

  2. In the navigation pane, choose Spot Requests.

  3. Select your Spot Fleet request and choose the Tags tab.

To describe Spot Fleet request tags

Use the describe-tags command to view the tags for the specified resource. In the following example, you describe the tags for the specified Spot Fleet request.

aws ec2 describe-tags \ --filters "Name=resource-id,Values=sfr-11112222-3333-4444-5555-66666EXAMPLE"
{ "Tags": [ { "Key": "Environment", "ResourceId": "sfr-11112222-3333-4444-5555-66666EXAMPLE", "ResourceType": "spot-fleet-request", "Value": "Production" }, { "Key": "Another key", "ResourceId": "sfr-11112222-3333-4444-5555-66666EXAMPLE", "ResourceType": "spot-fleet-request", "Value": "Another value" } ] }

You can also view the tags of a Spot Fleet request by describing the Spot Fleet request.

Use the describe-spot-fleet-requests command to view the configuration of the specified Spot Fleet request, which includes any tags that were specified for the fleet request.

aws ec2 describe-spot-fleet-requests \ --spot-fleet-request-ids sfr-11112222-3333-4444-5555-66666EXAMPLE
{ "SpotFleetRequestConfigs": [ { "ActivityStatus": "fulfilled", "CreateTime": "2020-02-13T02:49:19.709Z", "SpotFleetRequestConfig": { "AllocationStrategy": "capacityOptimized", "OnDemandAllocationStrategy": "lowestPrice", "ExcessCapacityTerminationPolicy": "Default", "FulfilledCapacity": 2.0, "OnDemandFulfilledCapacity": 0.0, "IamFleetRole": "arn:aws:iam::111122223333:role/aws-ec2-spot-fleet-tagging-role", "LaunchSpecifications": [ { "ImageId": "ami-0123456789EXAMPLE", "InstanceType": "c4.large" } ], "TargetCapacity": 2, "OnDemandTargetCapacity": 0, "Type": "maintain", "ReplaceUnhealthyInstances": false, "InstanceInterruptionBehavior": "terminate" }, "SpotFleetRequestId": "sfr-11112222-3333-4444-5555-66666EXAMPLE", "SpotFleetRequestState": "active", "Tags": [ { "Key": "Environment", "Value": "Production" }, { "Key": "Another key", "Value": "Another value" } ] } ] }

Describe your Spot Fleet

The Spot Fleet launches Spot Instances when your maximum price exceeds the Spot price and capacity is available. The Spot Instances run until they are interrupted or you terminate them.

To describe your Spot Fleet (console)
  1. Open the Amazon EC2 console at https://console.amazonaws.cn/ec2/.

  2. In the navigation pane, choose Spot Requests.

  3. Select your Spot Fleet request. To see the configuration details, choose Description.

  4. To list the Spot Instances for the Spot Fleet, choose Instances.

  5. To view the history for the Spot Fleet, choose History.

To describe your Spot Fleet (Amazon CLI)

Use the describe-spot-fleet-requests command to describe your Spot Fleet requests.

aws ec2 describe-spot-fleet-requests

Use the 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 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

Modify a Spot Fleet request

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

  • Increase the target capacity and On-Demand portion

  • Decrease the target capacity and On-Demand portion

Note

You can't modify a one-time Spot Fleet request. You can only modify a Spot Fleet request if you selected Maintain target capacity when you created the Spot Fleet request.

When you increase the target capacity, the Spot Fleet launches additional Spot Instances. When you increase the On-Demand portion, the Spot Fleet launches additional On-Demand Instances.

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 capacity 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 requests 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.

When a Spot Fleet terminates an instance because the target capacity was decreased, the instance receives a Spot Instance interruption notice.

To modify a Spot Fleet request (console)
  1. Open the Amazon EC2 console at https://console.amazonaws.cn/ec2/.

  2. In the navigation pane, choose Spot Requests.

  3. Select your Spot Fleet request.

  4. Choose Actions, Modify target capacity.

  5. In Modify target capacity, do the following:

    1. Enter the new target capacity and On-Demand portion.

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

    3. Choose Submit.

To modify a Spot Fleet request using the Amazon CLI

Use the 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

Cancel a Spot Fleet request

If you no longer require a Spot Fleet, you can cancel the Spot Fleet request. After you cancel a fleet request, all Spot requests associated with the fleet are also canceled, so that no new Spot Instances are launched.

When you cancel a Spot Fleet request, you must also specify if you want to terminate all of its instances. These include both On-Demand Instances and Spot Instances.

If you specify that the instances must be terminated when the fleet request is canceled, the fleet request enters the cancelled_terminating state. Otherwise, the fleet request enters the cancelled_running state and the instances continue to run until they are interrupted or you terminate them manually.

Restrictions
  • You can delete up to 100 fleets in a single request. If you exceed the specified number, no fleets are deleted.

To cancel a Spot Fleet request (console)
  1. Open the Amazon EC2 console at https://console.amazonaws.cn/ec2/.

  2. In the navigation pane, choose Spot Requests.

  3. Select your Spot Fleet request.

  4. Choose Actions, Cancel request.

  5. In the Cancel Spot request dialog box, do the following:

    1. To terminate the associated instances at the same time as canceling the Spot Fleet request, leave the Terminate instances check box selected. To cancel the Spot Fleet request without terminating the associated instances, clear the Terminate instances check box.

    2. Choose Confirm.

To cancel a Spot Fleet request and terminate its instances using the Amazon CLI

Use the cancel-spot-fleet-requests command to cancel the specified Spot Fleet request and terminate its On-Demand Instances and Spot Instances.

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

Example output

{ "SuccessfulFleetRequests": [ { "SpotFleetRequestId": "sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE", "CurrentSpotFleetRequestState": "cancelled_terminating", "PreviousSpotFleetRequestState": "active" } ], "UnsuccessfulFleetRequests": [] }
To cancel a Spot Fleet request without terminating its instances using the Amazon CLI

You can modify the previous command using the --no-terminate-instances parameter to cancel the specified Spot Fleet request without terminating its On-Demand Instances and Spot Instances.

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

Example output

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