• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Codemotion Magazine

Codemotion Magazine

We code the future. Together

  • Discover
    • Live
    • Tech Communities
    • Hackathons
    • Coding Challenges
    • For Kids
  • Watch
    • Talks
    • Playlists
    • Edu Paths
  • Magazine
    • AI/ML
    • Backend
    • Frontend
    • Dev Life
    • DevOps
    • Soft Skills
  • Talent
  • Partners
  • For Companies
Home » DevOps » Cloud » Working with Spot Instances on AWS Fargate
Cloud

Working with Spot Instances on AWS Fargate

In this article, you'll see how to use AWS Fargate on EC2 spot instances for workloads that are not sensitive to the occasional interruption.

Last update May 14, 2021 by Gilad David Maayan

Working with Spot Instances on AWS Fargate

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.

Table Of Contents
  1. What are AWS Spot Instances?
  2. How AWS Fargate Pricing Works
  3. AWS Fargate Spot
  4. Tutorial: Launch ECS Fargate Spot Task Using AWS Management Console
  5. Conclusion

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.

Loading the player...

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.

Conclusion

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.

Loading the player...
facebooktwitterlinkedinreddit
Share on:facebooktwitterlinkedinreddit
How Augmented Reality (AR) Enables Remote Troubleshooting
Previous Post
Population Shift Analysis: Monitoring Data Quality with Popmon
Next Post

Related articles

  • The AWS DevOps Toolset: CI/CD, Microservices, and More
  • 7 Mitigation Strategies to Address IoT Security Risk
  • Terraform AWS: Files and Variables
  • Building a Stateful Cloud App on Nimbella or AWS
  • Terraform AWS: Up and Running in 25 Minutes
  • How Deliberate Learning Will Grow Your CTO Career
  • Amazon, Google, and Microsoft Take their Clouds to the Edge
  • AWS vs. Azure Cost Comparison [2022]
  • Which Cloud Adoption Strategy Should I Follow?
  • Tackling Complex Cloud Architectures

Primary Sidebar

Lessons Learned in the Cloud

Dive deep into this whitepaper dedicated to the Cloud and discover more about Kubernetes.

OVH Logo Lessons learned in the cloud

Codemotion Milan 2022

https://youtu.be/ym8iilUy_U0

Latest Articles

AWS sv Azure

AWS vs. Azure Cost Comparison [2022]

Cloud

Which Cloud Adoption Strategy Should I Follow?

Cloud

php cover

PHP for Web Development in 2022: Dead, Alive, or Missing in Action?

Web Developer

Golden Rules for Combining Front-end Security and UI

Frontend

Discover More

  • Survey Reveals Four Types of Developers: Which One Are You?
  • Don’t Miss the Next Edition of our Devs Book Club with Matteo Pagani!
  • JavaScript: A First Look at the Type Annotation Proposal

Footer

  • Magazine
  • Events
  • Community
  • Learning
  • Kids
  • How to use our platform
  • Contact us
  • Become a contributor
  • About Codemotion Magazine
  • How to run a meetup
  • Tools for virtual conferences

Follow us

  • Facebook
  • Twitter
  • LinkedIn
  • Instagram
  • YouTube
  • RSS

DOWNLOAD APP

© Copyright Codemotion srl Via Marsala, 29/H, 00185 Roma P.IVA 12392791005 | Privacy policy | Terms and conditions

Follow us

  • Facebook
  • Twitter
  • LinkedIn
  • Instagram
  • RSS

DOWNLOAD APP

CONFERENCE CHECK-IN