DevOps best practices and cases

AWS EC2 money-saving guide

AWS is the most frequently used Cloud provider, according to Statista. Unfortunately, very often we do not pay attention to small costs, which can result in huge expenses at the end of the month. Even though AWS provides a bunch of tools to save and optimize costs, not many people use them. In this article, we will look at general recommendations, Reserved Instances, EC2 Spot, Spot Fleet, and Autoscaling groups.

Earlier we have already covered general tips on how to save money on AWS, in short:
  • choose the minimum instance that meets your requirements
  • disable all unused AWS resources
  • check the use of Amazon S3
  • invoice analysis
  • autoscaling

Reserved Instances

You can easily save up to 72% of the price of on-demand instances by reserving used machines for 1 or 3 years for a specific availability zone (server location). However, you need to pay the price for the year ahead immediately. Therefore, it is advisable to reserve the instances that require constant work so that the costs associated with their launch could be reduced.

Depending on business requirements, even instances for non-production systems can be reserved if they are required for continuous operation. Database and even CDN reservations can also be considered.

Spot Instances

According to the AWS website, you can save up to 90% compared to on-demand EC2 instances.

Also, you should consider that EC2 Spot works with supply and demand to set prices. If someone else has paid more and needs an EC2 server, Spot Instance will stop working with a two-minute grace period to meet this demand. However, Spot Instance does not work forever. We have to build our architecture taking into account unexpected interruptions of instances by following these approaches:
  • Stateless - data is not stored in the RAM or permanent memory of the server. It is better to write your data to some database or save small files to S3
  • Minimize dependencies, no part of the system should depend on this server
  • Multiple Availability Zones (AZs) - the more availability zones you have, the more instances you will be able to use. Your users can work with the service faster and more comfortably. Therefore we recommend at least two AZs.

Spot Fleet

With a regular spot instance request, you place a bid on a specific type in one specific AZ and hope to get it. With Spot Fleet, you can request different types of instances that meet your requirements. Also, you can distribute a spot-fleet bid over several AZs to increase the probability of getting an instance. This approach greatly increases the chances that you will get instances that are available at the price of spot-fleet.

Honestly, in most cases, it does not matter which server you are using if it has enough resources. Usually, we are interested in CPU and RAM volume, and GPU for ML/DL tasks. For example, both m4.xlarge and m5.xlarge instances have 16 GB of RAM and 4 CPUs. So it makes sense to quote both types of instances with a Spot Fleet request and Spot Fleet will give you the cheapest option. 

In the case of spot fleet, you can also apply special weights to each type of instance. The weighting tells the spot fleet what total performance we want. For example, we need 10 GB of total RAM and we choose two types of instances, one with 2 GB and the other with 4 GB.

If we assign each instance a weight equal to the amount of RAM, we can tell the spot fleet to give us 10 units of capacity. The Spot fleet uses the weight information to provide 10 units. This can be five 2GB units or two 4GB units and one 2GB unit. It does not matter. We still get a total of 10 units or 10 GB of capacity.

You need to design a fault-tolerant system for Spot Fleet as well as for the usual Spot Instances described in the previous section.

Configuring AutoScaling Group

EC2 Autoscaling group allows your EC2 to scale or disable unnecessary resources depending on the load. You can view the scaling settings on your dashboard using the steps described here. Analyze the result to see if you can configure a scaling policy to add instances less aggressively. Also, check the settings to reduce the minimum number of instances that serve end-user requests.

AWS provides many tools and profitable offers to save on the cost of EC2, both for regular and continuous use, as well as for non-permanent computing. Some services also call for good development practices, such as the 12 Factor App, to make your applications better.

If you still have questions or want to optimize your costs for AWS services, write to us.