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.
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.
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.
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.
In order to use Fargate Spot you are necessary to create ECS or EKS cluster.
Once the cluster is created, you can check Capacity Providers tab to see a list of assigned providers.
In AWS dashboard visit Task Definitions and create a new task definition. Make sure to check Fargate option.
Now go back to your cluster and create a new service. Here you can choose which capacity providers your cluster will use.
Once your task is up and running, you can check the attributes to make sure your container is using Fargate Spot capacity provider.
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.
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.