Saving 62% on EC2 costs ($41k to $15k)
Published: 2023-04-28 14:11:38

# Scenario

During the first few months of my new job, I created a cloud infrastructure to host a large eCommerce site running on Magento 2. I containerized the app and wrote a Terraform configuration to launch and configure everything needed for the service to work, including VPC, EC2 instances, ECS, S3, CloudFront, EFS, and more.

The issue with a Magento eCommerce site receiving an average of 10 million requests each month is that the load tends to slow down the infrastructure, requiring a powerful server as a base starting point. After load testing, I found that I need a `c5ad.2xlarge` compute-optimized instance with `8CPUs` and `16GB` of dedicated RAM to run Magento smoothly.

In addition, Magento requires additional services such as MariaDB, Redis, and Elasticsearch to function. To address this, I’ve chosen to use a dedicated machine for each service. Currently, I am not using RDS due to its pricing, but I’m considering whether the risk of sudden downtime from one of these instances is worth it. 

I’m referring to this risk as we experienced a service disruption in a previous project due to AWS maintenance on one of our running machines. We received an email notifying us of a hardware fault and the need to replace the current instance with another one within two weeks. Unfortunately, AWS shut down the machine immediately after the notice.

With RDS we could start a database with two running instances, decreasing the risk of downtime in case of hardware failure.

While this might be worth writing about, let's focus on our cost-saving strategy for now. As of today, it has been two years and we have not experienced any downtime related to these machines in this specific project.

# Problem

In total, we have a minimum of six instances running in production, including two base instances for ECS, service instances, and a bastion host. The instances are configured by the following sizes: `2x c5ad.2xlarge` for ECS, `1x m5ad.xlarge` for Redis, `1x c5ad.xlarge` for MariaDB, `1x c5ad.large` for ElasticSearch, and `1x m5a.xlarge` for the bastion host.

The monthly on-demand costs for each instance are `2x $288` for ECS, `1x $180` for Redis, `1x $144` for MariaDB, `1x $72` for ElasticSearch, and `1x $180` for the bastion host, totaling `$1,152/month`.

This amounts to `$13,824/year` and `$41,472/three years`.

How can we save money from this?

# Solution

It actually depends on your situation, and in my scenario, I have an eCommerce that is running for more than five years, so it makes sense to get reserved instances for three years paying all upfront.

Here there’s a Google Sheet with [aws-pricing-sheet-addon](https://github.com/getstrake/aws-pricing-sheets-addon) to get a quote with the latest pricing, comparing reserved instances with different payment methods.

Sample picture

![Costs Saving Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gnnvrhamh86e6a2w6auh.png)

Read-only URL

[Costs Evaluation spreadsheet](https://docs.google.com/spreadsheets/d/1KUOiLJ6odiJv7AbcjrGbj3_Hma7aiuMkJPTi5JnhKYs/edit?usp=sharing)

# Results

With this cost evaluation, I managed to save up to `62.40%` in EC2 costs, going from `$41,472.00` to `$15,593.42`.

It’s clear that you will need to pay `$15,593.42` all upfront, but depending on your situation, this can be a proficient way to save on cloud costs.

On a personal level, it was the first time that I’ve spent more than `$15.000` in one shot for someone else. It’s two years now that we’re saving and managers are happy about it 😃

# Conclusion

This article is part of a series where I write about production use cases as a cloud engineer. The focus is on meeting business needs, working as a team, and personal growth.

If you find this article useful or want to share your thoughts about it, feel free to write a comment. I would be happy to read it.

As a side note, I'd like to add that this article was written with the help of AI.

See you in the next chapter!