Cloudthread Y Combinator
April 27, 2022

AWS Fargate Pricing: A Primer

Learn how aws fargate pricing works and when to use it instead of EC2 instances. You'll see a few ways to reduce your bill too.

When most people hear the term "serverless," they think of lambdas. While lambdas are a serverless service, the concept of serverless is more centered around only paying for what you use, elastically scaling effortlessly, and having someone else take care of mundane management tasks like patching an operating system.

In this guide, you'll see how to navigate billing for AWS's serverless compute service for containers: Fargate. You'll get to know the cost difference between Fargate and EC2, and you'll see a few methods to reduce that cost.

AWS Fargate is a serverless compute engine for containerized workloads

What Is Fargate?

AWS Fargate is a serverless compute engine for containerized workloads. It sits behind Elastic Container Service (ECS) and Elastic Kubernetes Service (EKS) and manages a collection of EC2 instances for you based on how much CPU power and RAM you specify.

How Does AWS Fargate Pricing Work?

Since Fargate is a serverless service, it charges you only for the resources that you use. Fargate calculates how much to charge you based on how much vCPU capacity and RAM you use. You can find the current price for these values on the Fargate pricing page.

When you look up the charges for CPU and memory capacity, you'll see they're listed as per hour. In reality, like EC2, you're charged by the second, but hourly numbers are easier to make estimates with.

Being charged by the second does have one little gotcha on Fargate. Every time you spin up a Fargate task, you'll be charged a minimum of one minute of compute time.

What Makes AWS Fargate Pricing Different From EC2?

Fargate costs more than EC2. To see this, let's break down how much a t3.xlarge instance would cost through Fargate. This instance type has four vCPUs and 16 GB of memory. Using the Fargate pricing page, we can figure out how much that would cost per hour.

4($0.04048) + 16($0.004445) = $0.23304 per hour

It would cost about 23 cents per hour to run a t3.xlarge with Fargate. That's 140% of EC2's current price of $0.1664 per hour. So, if Fargate is so much more expensive, why bother using it? What benefit do you get for that 40% premium?

The benefit is that AWS manages all of the instances for you. To see if the premium is worth it to you, calculate how much the work of patching and upgrading the instances would cost. If that cost is more than Fargate's premium, then it's actually cheaper to use Fargate.

Potential Pitfalls of AWS Fargate Pricing

When you create a task definition for ECS, you can specify the amount of memory and number of vCPUs that you'll need from Fargate. As you know, there are a lot more factors that play into the performance of an application than just memory and the number of vCPU cores.

If you have a task to train a machine learning model, it would benefit a lot from an EC2 instance with a ridiculous GPU. Unfortunately, you can't tell Fargate this. So, you could end up paying more per second to run your application on a machine that will take longer to finish.

Strategies to Optimize Costs Using AWS Fargate

Before you start spending time optimizing costs, it's a good idea to enable cost allocation tags for all of your projects. If you tag all of your resources with their project name, then you'll be able to generate a report at the end of the month that shows exactly how much you spend on each resource for a specific project.

If you've made it this far then you've probably decided the total cost of Fargate makes it more cost effective than managing a bunch of EC2 instances yourself. Now let's see how you can reduce your Fargate costs and reduce your bill a bit more.

Use Compute Savings Plans

The easiest way to reduce Fargate costs is to use a compute savings plan. If you know that you'll be using a minimum dollar amount on compute resources (EC2, Fargate, and Lambda), then you can make a pact with AWS to spend that amount and you'll get a discounted rate. To see exactly how much you'll save, refer to AWS's Compute Savings Plan page.

Graviton processors run faster than their x86 counterparts, and the price per vCPU and GB of memory are about 20% cheaper, too.

Use Graviton2 Processors

AWS has developed its own ARM processor, called Graviton. AWS designed the processors to deliver the best price-performance for cloud computing workloads. Graviton processors run faster than their x86 counterparts, and the price per vCPU and GB of memory are about 20% cheaper, too.

Right now you can only use ARM processors with Linux-based containers. So, if you need to run a Windows-based container, you're out of luck.

Use Fargate for Short-Running Tasks

Since Fargate charges based on execution time, you can lower your bill by only using Fargate for short-running tasks. Use it for data processing and spin up an EC2 instance (or better yet another serverless service) when you need to deploy a web server or REST API.

Use Spot Instances

What does AWS do with EC2 instances that aren't being used? It lets customers bid on them at a steeply discounted rate. This recoups some of the cost of powering the machine while it's waiting for someone to spin up an on-demand instance.

There's a catch to using these spot instances, though. If AWS gets someone willing to pay full price for the spot instance that you're using, it'll give you a two-minute warning and you'll need to clean up whatever you're working on within that time window.

Fargate offers a similar deal, called Fargate Spot. Instead of bidding on what price you pay for the instances, you just pay the current discounted price. The price fluctuates based on trends in supply and demand, and the current price can be seen on the AWS Fargate pricing page.

If you're running a workload on Fargate that can handle interruptions, then using Fargate Spot can get you up to a 70% discount. The best way to make this work is for you to use a queue of items to process so that if your process gets interrupted it can pick right back up when more Fargate Spot capacity becomes available.

Use Fargate for Testing

Fargate works well for development and testing environments since it's serverless. Deploy your containers to ECS using Fargate when you need to test a new feature. Delete the ECS task when you finish validation, and ECS will clean up the compute resources for you.

Migrate to EC2

My final suggestion for reducing your Fargate bill is to not use it. You can avoid Fargate's premium if you already have a DevOps team. The trade-off is that you'll eat up time that your DevOps team could be doing something else.

If you don't have a DevOps team in place now, you can use Fargate to get up and running quickly. Later, when you get the capacity to manage a bunch of EC2 instances, you can migrate over to cut costs.

Fargate offsets the extra cost by removing your need to manage a collection of EC2 instances.

Conclusion

Fargate brings the serverless model to running containerized workloads. The only downside is that it charges a premium over EC2. Fargate offsets the extra cost by removing your need to manage a collection of EC2 instances. Reduce costs further by using a savings plan and ARM processors.

Reducing the amount of time you're using Fargate can reduce your bill, too. Use Fargate for short tasks on spot instances or to run testing and development environments.

Finally, if you can support EC2 instances, you can always migrate to that to reduce your AWS bill.

Make cloud costs a first class metric for your engineering organization.
Copyright © 2024 CloudThread Inc.
All rights reserved.
Copyright © 2024 CloudThread Inc. All rights reserved