The journey to a cheap(er) infrastructure

Nick Huizing
Growens Innovation Blog
4 min readFeb 15, 2021
Image credit LendingMemo.com

The cost of a migration to AWS will increase very fast, without a good optimization after or during the migration. There are several options to choose from, like spot instances or reserve the instances. By using the Cost Calculator and the number of optimization options offered by AWS, we at Datatrics decreased our monthly costs by 80%.

Prologue

Within Datatrics we recently moved our IT-infrastructure to Amazon Web Services, better known as AWS. We chose AWS because the MailUp Group is widely using Amazon Web Services, so it was an easy choice. Beside being used inside the group, it also offers better uptime, scalability and manageability than our previous Cloud Provider.

Within AWS we try to use mostly managed services, but when it is easier and mostly cheaper to deploy for example a database like MongoDB on an EC2 instance instead of using AWS DocumentDB, we go for that option.

Other services we used for running our infrastructure beside EC2 are: RDS Aurora, ElastiCache, SQS, EKS, CloudFront, ECR, ECS, ELB (ALB & NLB), ElasticSearch Service, S3 and Lambda.

We chose to use Terraform for deploying our systems on AWS. Terraform is a tool to implement infrastructure as code. This gives us the opportunity to see what will be changed, because Terraform is saving what is currently deployed in a state file and it gives also the opportunity to manage our infrastructure from a central place, like from our GitLab.

AWS Pricing

AWS offers three main pricing strategies for the available services:

  • Pay-as-you-go (On-demand)
    Pay for capacity based on the duration per hour
  • Reserve capacity
    Pay upfront and pay less each month
  • Pay less by using more
    When you save many data in for example S3, it will be cheaper when you have more data stored.

AWS also offers a free tier, which means you can use a select group of services for free every month. The resources in each service are limited to a minimum.

At the end of each month, the resources from the free tier are subtracted from the total costs. As a result, you automatically save a little. You will pay for the usage outside the free tier and for the usage of services which are not included in the free tier.

AWS also offers a cost calculator where you can calculate the monthly costs of the environment. In this calculator you can add the services and configure them to your usage.

The steps to a cheaper infrastructure

Our strategy was to move everything from our old cloud provider first, prior to optimizing the whole infrastructure in AWS.

At the beginning, after the data and application where moved to AWS, our daily expense was much higher than expected and desired.

Our first step was to determine the right instance types for our database instances, EC2 instances, Redis clusters and EKS. We decided to start low and increase it when the type was not sufficient. When we found the right instance type, we reserved those for a year.

For some applications and for our Kubernetes (EKS) workers, we make use of an Auto Scaling solution based on CPU usage or time of the day. We run less in the evening and night and more instances during business hours. We also reserved the minimal amount of instances in the Auto Scaling groups.

Also we make use of spot instances inside Auto Scaling Groups. A spot instance is an unused EC2 instance for less than the on-demand price. This will lower your EC2 costs significantly. There is one but, AWS has the possibility to shutdown a spot instance in a 2-minute warning. So, if the instance get the message, the instance will shutdown in 2 minutes.

For applications with a critical workload, like our APP or API, we use a mix of on-demand and spot instances, because we can’t afford that critical instances will shutdown within 2 minutes. So we decided to use on-demand instances for the minimum amount of instances in an Auto Scaling Group. We also use multiple instance types for spot instances, since it can happen that a pool of instances could run out of instances.

At the end, we also saw high costs for traffic between different availability zones. This because AWS also charges for the data traffic between the availability zones in a region. When we realized this, we decided to run everything in a single availability zone. It is a risk to run everything inside the same availability zone, since when the zone goes down, we need to re-deploy everything in a new availability zone. When you choose to do this, it is necessary to have a decent disaster recovery plan and a good back-up strategy.

Wrapping up

This article described how we at Datatrics decreased our daily expense. AWS offers many possibilities to do so, like Spot Instances, reserving the instances and the use of Auto Scaling Groups, so it can automatically scale up and down based on the load.

With the steps above, we optimized our infrastructure and we were able to reduce the costs by roughly 80%.

--

--