AWS Fargate cost optimization by using Spot instances

A

Hi, everybody. At 1st article we discussed how AWS Fargate costs can be optimized by using ARM CPU architecture. Let’s continue our cost optimization AWS Fargate journey 🙂

Current part would be devoted mostly to AWS Fargate Spot, so it would be good to define what is it at the beginning. There are 2 capacity providers that can be used for running docker containers at AWS Fargate services:

  • Fargate
  • Fargate Spot

Fargate Spot allows to run interruption tolerant Amazon ECS tasks at a discounted rate compared to the AWS 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.

In most cases you will find information that Spot option is better to use for test/dev environment, but I want to make a rather contrversional statement – we can use the Fargate spot in production also.

We can create hybride system and mix 2 provisers at once. It is not possible to use current trick always – but from my practice appears that works rather well in most of cases. If you are interested how to do it at terraform in details – please, join my udemy course “AWS Fargate DevOps: Autoscaling with Terraform at practice“. Here I will put only some part of code for understanding general idea:

resource "aws_ecs_service" "ecs-app-service" {
.... most of code is omitted for readability

  capacity_provider_strategy {
    capacity_provider = "FARGATE_SPOT"
    weight = 2
  }

  capacity_provider_strategy {
    capacity_provider = "FARGATE"
    weight = 1
    base   = 1
  }
.....
}

Let’s assume that to process loading we need 3 containers, so we set max ECS capacity to be equal 3 accordingly. The code above means, that:

  • for 1 Fargate container we want to have 2 spot containers;
  • base option guarantees that “Fargate” provider would be chosen in case we have only one container to be running;
  • spot provider would be used in case we need 2 or 3 containers;
  • in case 1 spot container would be killed by AWS – some time 3-5 min will be required for AWS to deploy a new container at spot resources.

As we always have some reserve, it is quite possible that 2 containers will be able to deal with short overloading. For sure all depends on your exact case and requirements – but it appears to be acceptable at practice rather often. And if it is your case – then you are a lucky guy.

You may ask: “And if 2 containers would be killed at once?” The probability of it is indeed rather low, though a lot depends on AWS region which you operate on.

Let’s calculate such a scenario: 3 fargate containers with next configuration 1 fargate + 2 spot versus 3 EC2 instances, taking into consideration savings plans. There is one pitfall here – remember, we cant use fargate spot for arm architecture – ehh, unfortunately:

Let’s return to aws compute pricing page:

Using data from screen above, we can calculate final price for AWS Fargate 1CPU, 2GB RAM, with 1 year saving plan using ARM x86 architecture:

0.0363168 per vCPU per hour × 24 hours × 30 days = 26,1481 USD
0.0039858 per GB per hour × 24 hours × 30 days × 2 GB = 5,7396 USD
Which give us almost 32 USD per month

In case fargate spot we are getting next calculations (prices are taken from “AWS Fargate pricing page” at September 2024):

0.01426032 per vCPU per hour × 24 hours × 30 days = 10,2674 USD
0.00156508 per GB per hour × 24 hours × 30 days × 2 GB = 2,2537 USD
Which give us 12,52 x 2 = 25$ for 2 spot instances per month

So, in summary for 1 fargate + 2 spot configuration we are getting 57$ per month.

In case EC2 all is simple. As you remember for previous article we already know price for the one 3a.small EC2 instance with 1 year upfront payment using EC2 saving plan. It is 11.97$ per month. So, in case 3 instances we are getting:

11.17 USD x 3 EC2 = 33 USD

So, final ration at Fargate/EC2 cost would be:

57/33 -> 1,7272 ratio versus initial 2.3 

Looks not so bad 🙂

Let’s try to accumulate all we have discussed within the last 2 articles + add some pepper in the format of additional tricks to make a final understanding when it is worth using AWS Fargate and when it is a bad choice. But let’s do it at next article (link will appear here when article would be ready).

If you are not ready to wait, or prefer to pass all material in a fast and more convenient way – then welcome to my course, were you can pass all material in a fast and convenient way. Here you can find coupon with discount, which I am updating regularly in the middle of every month.


architecture AWS cluster cyber-security devops devops-basics docker elasticsearch flask geo high availability java machine learning opensearch php programming languages python recommendation systems search systems spring boot symfony