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

6 minute read
0

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

Short description

Before you get started, keep the following in mind:

  • Fargate and Fargate Spot capacity providers don't need to be created. They are available to all accounts and only need to be associated with a cluster to be available for use.
  • Fargate Spot capacity provider can be associated with an existing cluster using ECS PutClusterCapacityProviders API and ECS put-cluster-capacity-providers CLI command reference. Adding Fargate Spot capacity provider to an existing cluster is not supported using the AWS Management Console.
  • Fargate and Fargate Spot capacity providers are reserved and can't be deleted. You can disassociate them from a cluster using the PutClusterCapacityProviders API.
  • Fargate Spot requires that your task use platform version 1.3.0 or later (for Linux) and Fargate Spot capacity provider is not supported for Windows containers on Fargate.

Resolution

Note: If you receive errors when running AWS CLI commands, make sure that you’re using the most recent AWS CLI version.

Create, associate, and disassociate Fargate Spot capacity provider with your cluster

Fargate Spot capacity providers are automatically associated with the cluster when they are created from the Amazon ECS console using the Networking only cluster template. For more information, see Creating a cluster for the Fargate launch type using the new console.

Associate Fargate Spot capacity provider with your cluster

If the Fargate Spot capacity provider isn't already associated, you can associate the capacity provider with your cluster using the PutClusterCapacityProviders API, or the following AWS CLI 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: To avoid overwriting your capacity providers, include all your existing capacity providers and the capacity provider strategy in the preceding command. Capacity providers that aren't included in the command are disassociated from the cluster.

Dissociate Fargate Spot capacity provider with your cluster

Any existing capacity providers that are associated with the cluster, and that are omitted from a PutClusterCapacityProviders API call, will be disassociated with the cluster. To disassociate the Fargate Spot capacity provider from your Amazon ECS cluster, run the following AWS CLI 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 preceding command includes the capacityProviders section that contains the capacity providers that are associated with your Amazon ECS cluster.

Run a task or create a service using Fargate Spot capacity provider

To run a task using a Fargate Spot capacity provider, 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 if 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>

Tracking Fargate OnDemand and Fargate Spot usage per service from Amazon CloudWatch is not currently supported. However, you can see the metric for total Fargate OnDemand and Spot usage using CloudWatch. For more information, see AWS Fargate usage metrics.

Frequently asked questions

What are some Fargate Spot best practices?

  • Fargate Spot is great for stateless, fault-tolerant workloads, but don’t rely solely on Spot Tasks for critical workloads. Instead, configure a mix of regular Fargate Tasks.
  • Handle interruptions gracefully by catching SIGTERM signals. When receiving a SIGTERM signal, it's a best practice to set StopTimeout to 120 seconds. For more information, see Graceful shutdowns with ECS.
  • Applications running on Fargate Spot must be fault-tolerant.

What happens to the tasks when the FARGATE_SPOT capacity is not available?

When the ECS scheduler is unable to launch a task due to capacity not being available, the SERVICE_TASK_PLACEMENT_FAILURE event is emitted. The Task will not get to the first stage PROVISIONING and a notice will not be displayed in the ECS Events. ECS scheduler will continue to try to launch the task. If capacity becomes available, then the SERVICE_STEADY_STATE event will emit.

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

It's not possible to have a fail back mechanism to FARGATE when there is no FARGATE_SPOT capacity available.

What is an example of how the tasks are placed when using the capacity providers in an ECS service?

For this example, there is a capacity provider strategy that contains the following two capacity providers:

Provider 1: FARGATE      |  Base:2   Weight:1
Provider 2: FARGATE_SPOT |  Base:0   Weight:3

According to this strategy, after the base value is satisfied, for every one task that runs using FARGATE, three tasks use FARGATE_SPOT. When the ECS service is created with the desired count of 5, then the following happens:

  • 2 tasks are launched using FARGATE to satisfy the base value and the remaining 3 are split between FARGATE and FARGATE_SPOT in a 1:3 ratio.
  • 3 tasks are run using FARGATE and 2 tasks in FARGATE_SPOT.

Using this same strategy, with a scale-out event that happened and the desired count is increased to 9. The ECS scheduler will see that 3 tasks have already run using FARGATE that has the base satisfied. Also, it will see that the new 4 tasks that are in PROVISIONING state are split between FARGATE and FARGATE_SPOT in a 1:3 ratio. The final 4 tasks are run using FARGATE and 5 tasks in FARGATE_SPOT.


AWS OFFICIAL
AWS OFFICIALUpdated 2 years ago