Launching EC2 Spot Instances via EC2 Auto Scaling group

When adopting EC2 Spot Instances, we recommend you to consider Amazon EC2 Auto Scaling group (ASG) since it offers the most up to date EC2 features such as attribute-based instance type selection, capacity rebalancing, scaling policies and many more functionalities.

Amazon EC2 Auto Scaling groups contain a collection of Amazon EC2 Instances that are treated as a logical grouping for the purposes of automatic scaling and management. Auto Scaling groups also enable 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.

In the past, Auto Scaling groups used launch configurations. Applications using launch configurations should migrate to launch templates so that you can leverage the latest features. 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.

Using attribute-based instance type selection and mixed instance groups

Being instance flexible is an important Spot best practice, you can use attribute-based instance type selection (ABIS) to automatically select multiple instance types matching your requirements. A common case when using Auto Scaling groups, is to use it with workloads that require a mix of Spot and On-Demand capacity.

In this step you create a json file for creating Auto Scaling groups using AWS CLI. The configuration uses the launch template that you created in the previous steps and ABIS to pick any current generation non-GPU instance types with 2 vCPU and no limit on memory. OnDemandBaseCapacity allows you to set an initial capacity of 1 On-Demand Instance. Remaining capacity is mix of 25% On-Demand Instances and 75% Spot Instances defined by the OnDemandPercentageAboveBaseCapacity.

cat <<EoF > ./asg-policy.json
{
   "LaunchTemplate":{
      "LaunchTemplateSpecification":{
         "LaunchTemplateId":"${LAUNCH_TEMPLATE_ID}",
         "Version":"1"
      },
      "Overrides":[{
         "InstanceRequirements": {
            "VCpuCount": {
               "Min": 2, 
               "Max": 2
            },
            "MemoryMiB": {
               "Min": 0
            },
            "CpuManufacturers": [
               "intel",
               "amd"
            ],
            "InstanceGenerations": [
               "current"
            ],
            "AcceleratorCount": {
               "Max": 0
            }
         }
      }]
   },
   "InstancesDistribution":{
      "OnDemandBaseCapacity":1,
      "OnDemandPercentageAboveBaseCapacity":25,
      "SpotAllocationStrategy":"price-capacity-optimized"
   }
}
EoF

In this configuration you set the SpotAllocationStrategy to price-capacity-optimized. The price-capacity-optimized allocation strategy allocates instances from the Spot Instance pools that offer low prices and high capacity availability. You can read more about the price-capacity-optimized allocation strategy in Introducing the price-capacity-optimized allocation strategy for EC2 Spot Instances blog post.

  1. Run the following commands to retrieve your default VPC and then its subnets.
export VPC_ID=$(aws ec2 describe-vpcs --filters Name=isDefault,Values=true | jq -r '.Vpcs[0].VpcId')
export SUBNETS=$(aws ec2 describe-subnets --filters Name=vpc-id,Values="${VPC_ID}")
export SUBNET_1=$((echo $SUBNETS) | jq -r '.Subnets[0].SubnetId')
export SUBNET_2=$((echo $SUBNETS) | jq -r '.Subnets[1].SubnetId')
  1. Run the following commands to create an Auto Scaling group across 2 Availability Zones, min-size 2, max-size 20, and desired-capacity 10 vCPU units.
aws autoscaling create-auto-scaling-group --auto-scaling-group-name EC2SpotWorkshopASG --min-size 2 --max-size 20 --desired-capacity 10 --desired-capacity-type vcpu --vpc-zone-identifier "${SUBNET_1},${SUBNET_2}" --capacity-rebalance --mixed-instances-policy file://asg-policy.json

You have now created a mixed instances Auto Scaling group!

Challenges

Given the configuration you used above, try to answer the following questions. Click to expand and see the answers.

1. How may 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?
5. How can you select specific instance types manually instead of ABIS in your Auto Scaling group?
6. How can you select a mix of instance types of different sizes in your Auto Scaling group?
7. How can you select capacity-optimized Spot allocation strategy in your Auto Scaling group?

Optional reads

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

  1. Attribute-based instance type selection: Amazon EC2 Auto Scaling groups selects a number of instance families and sizes based a set of instance attributes that describe your compute requirements. Attribute-based instance type selection.
  2. Instance distribution & Availability Zone rebalancing: Amazon EC2 Auto Scaling groups attempt to distribute instances evenly to maximize the high availability of your workloads. Instance distribution & AZ rebalancing.
  3. 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.
  4. 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.
  5. 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.
  6. Scale-in protection: Allowing to protect instances that are still working from being selected for scale-in operations Auto Scaling instance termination.
  7. 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.
  8. 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.
  9. 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.
  10. 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.
  11. 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.