Launching EC2 Spot Instances via EC2 Auto Scaling group

An Auto Scaling group contains a collection of Amazon EC2 Instances that are treated as a logical grouping for the purposes of automatic scaling and management. An Auto Scaling group also enables you to use Amazon EC2 Auto Scaling features such as health check replacements and scaling policies. Both maintaining the number of instances in an Auto Scaling group and automatic scaling are the core functionality of the Amazon EC2 Auto Scaling service.

Amazon EC2 Auto Scaling helps you ensure that you have the correct number of Amazon EC2 Instances available to handle the load for your application. You can specify the minimum and maximum number of instances, and Amazon EC2 Auto Scaling ensures that your group never goes below or above this size.

When adopting EC2 Spot Instances, our recommendation is to consider first Auto Scaling group since it offers a very rich API with benefits like scale-in protection, health checks, lifecycle hooks, rebalance recommendation integration, warm pools and predictive scaling, and many more functionalities that we list below.

In the past, Auto Scaling groups used Launch Configurations. Applications using Launch Configurations should migrate to Launch Templates. With Launch Templates you can provision capacity across multiple instance types using both Spot Instances and On-Demand Instances to achieve the desired scale, performance, and cost optimization.

Auto Scaling group example: Using mixed instance groups with Spot and On-Demand capacity

A common case when using Auto Scaling groups, is to use it with workloads that require a mix of Spot and On-Deamand capacity.

To apply Spot best practices we will launch a mixed instance Auto Scaling group using Spot and On-demand Instances.

This first step does create a json file. The file describes a mixed-instance-policy section with a set of overrides that drive diversification of Spot Instance pools. The configuration of the Auto Scaling group does refer to the Launch Template that we created in the previous steps.

cat <<EoF > ~/asg-policy.json
{
   "LaunchTemplate":{
      "LaunchTemplateSpecification":{
         "LaunchTemplateId":"${LAUNCH_TEMPLATE_ID}",
         "Version":"1"
      },
      "Overrides":[
         {
            "InstanceType":"c5.large"
         },
         {
            "InstanceType":"m5.large"
         },
         {
            "InstanceType":"r5.large"
         },
         {
            "InstanceType":"c4.large"
         },
         {
            "InstanceType":"m4.large"
         },
         {
            "InstanceType":"r4.large"
         }
      ]
   },
   "InstancesDistribution":{
      "OnDemandBaseCapacity":2,
      "OnDemandPercentageAboveBaseCapacity":25,
      "SpotAllocationStrategy":"capacity-optimized"
   }
}
EoF

In the override section, choose as many instances that qualify for your application as possible, in this case we selected a group of 6 instance types that meet the “.large” criteria.

With Auto Scaling groups you can define what is the balance between Spot vs On-Demand Instances that makes sense for your workload. OnDemandBaseCapacity allows you to set an initial capacity of On-Demand Instances to use. After that, any new procured capacity will be a mix of Spot and On-Demand Instances as defined by the OnDemandPercentageAboveBaseCapacity.

The configuration above, sets the SpotAllocationStrategy to capacity-optimized. The capacity-optimized allocation strategy allocates instances from the Spot Instance pools with the optimal capacity for the number of instances that are launching, making use of real-time capacity data and optimizing the selection of used Spot Instances. You can read about the benefits of using capcity-optimized in the blog post Capacity-Optimized Spot Instance allocation in action at Mobileye and Skyscanner.

Let’s create the Auto Scaling group. In this case the Auto Scaling group spans across 3 Availability Zones, and sets the min-size to 2, max-size to 10 and desired-capacity to 6.

aws autoscaling create-auto-scaling-group --auto-scaling-group-name EC2SpotWorkshopASG --min-size 1 --max-size 10 --desired-capacity 6 --vpc-zone-identifier "${SUBNET_1},${SUBNET_2},${SUBNET_3}" --capacity-rebalance --mixed-instances-policy file://asg-policy.json

You have now created a mixed instances Auto Scaling group!

Given the configuration we used above, Try to answer the following questions:

  1. How many Spot Instance pools does the Auto Scaling group consider when applying Spot diversification?
  2. How many Spot vs On-Demand Instances have been requested by the Auto Scaling group?
  3. How can you confirm which instances have been created within the Auto Scaling group?
  4. How can you check which instances have been launched using the Spot purchasing model and which ones using the On-Demand?
Show me the answers:

Auto Scaling group has rich functionality that helps reduce the heavy lifting of managing capacity. Auto Scaling groups can dynamically increase and decrease capacity as needed.

Brief Summary of Auto Scaling group functionality

These are some of the characteristics and functionality that make Amazon EC2 Auto Scaling groups the right choice for most workloads:

  1. Instance distribution & Availability Zone rebalancing: Amazon EC2 Auto Scaling groups attempt to distribute instances evenly to maximise the high availability of your workloads. Instance distribution & AZ rebalancing.
  2. Flexible scaling: Auto Scaling group has a set of rich APIs to manage the scaling of your workload, allowing workloads to control their scaling needs whichever those are, from Manual scaling, Scheduled scaling, Dynamic Scaling using Target tracking, Step scaling and Predictive scaling.
  3. Elastic Load Balancing integration: The integration with Elastic Load Balancing automatically distributes your incoming application traffic across all the EC2 Instances that you are running. Elastic Load Balancing and Amazon EC2 Auto Scaling.
  4. Instance refresh & instance replacement based on maximum instance lifetime: Auto Scaling group reduces the heavy lifting required when updating for example the underlying AMI. Instance Refresh allows users to gradually refresh the instances in an Auto Scaling group. Instance replacement can also be set up upon the maximum instance lifetime, helping users to apply best practices of governance.
  5. Scale-in protection: Allowing to protect instances that are still working from being selected for scale-in operations Auto Scaling instance termination.
  6. Lifecycle hooks: Enable an Auto Scaling group to trigger actions so that users can manage the lifecycle of Auto Scaling group instances. Amazon EC2 Auto Scaling lifecycle hooks.
  7. Capacity rebalance: Amazon EC2 Auto Scaling is aware of EC2 Instance rebalance recommendation notifications. The Amazon EC2 Spot service emits these notifications when Spot Instances are at elevated risk of interruption. When Capacity Rebalancing is enabled for an Auto Scaling Group, Amazon EC2 Auto Scaling attempts to proactively replace Spot Instances in the group that have received a rebalance recommendation, providing the opportunity to rebalance your workload to new Spot Instances that are not at elevated risk of interruption.
  8. Instance weights: When you configure an Auto Scaling group to launch multiple instance types, you have the option of defining the number of capacity units that each instance contributes to the desired capacity of the group, using instance weighting. This allows you to specify the relative weight of each instance type in a way that directly maps to the performance of your application. You can weight your instances to suit your specific application needs, for example, by the cores (vCPUs) or by memory (GiBs). EC2 Auto Scaling group weights.
  9. Support for multiple Launch Templates: Auto Scaling group supports multiple Launch Templates. This allows for extra flexibility in how the auto Scaling group is configured, for example supporting multiple architectures (i.e Graviton c6g and Intel c5) within a single Auto Scaling group. Multiple launch template documentation.
  10. Warm pools: Warm pool decrease latency of procuring capacity on your workloads by managing a pool of pre-initialized EC2 Instances. Whenever your application needs to scale out, the Auto Scaling Group can draw on the warm pool to meet its new desired capacity. Warm pools for Amazon EC2 Auto Scaling.

If you want to learn more about all the benefits of Auto Scaling groups, you can find more information in the Amazon EC2 Auto Scaling group documentation.