Skip to content

How do I use Fargate Spot capacity providers for Amazon ECS?

5 minute read
0

I want to use AWS Fargate Spot capacity providers for Amazon Elastic Container Service (Amazon ECS).

Short description

Prerequisite:

Review the following information:

  • You don't need to create Fargate and Fargate Spot capacity providers. The capacity providers are available to all AWS accounts and only need to be associated with a cluster to be available for use.
  • You can use the PutClusterCapacityProviders API or the put-cluster-capacity-providers AWS Command Line Interface (AWS CLI) command to associate a Fargate Spot capacity provider. You can't use the AWS Management Console to add a Fargate Spot capacity provider to an existing cluster.
  • Fargate and Fargate Spot capacity providers are reserved and can't be deleted. To disassociate them from a cluster, use the PutClusterCapacityProviders API.
  • Fargate Spot requires that your task use platform version 1.3.0 or later (for Linux). Fargate Spot capacity provider isn't supported for Windows containers on Fargate.

Resolution

Note: If you receive errors when you run AWS CLI commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.

Associate and disassociate Fargate Spot capacity providers

If you use the Networking only cluster template to create a Fargate Spot capacity provider, then then provider is automatically associated with the cluster. For more information, see Creating an Amazon ECS cluster for the Fargate launch type.

Associate a Fargate Spot capacity provider with your cluster

To associate a Fargate Spot capacity provider with your cluster, use the PutClusterCapacityProviders API. Or, run the following command:

aws ecs put-cluster-capacity-providers \
        --cluster <Cluster_name> \
        --capacity-providers FARGATE FARGATE_SPOT \
        --default-capacity-provider-strategy capacityProvider=FARGATE,weight=1 capacityProvider=FARGATE_SPOT,weight=1 \
        --region <Region>

Important: Include all your existing capacity providers and the capacity provider strategy in the command. Otherwise, you can overwrite your capacity providers. Providers that aren't included in the command are disassociated from the cluster.

Disassociate a Fargate Spot capacity provider with your cluster

Existing capacity providers that are associated with a cluster, and that are omitted from a PutClusterCapacityProviders API call are disassociated from the cluster. To use the AWS CLI to disassociate the Fargate Spot capacity provider from your Amazon ECS cluster, run the following command:

aws ecs put-cluster-capacity-providers \
        --cluster <Cluster_name> \
        --capacity-providers FARGATE \
        --default-capacity-provider-strategy capacityProvider=FARGATE,weight=1 \
        --region <Region>

Verify that your Fargate Spot capacity provider is associated with your Amazon ECS cluster

To verify that the Fargate Spot capacity provider is associated with your Amazon ECS cluster, run the following command:

aws ecs describe-clusters \
        --cluster <Cluster_name> \
        --region <Region>

The output looks similar to the following:

Output:
      "capacityProviders": [
           "FARGATE",
           "FARGATE_SPOT"
      ]

Note: The output of the command includes the capacity providers that are associated with your Amazon ECS cluster.

Use a Fargate Spot capacity provider to run a task or create a service

To use a Fargate Spot capacity provider to run a task, run the following command:

aws ecs run-task \
        --cluster <Cluster_name> \
        --capacity-provider-strategy capacityProvider=FARGATE_SPOT,weight=1 \
        --task-definition <Task_definition_family>:<revision> \
        --network-configuration "awsvpcConfiguration={subnets=[string,string],securityGroups=[string,string],assignPublicIp=string}" \
        --count <Number_of_Tasks> \
        --region <Region>

To create an Amazon ECS service that uses Fargate Spot capacity providers, run the following command:

aws ecs create-service \
         --cluster <Cluster_name> \
        --service-name <Service_name> \
        --capacity-provider-strategy capacityProvider=FARGATE,weight=1 capacityProvider=FARGATE_SPOT,weight=1 \
        --task-definition <Task_defintition_family>:<revision> \
        --network-configuration "awsvpcConfiguration={subnets=[string,string],securityGroups=[string,string],assignPublicIp=string}" \
        --desired-count <Number_of_tasks> \
        --region <Region>

Verify that your tasks are running on the Fargate Spot capacity provider

To confirm that your tasks are using Fargate capacity providers, run the following command:

aws ecs describe-tasks
         --cluster <Cluster_name> \
        --tasks <TaskID> \
        --region <Region>

You can't use Amazon CloudWatch to track Fargate OnDemand and Fargate Spot usage per service. However, you can use CloudWatch to see the metric for total Fargate OnDemand and Spot usage. For more information, see AWS Fargate usage metrics.

Frequently asked questions

What are some Fargate Spot best practices?

  • You can use Fargate Spot for stateless, fault-tolerant workloads, but don't rely on Spot Tasks for critical workloads. Instead, configure a mix of On-Demand Fargate Tasks.
  • To gracefully handle interruptions, use SIGTERM signals. When you receive a SIGTERM signal, it's a best practice to set StopTimeout to 120 seconds. For more information, see Graceful shutdowns with ECS.
  • Applications that run on Fargate Spot must be fault-tolerant.

What happens to the tasks when the FARGATE_SPOT capacity isn't available?

When the ECS scheduler can't launch a task because there's no capacity, the SERVICE_TASK_PLACEMENT_FAILURE event is emitted. The Task doesn't get to the first stage PROVISIONING, and no notice is displayed in the ECS Events. ECS scheduler continues to try to launch the task. If capacity becomes available, then the SERVICE_STEADY_STATE event is emitted.

Is there a failover to Fargate when no Fargate Spot capacity is available?

When there's no FARGATE_SPOT capacity available, it's not possible to have a fail back mechanism to Fargate.

AWS OFFICIALUpdated a year ago