3 min read | by Jordi Prats
If saving up to 90% for the same instance type is no reason enough to start using spot instances, to be able to use the same AutoScalingGroup to spawn both types of instances, saving us from the hassle of having to manage it by ourselves might help to change your mind
To do so we are goung to use a mixed instances policy that will contain the instance types it can launch.
The AutoScalingGroup will select the instance types according to the allocation strategy we set. We can choose between:
Assuming we have the following AutoScalingGroup where we want to add mixed_instances_policy, first we need to make sue we are using launch templates instead of launch configurations:
resource "aws_autoscaling_group" "demo" {
name_prefix = "demospot"
desired_capacity = "10"
max_size = "20"
min_size = "5"
capacity_rebalance = true
launch_template {
id = aws_launch_template.workers.id
version = "$Latest"
}
(...)
}
If we are using launch templates, adding a mixed_instances_policy to be able to use spot instances is as straightforward as this:
resource "aws_autoscaling_group" "demospot" {
name_prefix = "demospot"
desired_capacity = "10"
max_size = "20"
min_size = "5"
capacity_rebalance = true
mixed_instances_policy {
instances_distribution {
on_demand_base_capacity = 1
on_demand_percentage_above_base_capacity = 25
spot_allocation_strategy = "capacity-optimized"
}
launch_template {
launch_template_specification {
launch_template_id = aws_launch_template.demo.id
version = "$Latest"
}
}
}
(...)
}
What we need to take into consideration when using spot instances is:
Posted on 16/09/2021