How to choose the right EC2 Instance type (you don't)

Let AWS select instance types based on constraints like CPU, Memory, etc. using Attribute Based Instance Selection for your EC2 Auto Saling Group.

How to choose the right EC2 Instance type (you don't)
Photo by Jordan Harrison / Unsplash


EC2 is a beast of a service, and possibly daunting to start with. They have 400+ instance types at the time of writing, and they release tens of new instances each year. I will not explain the different instance types or merits of each one, the AWS documentation does a good job for that. But that doesn't answer which instance type you should use. AWS has a set of features that can help with this. My favorite is Attribute based instance selection.

Before I dive into this feature, I want to acknowledge that different instance types are very useful in solving different problems. I have seen costs go down by 30%+ by just switching to a different instance type. In this blog, I will talk about how Attribute Based Instance Selection can improve availability and reduce costs for both general and specialized applications.

Attribute based instance selection

So you have developed this super awesome application, and now it's time to deploy it onto the cloud. You will need servers for that. Ideally, you have all the information about how your application scales, and you can pick the best instance types that match the requirements to the tee. But that is rarely the case. This is where Attribute based instance selection (ABS) comes to the rescue.

How does it work?

ABS is a feature within EC2 AutoScaling that lets you express your instance requirements as a set of attributes, such as vCPU, memory, and storage. What it basically means is that you say my application needs a minimum of 4 GB memory and 2 CPU cores to function, and do not pick instances greater than 16 GB memory and 32 cores. With this information, AWS will pick instance types that best suit your needs. Easy right!!

But should you trust AWS? What is stopping them from picking the largest instance types possible and draining your bank account? Is this safe to use? Let me break this feature down for you, and help you make the decision. I will argue this feature can actually help you save money.

What is in it for AWS?

You may not have noticed, but AWS is trying to make you go instance agnostic for the past few years. That means they want your applications to not have to depend on certain instance types. They offer spot instances at a heavily discounted price and have features like Mixed instance groups within EC2 Auto Scaling to try and entice you to not specify instance types for your applications. The reason for this is demanding shaping. The more control AWS has over selecting instances, the better spread it can have over the data center utilization.

Consider a hypothetical example - Graviton is a type of processor created by AWS. It is likely it costs them less to operate these instance types, which is why they have an incentive to have a large utilization for these instance types. Now let's say a big customer like Netflix requests a large number of Graviton instances. In the ideal scenario, most of the graviton instances will be in use by customers, and AWS will not be able to provide enough instances. However, if more customers have applications that are instance type agnostic and interruptable, it can be moved to a different instance type and still satisfy the large request. For this reason, AWS is offering large discounts (up to 90%) for spot instances. The downstream effect is this is feature offerings like Attribute based instance selection and Mixed instance groups, to allow for a seamless transition compute between different instance types.

Remember, the cloud is elastic and "infinitely" scalable, but EC2 still needs to be scaled for the peak to stay true to this promise. Instance type agnostic applications help EC2 lower that peak.

Saving cost while maintaining availability

This is where the integration with EC2 Auto Scaling comes in handy. You ideally want to run a big portion of your workload in spot instances to get that lucrative discount. The catch is the instances being interupted with a few minutes' notice, and the same instance type no longer being available to be launched for spot (you will get Insufficient Capacity Exception).

Turns out there is an ASG feature just for this - Capacity rebalance. I will write a full blog post about this. As an overview, ASG will monitor for spot interruption notice, and automatically launch a replacement for this.

The second safety net is the scaling controls for Auto Scaling Groups. You should configure your group with a Target Tracking policy (warrents its own blog post) on key metrics to ensure availability is maintained.

Lastly, you should have alarming and a runbook in place to switch to on-demand instances in case no spot capacity is available.

Price Protection for Attribute based instance selection

Price Protection may be the last piece of the puzzle to make you feel more confortable with using ABS. AWS allows you to specify a threshold for the amount you are willing to pay over the cost of the lowest-priced instance for the desired configuration. Eg. your configuration warrents a c5.xlarge instance based on the CPU. ASG will launch an instance that does not exceed 20% of this price for on-demand instances and 100% of the cost of spot instances if this instance type is not available.

For spot instances, you are clearly saving a lot on costs with the spot discounts. For on-demand instances, you will have a better chance of finding some instance in case the ideal instance is not available (ICE), improving the availability of your service. The bottom line, this feature is worth giving a shot.

Configuring a group with Attribute Based Instance Selection

You can create Auto Scaling groups using the Console, CLI, and Cloud Formation across all publically available AWS regions. Terraform support is not available at the time of writing, but you can track the update here. AWS how to guide does a good job of walking through how to create an ASG with Attribute based instance selection, and I will not repeat that in the blog post. This is an example of CLI input to create a group with ABS.


If there is one thing you should take away from this blog, Attribute Based Inststance Selection can help you reduce cost and improve reliability for your applications. It can be useful for new applications, and existing complex workflow alike. Go check it out, and let me know what you think of this feature.

As always, you can subscribe for more blogs on AWS features and services that can help you with your improving your cloud setup. I am always open to requests for things you would like me to write about :)