Spot Instances

Complete FinOps Guide
Chapter 3.4 Drilldown: AWS Spot Instances

Amazon EC2 Spot Instances is one of many features that enables you to optimize your cost on the AWS platform. However, unlike other mechanisms such as Savings Plans and Reserved Instances that provide discounts on commitment, Spot Instances offer lower prices for taking advantage of AWS’s unused compute capacity while it’s available. This feature makes them ideal for workloads such as big data, development and test environments, and high-performance computing.

What is an Amazon EC2 Spot Instance?

An Amazon EC2 Spot Instance is a regular EC2 instance that runs on AWS’s spare server capacity. A Spot Instance sells at a discount of up to 90% compared to an On-demand Instance. However, AWS can terminate, stop, or hibernate your spot Instance after a 2-minute notice when the spare capacity is no longer available.

Since 2018, AWS prices spot instances based on the availability of spare capacity in each availability zone and not based on a “spot market” bidding process (which gave it its name). This new model has helped its price be less volatile even though it still varies based on the availability of spare capacity.

The ruling metric that determines the supply and demand set by AWS EC2 is the Spot price. AWS reviews usage data for each instance type in each Availability Zone and adjusts the Spot gradually, taking the long-term supply and demand into account. The table below illustrates examples of the savings you can achieve for various instance types. Note that AWS adjusts these costs as demand and supply change. As a result, discounts will fluctuate over time.

AWS Spot Instances Discounts
Instance Type On-Demand Price Spot Instance Price Discount
t2.medium 0.0520 0.0156 70%
c4.8xlarge 1.9020 0.5019 74%
i3.metal 5.7920 1.7376 70%
m4.16xlarge 3.7120 1.1328 69%
x1.32xlarge 16.806 5.0418 70%

While configuring a spot instance before launch, you set a maximum price you are willing to pay for the spot instance to continue running if the spare capacity depletes. The maximum price is set to the on-demand price by default, but you can change it to be lower or even higher.

Amazon EC2 Spot Instance Features

Although Spot Instances do not provide guaranteed uptime and can be terminated with only a 2-minute notice, you can use various AWS features to manage workload availability and keep service disruptions to a minimum. Let’s take a look at those features.

Auto Scaling Integration

Spot Instances can integrate with Amazon EC2 Auto Scaling. This feature helps you manage application availability by allowing you to add or remove EC2 instances with conditions you define. With this feature, you can automatically scale your capacity up or down using a standard metric such as CPU utilization or a custom metric such as the number of sessions supported by a load balancer (as a proxy to application workload).

Amazon EC2 Fleet

You can configure Spot Instances as part of your Amazon EC2 Fleet. An EC2 Fleet contains configuration information to launch a group of instances across various types, including On-Demand, Reserved, and Spot Instances. Leveraging this capability, you can launch multiple instance types across multiple Availability Zones. For instance, you can define separate On-Demand and Spot capacity targets, the maximum budget, and specify how Amazon EC2 should distribute your various workloads across your fleet.

On-Demand Run Instances Function Integration

AWS gives you the capability to launch Spot Instances via the Runinstances API. This feature allows you to launch instances programmatically from an app or service that needs ad-hoc compute capacity to run a particular workload even though still subject to the same 2-minute notice of termination.

Integration into other AWS Services

Spot Instances also offer integration into other AWS services such as Amazon Elastic MapReduce (EMR), AWS CloudFormation, Amazon Elastic Container Services (ECS), and AWS Batch. This means that you can use a spot instance with those services as long as the application can tolerate a termination with a 2-minute notice.

When and where to use Amazon EC2 Spot Instances

Due to their lower price and variable availability, specific workloads are more well-suited for Amazon EC2 Spot Instances than others. Typically, these workloads are fault-tolerant (given the very short nature of the termination warning), ad-hoc, and require a vast amount of computing power for short periods. Spot Instances are also useful for non-critical services such as development and test environments.

Big Data and Analytics

Spot Instances provide significant cost savings for Big Data and Analytics batch-processed workloads that require acceleration and scale. Running resource-intensive applications such as machine learning and natural language processing require a vast amount of computing resources. Remember that the analytics results must be able to tolerate the sporadic termination of the supporting instances.

High-Performance Computing

High-Performance Computing (HPC) workloads that require a vast amount of processing power can also benefit from Spot Instances, as long as the application can tolerate occasional downtime due to the occasional termination of spot instances. With the integration into AWS Batch and AWS CloudFormation, computer scientists can leverage the cost savings of Spot Instances and build solutions for big compute workloads such as algorithmic trading and genome sequencing.

Development and Testing

Test and development environments are crucial components of the software development process. However, running these environments using On-Demand instances can be costly and inefficient. Spot Instances give you all the features of regular VMs at a fraction of the cost.

Since development and test servers are not production environments, high availability is typically not a business requirement. Additionally, you can leverage Continuous Integration/Continuous Development (CI/CD) solutions using EC2 Fleets and orchestration services like Jenkins to automate the build, test, and deploy process.

Containerized Workloads

Spot Instances allow you to run container clusters at scale at a fraction of the cost. As containers typically host stateless and fault-tolerant applications, they are a good fit for Spot Instances as long as the cluster configuration is designed to recover from nodes that may suddenly terminate. Furthermore, as Spot Instances have seamless integration with ECS, you can leverage this capability to run workloads at scale at a material discount compared to On-Demand Instances.

Web Services

A typical web service needs the capability to scale up or down as demand increases or decreases. Leveraging an EC2 fleet, you can architect a cost-effective, highly available solution using a combination of On-Demand and Spot Instances. Ideally, you could use On-Demand Instances for the baseline demand and then scale with Spot Instances when demand increases.

Image and Media Rendering

Image and media rendering solutions typically require vast amounts of computing resources for a short period. This requirement makes them an ideal workload for Spot Instances as you can scale as timelines and projects demand more resources. In addition, you can also leverage image and media rendering solutions such as Autodesk Maya and Redshift from the Thinkbox Marketplace, allowing you to pay a discounted price for industry-standard rendering solutions.

The Benefits of using Amazon EC2 Spot Instances in a Spot Fleet

As mentioned, the core benefit of Amazon EC2 Spot Instances is the significant discount you receive for utilizing AWS's unused capacity. However, AWS does not guarantee the availability of Spot Instances as they may stop, terminate, or hibernate your instance (you can choose which you prefer in advance) when they need capacity or when you breach your Spot Price threshold.

Leveraging the features of an Amazon EC2 Fleet can help you minimize any potential disruption. The table below outlines the differences between using a standard Spot Instance versus an Amazon EC2 Fleet.

Spot Instances versus Amazon EC2 Fleets
Instance Type Spot Instance EC2 Fleet
Launch Can only launch when the Spot Instance request is active, AWS has the capacity, and the Spot Price is below the set maximum price. Can launch immediately if there is an On-Demand or Reserved Instance in the fleet.
Capacity Can only launch if there is spare capacity in the relevant Availability Zone. Capacity restrictions are not applied if there is an On-Demand or Reserved Instance in the fleet.
Hourly Price Spot Price - significant discount. A combination of On-Demand, Spot Price, and Reserved Instance Price depending on the composition of the fleet.
Availability Irregular - dependent on spare capacity and/or Spot Price. High Availability assuming there is an On-Demand or Reserved Instance in the fleet.

How do Spot Instances differ from Reserved Instances?

Spot Instances and Reserved Instances are AWS products that allow you to reduce your Amazon EC2 service cost. However, the use cases for each product differ. For instance, Spot Instances are ideal for ad-hoc high-intensity compute workloads that are fault-tolerant and flexible in nature and development and test environments. On the other hand, Reserved Instances are better-suited for long-term stable production environments where the resource demands of the application do not fluctuate excessively.

The table below details the differences between On-Demand, Reserved, and Spot Instances.

On-Demand versus Reserved Instances versus Spot Instances
Instance Type On-Demand Reserved Spot
Launch No Restrictions No Restrictions Dependent on Capacity and/or Spot Price
Price Most Expensive Up to 72% Up to 90%
Flexibility No Restrictions Some Limitations No Limitations on Instance type or Availability Zone
Availability High Availability High Availability Interruptions possible with little warning

Getting started with Amazon EC2 Spot Instances

The process for creating, configuring, and launching a Spot Instance is almost identical to the steps you use for an On-Demand Instance. In the example below, we will configure an Ubuntu 20.04 virtual machine as a Spot Instance.

Step 1: Choose an Amazon Machine Image (AMI)

Search and select the Amazon Machine Image you want to configure as a Spot Instance.

Select your Spot AMI

Step 2: Choose an Instance Type

Select an Instance Type and then click on 'Next: Configure Instance Details'

Select the instance type for your Spot instance

Step 3: Configure Instance Details

The AWS Configure Instance Details page is where you can choose a Spot Instance instead of leaving it as a default On-Demand Instance. As you can see in the image below, selecting the 'Request Spot Instances' checkbox provides the Spot Price for the relevant Availability Zones. It also provides an optional field to enter the maximum price you are willing to spend per hour. AWS will cost your Spot Instance at the stated Spot Price if you do not enter a maximum price. However, if you specify a price, then AWS will keep your instance running until its current Spot Price exceeds the maximum price you set.

Finally, the page also gives you the option to make your instance persistent. AWS will shut down your Spot Instance if you select this option. If the platform needs capacity or the Spot Price exceeds the maximum price, it will perform the shutdown action. If you do not choose this option, AWS will terminate your Spot Instance when it needs capacity or when the Spot Price exceeds the maximum price. Once you have made your selection, you can continue configuring your virtual machine like a regular On-Demand instance.

Configure your Spot instance details

Monitoring and evaluating your AWS Spot Instances

As AWS cannot guarantee the high availability of Spot Instances, tracking when your Spot Instances run and terminate is vital. AWS offers three features that assist you in tracking and controlling any Spot Instance interruptions.

Termination Notices

Amazon EC2 provides a two-minute warning before interrupting a Spot Instance. However, this event only applies to shut down or termination events, as hibernation events commence immediately. Amazon CloudWatch is capable of detecting this event and issues a termination notice. Using CloudWatch events, you can configure a rule to action further processes such as starting up an On-Demand Instance when an interruption is about to occur.

Persistent Requests

In addition to termination notices, you can also use persistent requests to keep your Spot Instance running while you launch an On-Demand Instance. AWS keeps a persistent Spot Instance request active until it expires or you cancel it. You can track the state of your Spot Instance via its Spot request status.

Block Durations

Another feature that allowed you to manage any potential Spot Instance interruptions was block durations. It enabled you to specify a duration requirement when requesting a Spot Instance. However, this feature known as Spot blocks is no longer available to new customers from 1 July 2021.

Conclusion

Amazon EC2 Spot Instances allow you to run virtual machines at a fraction of a standard On-Demand Instance cost. However, as the pricing depends on using spare Amazon EC2 capacity, AWS can interrupt a Spot Instance workload by shutting it down, terminating, or setting the virtual machine to hibernate. Nevertheless, Spot Instances are well suited for particular workloads such as Big Data and Analytics, High-Performance Computing, and development and testing environments. They also integrate well with other AWS services, including auto-scaling, EC2 fleets, functions, and batches. This integration allows you to build solutions that deliver high availability while enabling you to continue benefiting from the lower Spot Instance cost.

Although Spot Instances have their limitations, using them in combination with Reserved Instances and AWS Savings Plans allows you to build an effective cost optimization strategy. Using a combination of these features, you can leverage the compute power you need from the AWS platform while aligning your architecture with the cost optimization pillar of the AWS Well-Architected Framework.

Continue Reading this Series