Spot Instances are designed around fluctuating workloads with flexible start and end times. But when used with Auto Scaling, it can make for a replacement for On-Demand instances at a fraction of the cost.
Spot Instances Save Around 50-70%
Spot Instances are a special kind of EC2 instance that enable AWS to sell off spare compute capacity, often at large discounts. They can be terminated at any time by AWS, with two minutes notice, but termination rates are quite low (less than 5% per month for most instances), and they’re intended to be used in an autoscaling group anyway.
Spot instance pricing used to fluctuate throughout the day, much like a stock market, making it hard to judge the total monthly costs for running a fleet of always-on spot instances, and causing instances to terminate quite frequently.
However, AWS fixed this problem in early 2018, and now spot instances work much differently. Rather than bidding on spare compute capacity and letting the community decide the price, AWS sets the price to the lowest value at which they’re willing to sell their spare compute capacity.
This leads to spot instance pricing being fairly consistent, as well as lasting much longer. Most fluctuations happen over the course of a month, rather than erratic daily spikes. Most instances, like the
c5 series, stay fairly flat:
Some instances see almost no fluctuation, like the
t3 series. You can view the current prices for spot instances from the EC2 console, under “Spot Requests” > “Pricing History”:
On average, though, you can likely save at least 50%, and up to about 70% for most of the latest instances. AWS claims that spot instances can save you “up to” 90%, but that’s mostly just marketing, as the only 90% savings are on older instance types that AWS rarely sells anymore.
It puts spot instances in a very useful spot. They’re about the same price as a 3-year Reserved Instance contract, without the obvious downside of being locked into a 3-year contract.
Auto Scaling Fixes Spot Instance Downsides
You’d think the great cost savings would be offset by a different downside, but it really isn’t in practice. The main problem with spot instances is that they can be terminated at any time by AWS, with only two minutes notice. This sounds like an issue, but with Auto Scaling, this problem is minimized.
Auto Scaling is a feature of AWS that scales your server architecture up to meet demand. If your web server becomes overburdened, a new one is automatically created to help balance the load. This requires you to automate your server’s lifecycle, all the way from installing packages to getting your code up and running, but it’s fairly simple to set up otherwise.
Auto Scaling tries to maintain a target amount of servers, and if one of your servers crashes, a replacement server can be spun up. This also works for Spot Instances—in the event that AWS reclaims the compute capacity, a new Spot Instance can be created in its spot. If you have multiple instances in an Auto Scaling group, you won’t experience any downtime. The time it takes to set up Auto Scaling is the only downside of Spot Instances, but you only have to do it once to save a ton of money each month on compute costs.
With how great spot instances are, they make On-Demand look strangely overpriced in comparison. Spot Instances seem to be the real cost AWS is willing to sell their instances at—they want you to adapt to their schema of scalable services running fleets of servers, as it’s better for both your business’s growth and AWS’s margins, and they’re using lower prices to entice you to set up auto scaling. Auto Scaling itself also saves money, and when combined with Spot Instances, it makes running scalable workloads like web servers much cheaper than reserved instances.
Of course, if all you need is a single web server, and want it to be available 100% of the time, it may make more sense to use a reserved instance (which saves you a similar amount of money), rather than setting up an Auto Scaling group. Spot Instances are only worth it when you have more than one instance, otherwise, you may experience a short downtime if AWS reclaims the instance.
How to Work with Auto Scaling
You can read our full guide on setting up Auto Scaling, but the gist of it is pretty simple.
First, you need to find a way to automate your server’s lifecycle. You must create a construction script that can handle all of the setup that you would usually do if you were to manually create a new server. While it may take some time, it ensures that scaling can happen automatically. To speed up new instance creation times, create a custom AMI that includes all of your preinstalled software (and even your own code, if you choose).
Next, create a new Launch Configuration from the Auto Scaling tab in the EC2 Console. This configuration contains all of the info about how servers are launched into the Auto Scaling group, such as instance type, images, and startup scripts. From the “Configure Detail” tab, check “Request Spot Instances.”
Using Spot Instances in an autoscaling group doesn’t have much of a downside, unless your application really can’t deal with interruptions on the server level. A good rule of thumb is if your application can be easily load balanced, it likely has no issues using Spot Instances.