Getty Images
Best practices for rightsizing EC2 instances
EC2 instances that are improperly sized drain money and restrict performance on workloads. Learn how rightsizing EC2 instances can optimize costs with these best practices.
Improperly sized EC2 instances can have a dramatic impact on both cloud costs and workload performance.
Workloads that lack adequate resources are at risk of running out of CPU or memory. Autoscaling can help address this risk, but it doesn't always guarantee that workloads remain stable during periods of high demand. By rightsizing EC2 instances, cloud teams can achieve an optimal balance between instance cost and performance.
What is EC2 rightsizing?
EC2 rightsizing is the process of choosing the best instance types for workloads hosted on Amazon EC2 to optimize costs.
Amazon offers several hundred EC2 instances at varying prices. Each type provides different levels of virtual CPU and memory resources. Some instances also come with special features, such as access to GPUs to support GPU-accelerated machine learning workloads. Choosing an instance that provides more CPU, memory or other resources than a workload requires can result in overspending.
Admins should understand the main types of instances within EC2 to get a sense of which images best fit particular workloads. For example, it's typically more cost-effective to run workloads with high memory requirements using a memory-optimized instance than it is to force the workload into a general-purpose instance.
Rightsizing should be a systematic, continuous practice -- not done in an ad hoc fashion on an irregular basis. Follow these rightsizing best practices to keep performance up and costs down. Best practices include the following:
- Tag and track workload resource usage.
- Monitor and compare costs.
- Use rightsizing tools.
- Take advantage of EC2 pricing plans.
- Switch instance families.
1. Tag and track workload resource usage
Use tags in conjunction with EC2 rightsizing to gain cost optimization recommendations. Tags help users track instances and what they're paying for each one.
Tracking the resources that workloads consume on EC2 helps identify areas to rightsize unoptimized instances.
Customers can track EC2 instance resource usage with the following:
- Amazon CloudWatch. This service provides basic per-instance resource metrics.
- Third-party observability tools. Deploying observability tools, including application performance management offerings, directly on the instance provides visibility into how much CPU, memory and storage throughput specific applications or processes consume.
Cloud teams can improve the performance of workloads hosted on EC2 instances to achieve an ideal balance between cost and performance. This isn't technically a form of rightsizing, but it works toward a similar end.
Turn off unnecessary application features or regenerate the instance based on a different OS that has less overhead. These changes can reduce the resources consumed by EC2 workloads. This achieves better performance on the same instance types or, in some cases, makes it possible to switch to a lower-cost instance.
2. Monitor and compare costs
In addition to tracking resource utilization, it's important to monitor EC2 costs. Find billing details for individual EC2 instances in AWS Cost Explorer by opening the Billing console. Select Cost allocation tags. From there, choose the appropriate tags for any relevant EC2 instances.
Compare the cost of each instance to its resource utilization data. If an instance uses few resources but costs a lot, change to a different instance type that provides fewer resources at a lower price point. If the instance isn't using the resources allocated to it, there is no need to pay for it.
3. Use rightsizing tools
A variety of tools can help rightsize EC2 instances. Amazon offers some native tools, including the following:
- AWS Compute Optimizer. This uses AI to generate cost optimization recommendations by analyzing usage trends. Based on this analysis, Compute Optimizer suggests alternative configurations that enable workloads to achieve the same performance using a different EC2 instance configuration, lowering costs.
- AWS Cost Explorer. This helps provide a better understanding of spending to identify workloads where businesses might be overpaying and provides analytics and visualization features for exploring the cost and usage of EC2 instances and other resources.
Third-party platforms, such as ProsperOps and IBM Turbonomic, can automatically analyze EC2 instances to identify wasted spending. In some cases, they can automatically migrate workloads to more cost-effective instance types.
4. Take advantage of EC2 pricing plans
There are four types of pricing plans for EC2 instances:
- On-Demand Instance. This provides access to instances when requested and comes with no pricing discounts.
- Reserved Instance. Customers get discounted hourly pricing but must commit to using the instance for a certain period of time.
- Spot Instance. This offers deep discounts; however, it can shut down at any time with no warning.
- Dedicated Host. This provides the greatest control over instances by enabling customers to choose physical hosts and comes with a cost premium.
To rightsize an EC2 instance and reduce costs, it's important to choose the right type of instance for a given workload. For example, Reserved Instances can be a bargain for a cloud server that hosts a web app that runs indefinitely. Avoid Spot Instances for this purpose because the instance -- along with the app it hosts -- could shut down without warning. A Spot Instance is a good fit for a batch data processing workload that can be interrupted and restarted later.
Technically, using pricing discounts isn't a form of rightsizing because it doesn't involve modifying the instance itself; you just change to a different pricing plan. Still, this practice goes hand in hand with rightsizing because it helps to achieve better cost and performance.
5. Switch instance families
Amazon offers more than a half dozen EC2 instance families. These categories of EC2 instances are designed for different types of workloads.
In addition to a general-purpose instance family, there are instance types optimized for the following workloads, among others:
- Compute-intensive workloads.
- Workloads with high memory requirements.
- Workloads with high storage needs.
Although switching instance families doesn't always guarantee a lower cost without a change in performance, it often can. If you're currently using a type of instance that is not optimized for the workloads it hosts, consider switching.
How to upgrade EC2 instances
Sometimes, rightsizing an EC2 instance requires upgrading to a different instance type. Workloads might change, and a different instance might provide a better balance between cost and performance. Or Amazon might release new instance types that better meet a variety of business use cases.
Regardless, users can upgrade existing instances to a new EC2 instance by following the steps below.
1. Stop the instance
First, stop the instance that will receive the upgrade. To do this, log in to the AWS Console, select Instances, choose the instance and click the Instance state button near the upper-right corner of the console.
This opens a dialog box. Use this to change the instance state to Stop.
Confirm that the instance is stopped by navigating back to the main Instances screen.
It's necessary to confirm a stopped instance because a running instance cannot receive an upgrade. However, it is possible to configure an autoscaling group as a means of upgrading instances while avoiding downtime.
2. Change the instance type
With the instance stopped, navigate to Actions > Instance settings > Change instance type in the Instances screen.
This opens a dialog that enables users to change to a new instance type.
In this example, a t2.micro instance changes to a t3a.nano instance.
Press the Change button for changes to take effect.
3. Restart the instance
Once the instance type is changed, users must restart the instance. To run it, use the Instance state menu, and select Start instance.
Note that these steps assume users are keeping the same Elastic Block Store (EBS) volume and network settings for their instance. If that's not the case, upgrade the instance first, and then adjust its EBS and network configuration.
How to upgrade EC2 instances without downtime
As mentioned, Amazon doesn't allow upgrades to a running EC2 instance. Users must shut it down first. This generally means that upgrades require downtime for any workloads hosted on the instance users are upgrading.
However, there are some workarounds. One is to use autoscaling groups. This approach enables applications to keep running by setting them up to run on multiple instances. Users can stop one instance and perform the upgrade without shutting down the app.
Another strategy is to use blue/green deployments. This entails setting up two environments: one where the original EC2 instance hosts an application and a second where the app runs on a new instance type. When it's time to shut down the original instance type, change the network settings to redirect traffic to the new environment.
This approach doesn't technically upgrade an instance; it replaces an older instance with a newer one and requires two instances that run simultaneously. However, it achieves the goal of changing to a new instance type without downtime.
Make rightsizing a continuous practice
Workloads change over time, so strive to make EC2 rightsizing an ongoing process. Often, an EC2 instance type no longer provides value because its workload doesn't need the same CPU, memory or other resources as before.
Cost monitoring tools that run continuously can also help provide an ongoing review of EC2 instance types. Another option is to require evaluations of instance selections on a regular basis. This could be once a month and coincide with payment of the AWS bill.
Editor's note: This article was expanded to include steps on how to rightsize EC2 instances.
Chris Tozzi is a freelance writer, research adviser, and professor of IT and society who has previously worked as a journalist and Linux systems administrator.