AWS Fargate is a popular cloud service that lets you run containers in a serverless model—without having to manage the underlying server infrastructure.
In this article, you’ll see how to use Fargate with a twist: running Fargate on EC2 spot instances and getting substantial discounts, for workloads that are not sensitive to the occasional interruption.
What are AWS Spot Instances?
The AWS Spot Instance purchasing option lets you buy unused Amazon Elastic Compute Cloud (Amazon EC2) capacity at a discounted rate. You can acquire Spot Instances through a bidding process, in which you specify the maximum price per hour you are willing to pay per instance.
When an instance becomes available at your target price, you get the instance for a discounted price. Once the price exceeds your bid or equals on-demand pricing, the instance is stopped. When the price decreases again, the system reactivates the instance. This process repeats until you cancel your bid.
How AWS Fargate Pricing Works
AWS Fargate, part of the AWS serverless ecosystem which includes popular tools like AWS Lambda, lets you run containers on serverless infrastructure. You can use Fargate to provision your Amazon Elastic Container Service (Amazon ECS) and Amazon Elastic Kubernetes Service (Amazon EKS) without having to manage servers and infrastructure.
Like other AWS services, Fargate provides an on-demand pricing option that allows you to provision resources as needed. You do not need to pay any upfront fees. Instead, you pay for the virtual centralized processing unit (vCPU) and memory resources your containerized application consumes.
Fargate bills you per second, starting at the minimum duration of one minute. The starting time begins when you start downloading the container image and ends at the termination time of the Amazon EKS Pod or the Amazon ECS Task. Additional fees apply for data transfers and operations performed by other AWS services.
AWS Fargate Spot
Fargate Spot is a new, built-in Fargate feature that lets you run interruption-tolerant EC2 Tasks at discounts of up to 70% off the market price. This feature works just like AWS Spot Instance—you get a significant discount for using spare capacity.
Whenever there is available capacity for Fargate Spot, you can launch tasks according to specified requests. The system notifies you two minutes before AWS needs this capacity back, and then tasks running on these instances are interrupted.
When Fargate Spot capacity is no longer available, Fargate scales down tasks running on this capacity and maintains all regular tasks. To ensure continuity, prefer running mainly fault-tolerant and parallelizable workloads, such as genomic processing and image rendering, on Fargate Spot.
To use Fargate Spot, you need to create a Service Autoscaling policy, in which you define the minimum amount of regular tasks you want running during all times and tasks allocated to Fargate Spot capacity. Once capacity is available, the Scheduler launches tasks according to your predefined requirements.
Tutorial: Launch ECS Fargate Spot Task Using AWS Management Console
Here is a quick walkthrough explaining how to launch an EC2 Fargate Spot Task via the AWS Console:
1. Go to the Amazon ECS console web page
2. Go to the navigation menu and select a Region
3. Go to the navigation pane and choose the Clusters option
4. In the Clusters page, choose the Create Cluster option
5. Choose the block that creates a Networking only Cluster
The “networking only” cluster option lets you launch a cluster with a new Amazon Virtual Private Cloud (Amazon VPC), which will be used for your Fargate tasks.
Once you set this up, your cluster will automatically be associated with FARGATE and FARGATE_SPOT capacity providers (see the image below).
6. From the top right-hand side, choose Update Cluster. This option lets you define a capacity provider strategy.
7. Create a Task Definition. You can choose some Task definitions from this repository. Here is how to set this up:
- Locate the Task Definition link in the left navigation panel.
- Choose the Create a New Task Definition option.
- Choose a Fargate launch type.
- In the bottom of the page, locate the Configure via JSON button.
- Delete the pre-populated JSON entry.
- Add the sample Fargate WebApp task definition provided in the image below to the now empty JSON entry.
- Click the Save button.
- Click the Create button.
8. Run the Task definition. Here is the process you should follow:
- Select your newly created Task definition
- Choose the Action, Run Task option
- Define the amount of Tasks you want to run (the image below defines 10 tasks)
- Configure the VPC and security groups
- Click the Run Task button
The image above starts 10 tasks—8/10 are launched on FARGATE_SPOT and 2/10 are launched on FARGATE with a 4:1 ratio. To see the ratio, you can choose any Task, go to the Details tab, and locate the “Capacity provider” value for each task.
Amazon Fargate is a powerful innovation that allows many developers to run containers without having to manage the underlying infrastructure. The convergence between containers and serverless is making an impact many cloud native development projects.
In this article, I showed how to use spare capacity on Amazon EC2 to obtain significant discounts on Amazon Fargate. I discussed spot pricing concepts on Amazon EC2 and Fargate, and provided a brief tutorial that shows how to create a Fargate EC2 Spot Task. I hope this will be of help as you start leveraging serverless infrastructure in your organization.