Spot Instances

Spot Instances in AWS [Complete Guide]

Hi, Bits Lovers. This post will dive deep into Spot Instances and how we can time our workloads utilizing Spot Instances and Spot Fleets. So we’re just going to do a review of what Spot Instances are. We’re then going to go forward and look at our spot prices. Next, we’re going to examine what Spot Blocks are. After that, we will discuss different use cases and verify what happens when our Spot Instances are terminated. We’ll then look at Spot Fleets. Finally, see the Launch Pools, also at different strategies, and we’ll move on to our conclusions.

Spot Instances vs On Demand

So an Amazon EC2 Spot Instance allows you to take benefit of unused EC2 capacity in the AWS Cloud. And essentially, Spot Instances are available for up to a 90% deal compared to on-demand prices, but they come with a trick. 

When to use Spot Instances

Spot Instances use cases

Fundamentally, Spot Instances are used where you require stateless, fault-tolerant, or flexible applications. It means that you wouldn’t use it on web servers, for example, only because a web server requires to be on and running 24 hours per day. 

We would utilize it is for your applications that are big data applications, where you’ve got containerized workloads, CI/CD, where you’ve got high-performance computing, and then other test and development workloads. 

How to use Spot Instances?

Request Spot Instances

So to use Spot Instances, you must first determine your highest for it. And the AWS will provision the instance so long as the spot price is below your highest spot price. For example, let’s imagine you were prepared to expend two dollars an hour. If Amazon spot prices are 75 cents an hour, AWS will provision your instances. If it then raises to $2.10 an hour, then because your highest price is $2, your instances will either be stopped or terminated, relying on what you desire. 

Spot Instances Cost

So the hourly spot price changes depending on the capacity and the region. So it means that it’s different. Even down to the particular availability zones, it could be different. So it’s not just the region itself. It’s pretty different, relying on the capacity of the availability zones and the regions. And like mentioned before, if our spot price goes beyond the highest, you have two minutes to decide whether or not you desire to terminate or stop your instances. 

For instance, we can either stop them and resume the workloads once the spot price has moved above your highest spot price. Or we can go manually and terminate your EC2 instances. It is up to you and your scenario. 

How to avoid Spot Instance Termination?

What is Spot Blocks 

We can use Spot Blocks to prevent your Spot Instances from being terminated even if the spot price moves over your highest spot price. For example, we can now set your Spot Blocks for between 1 to 3 hours, and we’ve got a workload that definitely cannot be terminated even if the spot price drives beyond your highest spot price. We can utilize a spot block to stop those instances from being terminated or stopped for just one to six hours.

AWS Spot Instances Price History

So the wonderful thing regarding AWS is we can fetch all the historical data for Spot Instances. It means that we can split this across different operating systems against your different instance types and EC2 families types. Also, we can filter by over specific date ranges, and then it breaks it down to particular availability zones, comparing it with the on-demand price. And choose the lowest cost availability zone.

So it offers you tremendous flexibility in choosing your highest spot price simply by doing some investigation into your pricing history. Like, the screen below:

Spot Instances Price History
Spot Instances Price History

When not using Spot Instances

So where Spot Instances are not helpful for a scenario like persistent workloads. So, for instance, web servers or database servers where we’re permanently going to have a load on them. Or scenario like essential jobs because we don’t desire our Spot Instances to be stopped or terminated in a critical application or process. And like mentioned before, databases don’t apply to running on a Spot Instance because it can be terminated. 

So won’t have access to that data. You have to choose whether you need to use on-demand, reserved, Spot Instances, or dedicated hosts. So certainly don’t use Spot Instances for anything critical or where applications need persistent workloads or databases


Best Posts Related:

CI/CD Using AWS Fargate

How to Speed Up AWS S3

AWS Auto Scaling Hooks


Spot Instance Termination Workflow

Spot Instances Lifecycle
Spot Instances Lifecycle

When we make our Spot Instances, we determine the highest price when we make a spot request. So it might be 20 cents an hour. We might have 50 instances in this spot request. We then describe our launch specification. For example, what AMI is it? Linux, Amazon Linux 2. Next, we’ve got our request type, which can either be one-time or persistent. And if it’s persistent, you have to specify when the request is valid.

Once we submit our spot requests, our instances will be provisioned, and they’ll be launched as soon as the spot price goes below our highest price. So we need to make one spot request, and these instances will all be stopped or terminated according to what we configure. If, however, we have a persistent request, what is practically doing is it’s examining to catch if the request is open, active, or disabled as opposed to failed, canceled, or closed.   

Spot Instances Request State
Spot Instances Request State

Let’s analyze a vital scenario that could happen. First, you need to understand: if you’ve got a spot request that’s open and persistent, you can’t terminate your instances because your spot request is going to examine at the spot price, and it’s will communicate that your request that you created with 50 instances, there’s currently none in there, and the spot price is still more inferior than your highest price. So, what will happen is to keep re-provisioning those instances. So you get stuck in this circle.   

How to resolve that loop? First, you have to cancel the spot requests because that won’t terminate your instances. So, you still need to terminate those instances. 

What is Spot Fleet?

It’s just a collection of Spot Instances and one alternative of on-demand instances. Also, a Spot Fleet tries to launch the number of Spot Instances and on-demand instances to satisfy the target capacity you determined in the Spot fleet request. Finally, the request for Spot Instances is fulfilled if there’s available capacity and the highest price you chose in the request that you did exceed the current spot price.

How Spot Fleet works

If the spot price is below your highest price, the Spot Fleet also tries to keep its target capacity fleet if your Spot Instances are interrupted. In other words, Spot Fleets will attempt and compare the target capacity with your price constraints, so it’s just a method of completing your capacity within your budget. So we can set up other Launch Pools and specify configurations like EC2 instance types, operating systems, and availability zones.

The EC2 instance can all be in separate pools, and then we can have multiple collections. And the fleet will determine the most suitable method to execute depending on the approach you define. Also, Spot Fleets will prevent launching instances once we match your price threshold or the capacity specified. But, there are different strategies.

Different Strategies to Optimize Spot Instances

diversified

Then we’ve got diversified. This is where the Spot Instances are distributed across all the different pools.

InstancePoolsToUseCount

The Spot Instances are distributed across the number of Spot Instance pools you specify. And this parameter is only valid when used in combination with the lowestPrice

capacityOptimized

The Spot Instances come from the pool with optimal capacity for the number of instances launching. So it’s where you are optimizing your capacity.

 lowestPrice

 The lowest price is probably one of the most popular. And this is where your Spot Instances come from the pool with the lowest price. And this is the default strategy.

Conclusion

So Spot Instances save up to 90% of the cost of on-demand instances. So it’s valuable for any computing where we don’t require persistent storage, like a database. Also, we can prevent Spot Instances from terminating automatically by utilizing a Spot Block. And finally, a Spot Fleet is just a group of Spot Instances and one alternative of on-demand instances. And we can configure it in various approaches and use the four techniques we just learned and do them in pools. Also, we can have multiple collections in a spot fleet. 

Leave a Comment

Your email address will not be published. Required fields are marked *

Free PDF with a useful Mind Map that illustrates everything you should know about AWS VPC in a single view.