In the “Architecting on AWS” course there is a slide on Autoscaling Purchasing Options.
From the course notes:
Amazon EC2 Auto Scaling supports multiple purchasing options within the same Auto Scaling group (ASG). You can include Spot, On-Demand, and Reserved Instances (RIs) within a single ASG, allowing you to save up to 90% on compute costs.
The following walk through only takes a few minutes to try.
I keep it minimal to demonstrate the basic features and get started quickly, leaving you to try out the many further options.
In order to demonstrate using a mixture of On-Demand and Spot pricing, I will use a steady state Auto Scaling Group of 4:4:4, that is Minimum, Maximum and Desired all of 4. I aim to have a 50%/50% mix of On-Demand and Spot, that is 2 of each.
Another options is to have always have a certain number of On-Demand Base instances as part of the mix, but I will leave that Base number at zero.
When configuring Launch Templates and Auto Scaling Groups there is no reference to Reserved Instances. That part is automatically dealt with by matching your Reserved Instance choices, if any, with your actual running instances across the account, and then billing at the Reserved Instance price.
Everything that follows is done from the EC2 console.
This use case requires the use of a Launch Template.
Create a Launch Template. Give it a name. At a minimum, when the Launch Template will be used by an ASG, you must supply and AMI and Instance Type. I choose an AMZ Linux 2 AMI and t2.micro Instance Type.
Leave all rest as defaults. Note that expanding Advanced Details shows that you can request Spot instances here, but you must not tick this if the use case is to have a mix of On-Demand and Spot within and ASG.
Create an Auto Scaling Group and select the Launch Template. Select “Combine purchase options and instances”.
For Instance Distribution, clear “Use the default” and then choose “On-Demand Percentage” above base=50%, leaving everything else at default.
Choose to Start with 4 instances. This is the desired number to start with. If you use Auto Scaling policies, then when scaling in and out, this the staring desired number and then it changes dynamically.
Select a subnet. In production it would be recommended to use more than one.
On the next screen, choose “No scaling policies”. To keep it simple, and to avoid needing to run some kind of simulated stress, we will keep it to a steady state group of 4 instances.
After a few seconds, you should see 4 instances launching.
We will want to verify the mix of On-Demand and Spot.
It is not possible to see the purchasing options from the instance details. Instead, you can go to EC2, Spot Requests to see 2 active spot requests, both fulfilled.
Alternatively, using CLI, the following will show details of the 2 Spot Instances.
aws ec2 describe-spot-instance-requests
If you select EC2, Spot Requests and click “Savings Summary” you can see A high-level summary of your savings across all of your running and recently terminated Spot Instances
To clean up:
Delete the ASG and Launch Configuration. The instances will be terminated, and in a few minutes you will see that the spot requests are closed.
The docs are here
Auto Scaling Groups with Multiple Instance Types and Purchase Options
Introducing the capacity-optimized allocation strategy for Amazon EC2 Spot Instances
and for information about how the spot pricing model has changed recently
New Amazon EC2 Spot pricing model: Simplified purchasing without bidding and fewer interruptions