Introduction

Recently, my team and I have decided to come up with the strategy to reduce our AWS costs. For infrastructure’s computational cost, one thing came to our attention - Fargate Spot. This capability provides CPU and RAM resources at significantly lower prices compared to original Fargate option, so we decided to check it out.

Table of prices for Fargate and Fargate Spot, showing the vCPU per hour and GB per hour being for around 70% cheaper in favor of Fargate Spot.
Pricing comparison for Fargate and Fargate Spot (*on a day of blog publishing)

About

In 2017 AWS introduced a new service called Fargate. This service provides easier setup and management of containerized infrastructure for ECS and EKS. Since then, many companies have chosen Fargate option to create new clusters. Soon with appearance of Fargate Spot each cluster could now facilitate between two capacity providers: Fargate and Fargate Spot.

List of capacity providers that are available for Fargate clusters, displaying Fargate and Fargate Spot.
List of capacity providers that are available for Fargate clusters

The idea behind “Spot” is that the most of the time many physical machines (i.e. EC2 instances) in data centers has some spare resources unutilized. This spare CPU and RAM on idle machines can now be put to work with Fargate Spot capacity providers. Each task in your cluster that has Fargate Spot setup will run on AWS spare resources.

Depicting arbitrary level of consumption of EC2 instances and level of spare resources
Arbitrary levels of EC2 consumption and spare resources in AWS data center

However, Fargate Spot has its downside. If any of the spare resources become required by the actual host that has higher priority, the users will get warning notification that the task and its container will be gracefully stopped within 2 minutes. As most of the time these tasks run in a cluster that has redundancy enabled (i.e. number of expected tasks running), the cluster will spin another container instance on the next available spare resource.

Due to this, our team decided not to utilize Fargate Spot on our production instances, but instead use it on development environments where accidental downtime, for a period of a new container spin, can be tolerated.

Setup

In order to use Fargate Spot you are necessary to create ECS or EKS cluster.

Choosing Fargate cluster template among other two displaying EC2 Linux and EC2 Windows.
Choose Fargate cluster template

Once the cluster is created, you can check Capacity Providers tab to see a list of assigned providers.

List of capacity providers that are available for Fargate clusters, displaying Fargate and Fargate Spot.
List of capacity providers that are available for Fargate clusters

In AWS dashboard visit Task Definitions and create a new task definition. Make sure to check Fargate option.

Selection in task definition form to choose Fargate compatibility option
Choose Fargate option for Task Definition

Now go back to your cluster and create a new service. Here you can choose which capacity providers your cluster will use.

Showing the list of capacity providers in cluster details tab. Shows Fargate and Fargate Spot.
Add capacity providers for the cluster

Once your task is up and running, you can check the attributes to make sure your container is using Fargate Spot capacity provider.

Details of running task, showing Fargate Spot provider is used
Task details show usage of Fargate Spot

Results

Our platform consists of more then a dozen containers. For the purpose of continues delivery, we employ three different environments for development and testing, which comes to a total number of 40-50 containers. Our costs have dropped by ~65% compared to our previous setup with Fargate as a default capacity provider.

A pilar chart showing daily prices and decline in around 65%.
Our ECS cost reduction during November

In conclusion, we can say Fargate Spot is a good option for applications that perform short running tasks that can rarely be affected by downtime interruption. Also, in a case of multiple redundancy tasks, you can choose to run your main task as Fargate while other tasks can use Fargate Spot. This way you can achieve lower costs and still preserve high availability for your cluster.

References