Fargate Spot in CloudFormation

TOC

Fargate Spot in CloudFormation

With Fargate Spot, you can use Fargate at a lower price than usual.

With Fargate Spot you can run interruption tolerant Amazon ECS tasks at a discounted rate compared to the Fargate price. Fargate Spot runs tasks on spare compute capacity. When AWS needs the capacity back, your tasks will be interrupted with a two-minute warning.

AWS Fargate capacity providers

In this article, we will checking the basic actions of Fargate Spot.
We will also check how to always execute tasks on Fargate Spot.

Environment

Diagram of Fargate Spot in CloudFormation

Prepare a public subnet and create a Fargate type ECS.
Configure capacity providers on the ECS cluster and distribute the infrastructure on which the ECS tasks will act in the following proportions

  • Normal Fargate: 1
  • Fargate Spot: 2

In this case, a total of seven tasks will be created.
However, at least one of the tasks will be running on Fargate.

CloudFormation Template Files

The above configuration will be built using CloudFormation.
The CloudFormation templates are located at the following URL

https://github.com/awstut-an-r/awstut-fa/tree/main/090

Explanation of key points of the template files

This page focuses on matters related to how to configure Fargate Spot.

For basic information about Fargate, please refer to the following page

あわせて読みたい
Introduction to Fargate with CloudFormation 【Configuration for Getting Started with Fargate with CloudFormation】 AWS Fargate is a serverless service that allows you to run Docker containers.In this i...

ECS Cluster

Resources:
  Cluster:
    Type: AWS::ECS::Cluster
    Properties:
      CapacityProviders:
        - FARGATE_SPOT
        - FARGATE
      ClusterName: !Sub "${Prefix}-cluster"
      DefaultCapacityProviderStrategy:
        - CapacityProvider: FARGATE_SPOT
          Weight: 2
        - Base: 1
          CapacityProvider: FARGATE
          Weight: 1
Code language: YAML (yaml)

Configure capacity provider settings for the ECS cluster.
The capacity provider settings configured for the cluster will be the default settings for the entire cluster.

There are two properties that can be used to configure capacity providers.

The first is the CapacityProviders property, which specifies the cluster’s capacity providers, i.e., the infrastructure on which ECS tasks are executed.
In this case, we will run the task on both Fargate and Fargate Spot, so we specify “FARGATE” and “FARGATE SPOT”.

The second is the DefaultCapacityProviderStrategy property.
Specify the percentage of tasks to be launched at the Fargate and Fargate Spot for this property.
The details are explained on the official AWS page, but the Weight property specifies the percentage, and the Base property specifies the minimum number of tasks to be launched.
In this case, the ratio of Fargate Spot to Fargate is 2:1, so the Weight and Base properties are “2” and “1” respectively.
And since we want Fargate to execute at least one task, we specify “1” for the Base property on the Fargate side.

https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/cluster-capacity-providers.html

ECS Service

Resources:
  Service1:
    Type: AWS::ECS::Service
    Properties:
      #CapacityProviderStrategy:
      Cluster: !Ref Cluster
      DesiredCount: 7
      #LaunchType: FARGATE
      TaskDefinition: !Ref TaskDefinition1
      ServiceName: !Sub "${Prefix}-service1"
      NetworkConfiguration:
        AwsvpcConfiguration:
          AssignPublicIp: ENABLED
          SecurityGroups:
            - !Ref ContainerSecurityGroup
          Subnets:
            - !Ref PublicSubnet1
Code language: YAML (yaml)

If the default capacity provider is configured on the cluster, no configuration is required on the ECS service side.
Therefore, the CapacityProviderStrategy and LaunchType properties are not used.
If both properties are used, the contents here take precedence.

Architecting

Using CloudFormation, we will build this environment and check the actual behavior.

Create aCloudFormation stacks and check resources in stacks

Create CloudFormation stacks.
For information on how to create stacks and check each stack, please refer to the following page

あわせて読みたい
CloudFormation’s nested stack 【How to build an environment with a nested CloudFormation stack】 Examine nested stacks in CloudFormation. CloudFormation allows you to nest stacks. Nested ...

After checking the resources in each stack, information on the main resources created this time is as follows

  • ECS cluster: fa-090-cluster
  • ECS service: fa-090-service

Confirm the created resources from AWS Management Console.
Confirm the ECS cluster.

Detail of Fargate 1.

Indeed, a cluster has been created.

Next, check the ECS service.

Detail of Fargate 2.

The service has indeed been created, and the configuration status of the capacity provider is shown.
Specifically, we can confirm the ratio of Fargate Spot to Fargate (2:1) when ECS tasks are launched, and that at least one task is executed on Fargate.

Check the ECS tasks that are running.

Detail of Fargate 3.

We can see that there are indeed 7 tasks running, as per the desired number of tasks.

Let’s check some task details.

Detail of Fargate 4.

Here is an example of a task running on Fargate.
The “Capacity Provider” value is “FARGATE”.

Detail of Fargate 5.

Here is an example of a task running on Fargate Spot.
The value of “Capacity Provider” is “FARGATE SPOT”.

In this case, seven tasks are running, and all capacity providers have been checked.
The breakdown is as follows

Capacity ProviderNumber of Tasks
Fargate Spot4
Fargate3

First, one task is executed on Fargate for the base set on the Fargate side, i.e., one task is executed on Fargate.

The remaining six tasks are allocated by the capacity provider strategy so that the ratio of Fargate Spot to Fargate is 2:1 when the ECS tasks are launched.
In other words, there are four tasks on the Fargate Spot and two on the Fargate.

The above table is a summary of the above.

(Reference) Always execute tasks on the Fargate Spot

By modifying the CloudFormation template file as shown below, the task will always be actioned on the Fargate Spot.

Resources:
  Cluster:
    Type: AWS::ECS::Cluster
    Properties:
      CapacityProviders:
        - FARGATE_SPOT
        #- FARGATE
      ClusterName: !Sub "${Prefix}-cluster"
      DefaultCapacityProviderStrategy:
        - CapacityProvider: FARGATE_SPOT
          Weight: 1
        #- Base: 1
        #  CapacityProvider: FARGATE
        #  Weight: 1
Code language: YAML (yaml)

In the CapacityProvider, the description of Fargate has been commented out.
This means that the tasks that will be actioned in this cluster will always be on the Fargate Spot.

Summary

In this article, we have checked the basic operation of Fargate Spot.

TOC