In this article, we will give describe some strategies to reduce Amazon EC2 costs. This article is part of our How to reduce AWS Costs series.
Amazon EC2 is a core product of Amazon Web Services. But there are many different options to choose from. And your instances could be deployed in several ways to achieve similar results. So choosing the most cost-effective instance might not be simple.
We will mention here some strategies you should consider to reduce the monthly costs of your Amazon EC2 instances. Note that each strategy has to be analyzed in terms of the requirements of your services. And you should use the strategies that work best for you. Also that Strategies to reduce Amazon EBS Costs article describes similar approaches for EBS.
First, let’s talk about EC2 instance families.
1. Change to “T” family instances.
The “T” family is one of the most used in AWS and the first you should consider. It’s used for bursty traffic. For example, when your CPU usage is normally low, but with high CPU spikes for short periods of time. This family of instances is ideal for a website with low traffic. And the cost is very low in comparison with other instances.
But keep in mind that these processors can’t have a high CPU usage for long periods. Otherwise, they will reduce their performance to their baseline performance (for non-unlimited mode), or incur additional costs (for unlimited mode).
So using switching to a “T” family instances (for example t3a.small or t3a.medium) will have cost advantages. But using them depends on your application requirements.
2. Change to “A” family instances
The “A” family of instances is based on ARM processors. These are used for smartphones, laptops, and tablet computers. They have lower power consumption, and also lower costs than traditional x86 processors.
This family of instances is even less expensive than “A” families. So it’s worth considering switching to these instance family.
Note that you will have to use an operating system compiled for ARM. For example, Amazon Linux 2, Red Hat Linux, or Suse Linux. When you choose the AMI for a new EC2, you will find the Operating Systems Available.
Also, your application should be able to run on this ARM-based processor. Standard applications (like Web Servers, or FTP server) can run on ARM normally.
3. Use other instance families
Families “T” and “A” are usually the most economical. Otherwise, you can choose any of these other families below according to your use case:
- “M” families. They are for intended general purposes applications. Their Memory-to-vCPU ratio is 4. For example, an m5.large has 8 GiB of memory for 2 vCPUs.
- “C” families. These are compute-optimized families. And they are used for applications that need high CPU usage. Their Memory-to-vCPU ratio is 2. For example, a c5.large instance has 4GiB RAM for 2 vCPU.
- “R”, “X”, and “Z” families are memory-optimized. In contrast with “C” families, they need higher amounts of memory. Their Memory-to-vCPU ratio is between 7.6 and 30.
- “P” and “G” families are the GPU optimized. They use hardware accelerators to perform functions (like floating point calculations or graphics processing) faster.
- “I”, “D”, and “H” families are storage-optimized. They have specific characteristics that allow them to read or write large amounts of data on local storage.
Many companies use “T” or “M” families for all their applications. But this could be optimized according to the use case.
A good approach is to start defining the quantity of vCPUs and RAM that your application needs. Then go to AWS Calculator, and create a cost estimation for Amazon EC2. Then enter the minimum vCPUs and Memory you need. The tool will automatically show the best instance for these 2 parameters. And also provide a monthly cost estimation. You can also test different values to understand how parameters affect the EC2 pricing.
4. Change Region
Some regions are much expensive than others. So it’s worth thinking about moving your instances to a region with lower prices.
You can also check our Choosing an AWS Region article describing the factors to consider to define the AWS region to use.
5. Change the Operating System
Remember that when your EC2 is running, you are not only paying for the hardware. The EC2 instances also have a cost associated with the Operating System used.
For example, if you use Microsoft Windows operating system or RedHat, this will incur additional costs every hour. So you can consider also migrating your application to a Linux platform. It’s free, so you don’t have a license cost.
6. Change to a newer instance
As technology improves, AWS releases new types of instances. They have better performance and might have lower costs.
For example, let’s say you are using a t2.small instance. If you switch to the new generation t3.small instances, you should get a 10% discount. And if you switch to a t3a.small, you could get a 20% discount compared to t2.small. These instances have all 2 GB of RAM.
Updating the instances to a newer one can be done in seconds. So you should frequently check if new instances appear, and consider changing to the latest ones. You can check current instance types here
7. Change instance type based on CPU Usage
Many times the instance type is dimensioned early based on your application assumptions. But later the instance remains over-dimensioned for the current usage.
You can use Cloudwatch to analyze the CPU usage for each of your instances in the last days. If the average CPU usage is below 10%, you should probably consider switching using a smaller instance.
8. Change instance type based on Memory Usage
This point is similar to the previous one. Some instances might have much more memory than necessary. But in this case, Cloudwatch by default doesn’t show the amount of memory used by your instance. To find out the memory used, you log into your instance. And check the memory used by the Operating system.
Another option to check memory usage is to install CloudWatch Agent. This agent collects some metrics of your instance and sends them to CloudWatch. Then you can see all the metrics inside the AWS CloudWatch console without logging into the instance. And they are saved for some time.
In case the average memory used is below 10%, you should consider changing to a smaller instance. Keep in mind that you could use some swap memory to cover some unexpected memory spike, and avoid the application to crash because of low memory.
9. Use instances part-time
Some instances are used only partially during the day. For example, an instance with your CRM is probably not used at night. So you can turn the instance off without affecting your service. AWS allows can turn on (or off) your instances at regular periods.
If the instance is turned off, you will not be charged by the instance processor and memory. But you will still pay for the EBS storage.
10. Use instances Auto-Scaling
This strategy can be used for applications that could be distributed in several instances. For example, if you have a web service served by various instances simultaneously behind a load balancer.
Amazon EC2 instances support Auto-Scaling. This a scheme where the number of running instances is increased (or decreased) according to current traffic. For example, at night your website traffic might be very low. So only one instance keeps working. When traffic increases, the number of instances is also increased to 2 or 3.
This adjustment is done automatically based on metrics you define (like traffic, or current CPU usage of the running instances), or based on the time of the day.
This architecture will require a load balancer. But also there is another benefit related to resilience. If an instance fails, your load balancer could start sending traffic to the rest of the instances. And also start a new instance to replace the failing one. So, in this case, your service isn’t affected.
11. Use an AWS managed service
AWS continuously release services. And they might perform that same type of service that you are executing on your EC2. For example:
- AWS RDS could replace an instance with a relational database
- AWS DynamoDB and AWS DocumentDB could replace an instance with a non-relational database
- AWS SFTP could replace an instance with an FTP service
- AWS Fargate could replace an instance with containers.
- AWS Elasticache could replace an EC2 with an In-Memory database (Redis or Memcached)
- AWS EFS, AWS Storage Gateway, AWS FSX Server for Windows File Server, and AWS WorkDocs might replace an instance with an NFS (or SMB) file server.
- AWS Managed Microsoft AD might replace an instance with a Microsoft Active Directory.
Using an AWS service has benefits over running a service in one instance. These AWS services are highly available. AWS takes care of infrastructure, OS, and application software. So you don’t have to maintain your service yourself. You don’t have to pay for licenses. AWS applies OS and application patches automatically. And apart from all this, you get service metrics integrated on AWS CloudWatch. An AWS managed service might have cost advantage also. But this depends on the service usage and configuration.
12. Use Spot Instances
EC2 instances can be purchased in 5 ways.
- OnDemand Instances
- Spot instances
- Saving Plans
- Reserve Instances
- Dedicated Hosts
Usually, you will use OnDemand instances in most cases. Savings Plans and Reserved Instances are used to get a discount by committing for 1 year or 3 years of usage.
Dedicated hosts are used when you need to transfer some previously purchased licenses (e.g. Microsoft Windows, Microsoft SQL Server, or RedHat) to your instances. Dedicated hosts are usually the most expensive option.
But what about Spot instances? These are the most economical of all. You can easily get a 70% discount over OnDemand prices. The prices of these instances are dynamic and based on supply (AWS unused instances) and demand (spot requests by AWS customers). So it’s constantly changing.
There are 2 ways to launch a spot instance.
- The first one is setting a threshold price. The instance will run as long as the AWS spot price is above the threshold price. When the spot price goes below that threshold, the instance is shut down (or hibernated). And it will resume when the spot is above that threshold again.
- The second way to launch spot the instance is by defining a fixed time (1 to 6 hours). This is called Block Spot. You will pay the current spot price for the fixed period you chose. And instance will shut down afterward.
It’s very important to note that you don’t know how long the spot instance will run and if the current task will be finished. Not every application can run on spot instances. Your application should be tolerant of this condition. For example, batch jobs, or tasks executed only once a day, are good candidates to use spot instances.
13. Use Savings Plans & Reserved Instances
As previously mentioned, this allows for saving money by committing to use EC2s for 1 or 3 years. You purchase a plan, and that gives you a discount. You can check Using AWS Savings Plans to reduce EC2 costs blog post with more information.
This strategy, especially for Reserved Instances, should be implemented only if your infrastructure won’t suffer changes.
If you need help to reduce your AWS bill, you can send us an email. We are happy to help.
If you enjoyed this article, please share!