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

Last updated: 2021-04-05

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:

  • You can associate Fargate Spot capacity providers with Amazon ECS clusters using only the AWS Command Line Interface (AWS CLI) or the Amazon ECS API.
  • You can't delete Fargate or Fargate Spot capacity providers.
  • You can disassociate Fargate or Fargate Spot capacity providers from clusters.

Note: Fargate Spot capacity is suitable for running fault-tolerant tasks because it runs on spare capacity. AWS can take back that spare capacity when needed. In this scenario, your tasks are interrupted with a two-minute warning. For more information, see AWS Fargate capacity providers.

Resolution

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

Associate your cluster with a Fargate Spot capacity provider

1.    Create a new Fargate cluster and associate a Fargate Spot capacity provider with your cluster using either the AWS Management Console, or the following AWS CLI command:

$ aws ecs create-cluster --cluster-name your_Fargate_cluster_name --capacity-providers FARGATE_SPOT --region your_region

Note: If you use the AWS Management Console with the Networking only cluster template, then you can create a Fargate cluster that automatically has the Fargate cluster and Fargate Spot capacity provider associated with it.

2.    (Optional) To disassociate the automatically created Fargate Spot capacity provider from your Amazon ECS cluster, run the following command:

$ aws ecs put-cluster-capacity-providers --cluster your_FargateCluster_name --capacity-providers FARGATE_SPOT --default-capacity-provider-strategy your_existing_default_capacity_provider --region your_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 your_cluster_name --region your_region

Note: The output of the preceding command includes the Capacity providers section. This section contains the capacity providers that are associated with your Amazon ECS cluster.

Add or associate an existing Amazon ECS cluster with a Fargate Spot capacity provider

Note: You can't add or associate a Fargate Spot capacity provider to an existing cluster using the AWS Management Console. Use the AWS CLI command line instead.

To add or associate an existing cluster with a Fargate Spot capacity provider, run the following command:

$ aws ecs put-cluster-capacity-providers --cluster your_Fargate_cluster_name --capacity-providers FARGATE_SPOT your_existing_capacity_provider1 --default-capacity-provider-strategy your_existing_default_capacity_provider_strategy --region your_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.

Handle Fargate Spot capacity termination notices

When a task running on a Fargate Spot capacity provider is stopped due to a spot interruption, you receive a warning two minutes before the task is forcefully stopped by Amazon ECS. The warning is sent as a task change event to Amazon EventBridge and as a SIGTERM signal to the running task. If the task is part of a service, then the service scheduler continuously tries to launch additional tasks on the Fargate Spot capacity provider. The service scheduler tries these additional launches to see if capacity is available after receiving an interruption signal.

1.    Use an event pattern to create an EventBridge rule for Amazon ECS task state change events.

2.    To give a container time to exit gracefully, set the StopTimeout value to 120 seconds or less in the Containers Definitions section of your task definition.

Note: The default StopTimeout value is 30 seconds. The SIGTERM signal has to be received from within the container to perform cleanup actions. If the signal fails to get processed, the task receives a SIGKILL signal after the configured StopTimeout value. A signal failure can result in data loss or corruption.

Here's an example of a task state change TerminationNotice event:

{
  "version": "0",
  "id": "9bcdac79-b31f-4d3d-9410-fbd727c29fab",
  "detail-type": "ECS Task State Change",
  "source": "aws.ecs",
  "account": "111122223333",
  "resources": [
    "arn:aws:ecs:us-east-1:111122223333:task/b99d40b3-5176-4f71-9a52-9dbd6f1cebef"
  ],
  "detail": {
    "clusterArn": "arn:aws:ecs:us-east-1:111122223333:cluster/default",
    "createdAt": "2016-12-06T16:41:05.702Z",
    "desiredStatus": "STOPPED",
    "lastStatus": "RUNNING",
    "stoppedReason": "Your Spot Task was interrupted.",
    "stopCode": "TerminationNotice",
    "taskArn": "arn:aws:ecs:us-east-1:111122223333:task/b99d40b3-5176-4f71-9a52-9dbd6fEXAMPLE",
    ...
  }
}

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

Important: The Fargate Spot capacity provider requires the AWS Fargate platform version 1.3.0.

Tip: If want to exit a standalone task gracefully when receiving a SIGTERM signal, it's a best practice to set StopTimeout to 120 seconds.

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

aws ecs run-task \
   --capacity-provider-strategy capacityProvider=FARGATE_SPOT,weight=1 \
   --cluster your_FargateCluster_name \
   --task-definition your_task-def-family:revision \
   --network-configuration "awsvpcConfiguration={subnets=[string,string],securityGroups=[string,string],assignPublicIp=string}" \
   --count your_integer \
   --region your_region

Create a service using the Fargate Spot capacity provider

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

aws ecs create-service \ 
   --service-name your_FargateService_name \
   --capacity-provider-strategy capacityProvider=FARGATE,weight=1 capacityProvider=FARGATE_SPOT,weight=1 \
   --cluster your_FargateCluster_name \
   --service-name your_FargateService \
   --task-definition your_task-def-family:revisio> \
   --network-configuration "awsvpcConfiguration={subnets=[string,string],securityGroups=[string,string],assignPublicIp=string}" \
   --desired-count your_integer \
   --region your_region

Confirm 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 your_cluster_name --tasks your_task_ID --region your_region

Did this article help?


Do you need billing or technical support?