How to Launch Spot Instances with Your AWS EC2 Auto Scaling Group

Incorporating Spot Instances into your EC2 Auto Scaling strategy can help you significantly lower your costs. In this guide, we'll show how you can launch Spot Instances with your EC2 Auto Scaling group.

Patrick Londa
Author
Jan 6, 2023
 • 
5
 min read
Share this post

If your application gets a spike of traffic and needs to increase the number of instances running to keep up, your Auto Scaling group has preferences, either customized at the group-level or at the launch template-level, that dictate which type of instances it launches.

In this guide, we’ll show you how to configure your launch templates and Auto Scaling groups to incorporate Spot Instances and achieve lower computing costs.

Supplementing EC2 On-Demand Instances with EC2 Spot Instances

With the right settings, you can create an Auto Scaling group that supports multiple instance types, for example both EC2 Spot Instances and EC2 On-Demand Instances. To set this up, you change the configuration at the group-level.

Before you do that, make sure that the launch template your Auto Scaling group is referencing has the Purchasing option parameter left as unspecified (“InstanceMarketOptions”). If the launch template has one instance type specified, you will receive an error when updating your Auto Scaling group to have multiple types.

Using the AWS Console:

Here are the steps for creating an Auto Scaling group that supports both EC2 On-Demand and other EC2 instance types.

  1. Open the Amazon EC2 console and navigate to the Auto Scaling Groups page.
  2. Click Create an Auto Scaling group.
  3. Choose a version of an existing launch template that does not have Purchasing option specified.
  4. Select the VPC and subnets for the Auto Scaling group. The VPC must be the same one used by the security group for the referenced launch template.
  5. At Instance type requirements, click Override launch template and select Manually add instance types.
  6. By default, you’ll see Family and generation flexible selected. If there are specific instance types that you don’t want to use, you can remove them by clicking the X. You can then move the order to prioritize the instance types. The Weight column allows you to specify how each instance type contributes towards your desired capacity. For example, if there are instance types with subpar memory, storage, or network traits, you can weight them less (less than 1 instance) relative to your desired capacity, so you can ensure your performance requirements are met. You can read more details on weighting here. If you don’t want to specify weights, just make sure that all the boxes in the Weight column are empty.
  7. For Instance purchase options, you can choose the percent mix of On-Demand Instances and Spot Instances with Instance distribution. To ensure a floor capacity, select Include On-Demand base capacity. Once you scale beyond this base capacity level, the Auto Scaling group will use your Instance distribution to select instance types.
  8. Next, you need to specify your Auto Scaling group’s allocation strategies. If you manually ranked instance types, your On-Demand allocation strategy will default to Prioritized based on your ordering. For Spot allocation strategy, Price capacity optimized is the default and recommended strategy. There are other Spot Instance allocation strategies you could choose like capacity-optimized, capacity-optimized prioritized, and lowest-price. You can also specify a maximum bid for Spot Instances, but it’s not required. If you don’t, the maximum bid will be set by default to the price of On-Demand Instances.
  9. Now that you’ve specified how instances should be launched, you can also enable Capacity rebalance if you want to ensure your instance mix can respond to an interruption in Spot Instances. With this enabled, if Spot Instance prices spike and trigger the termination of your Spot Instances, your On-Demand Instances will be scaled down to maintain the desired percentage mix.
  10. Lastly, you’ll want to set a Desired capacity for your Auto Scaling group, with a minimum capacity and maximum capacity. These add guardrails to your scaling and set a goal for your Auto Scaling group to moderate against.
  11. If you want to add tags or scaling notifications, you can set those up and then move to the Review page, and click Create Auto Scaling group.

If you have an existing Auto Scaling group that you want to update to support multiple instance types, you can click Edit on that Auto Scaling group and then make the adjustments outlined above in steps 5-8.

Using the AWS CLI:

To create an Auto Scaling group with the same settings as we described above, but with the AWS CLI, you can use the create-auto-scaling group command with a JSON or YAML file that contains your specific configuration preferences.

Here’s an example with a JSON file:

aws autoscaling create-auto-scaling-group 
--cli-input-json file://~/config.json

This is the file being reference:

{
	"AutoScalingGroupName": "demo-asg",
	"MixedInstancesPolicy": {
    	"LaunchTemplate": {
        	"LaunchTemplateSpecification": {
            	"LaunchTemplateName": "demo-launch-template",
            	"Version": "$Latest"
        	},
        	"Overrides": [
            	{
                	"InstanceType": "c5.large"
            	},
            	{
                	"InstanceType": "c5a.large"
            	},
            	{
                	"InstanceType": "m5.large"
            	},
            	{
                	"InstanceType": "m5a.large"
            	},
            	{
                	"InstanceType": "c4.large"
            	},
            	{
                	"InstanceType": "m4.large"
            	},
            	{
                	"InstanceType": "c3.large"
            	},
            	{
                	"InstanceType": "m3.large"
            	}
        	]
    	},
    	"InstancesDistribution": {
        	"OnDemandPercentageAboveBaseCapacity": 30,
        	"SpotAllocationStrategy": "price-capacity-optimized"
    	}
	},
	"MinSize": 1,
	"MaxSize": 5,
	"DesiredCapacity": 3,
	"VPCZoneIdentifier": "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782"
}

 You’ll see topics we covered above in the Console steps, like selecting a launch template, overriding the launch template to specify instance types manually, setting allocation strategies and percent distributions for instance types. If you skipped to this CLI section, you should scroll up to read about each of those.

 If you prefer writing YAML files to JSON files, you can reference a YAML file with the same command. Here’s an example YAML file:

---
AutoScalingGroupName: demo-asg
MixedInstancesPolicy:
  LaunchTemplate:
    LaunchTemplateSpecification:
 	 LaunchTemplateName: demo-launch-template
 	 Version: $Default
    Overrides:
    - InstanceType: c5.large
    - InstanceType: c5a.large
    - InstanceType: m5.large
    - InstanceType: m5a.large
    - InstanceType: c4.large
    - InstanceType: m4.large
    - InstanceType: c3.large
    - InstanceType: m3.large
  InstancesDistribution:
    OnDemandPercentageAboveBaseCapacity: 30
    SpotAllocationStrategy: price-capacity-optimized
MinSize: 1
MaxSize: 5
DesiredCapacity: 3
VPCZoneIdentifier: subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782

 If you want to validate that your Auto Scaling group has been created, you can use the describe-auto-scaling-groups command to see that your policy is in place.

If you want to update an existing Auto Scaling group instead of creating a new one, you can use the same process with the update-auto-scaling-group command.

Optimize Auto Scaling Group Settings with Blink

If you want to scale this approach, it’s very time-consuming. You’ll need to verify if launch templates are compatible, update them if they aren’t, update the Auto Scaling group to use that new version, and then customize the Auto Scaling group settings.

What if you could run these checks and incorporate Spot Instances at scale with a couple clicks in a reusable automation?

With Blink, you can use no-code steps to turn manual tasks into best practices at scale. Drag and drop from preconfigured actions for each platform, or use your own scripts and add conditional branches, approval flow, and notifications.

Get started with Blink and impact your EC2 costs at scale today.If your application is fault-tolerant and can handle occasional subpar performance, then AWS EC2 Spot Instances can be a great way to lower your costs compared to EC2 On-Demand instances. Spot Instances are extra computing capacity that AWS can offer at a highly discounted price, in exchange for less guaranteed availability.

Automate your security operations everywhere.

Blink is secure, decentralized, and cloud-native. 
Get modern cloud and security operations today.

Get a Demo