Using mixed_instances_policy to use Spot instances on an ASG

3 min read

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:

  • lowest-price: Spot instances come from the pool with the lowest price (default)
  • capacity-optimized: Spot instances come from the pool with optimal capacity for the number of instances that need to me launched
  • capacity-optimized-prioritized: It is used to give certain instance types a higher chance of launching first (when the ASG has several instance types configured)

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:

  • Spot instances will be terminated as soon as the spot price goes beyond our bid price, so whatever we are running on the instance we need to make sure it can be stopped at any time
  • We might want to adjust the on_demand_base_capacity and on_demand_percentage_above_base_capacity to make sure we have a minimum number of on demand instances so the application it is running is not affected when the spot instances are terminated

Posted on 16/09/2021