How I cut my cloud costs by 70%

Use a combination of Terraform and Ansible to reduce the cloud costs.

How I cut my cloud costs by 70%
Photo by Ibrahim Rifath / Unsplash

As some of my regular readers may know, and as I've subtly hinted at in my previous posts, I host this blog myself on DigitalOcean. Because there is no blogging service behind this blog there are some costs associated with the hosting and maintaining the infrastructure of the blog. In this post I'm going to detail the infrastructure, the cost associated with it, and how I managed to reduce it.

⚠️
Spoiler Aler: Kubernetes is expensive on a small scale!

Kubernetes for hosting the blog

While creating your own custom cluster with Kubernetes is possible, it is generally not recommended because of the maintenance overhead, and because of the plethora of managed options out there. Digital Ocean has one such option for running a managed cluster.

The caveat here is that a minimum, stable, recommended configuration of a cluster will cost $20 USD minimum. This cost does not include load-balancer costs, extra volume costs, and database costs. Plus only half the RAM is usable.

Digital Ocean Kubernetes configuration

Once I factored in the attached storage, and load-balancer costs, the final cost of hosting came to be about $42/month. This in my opinion is a lot for hosting a blog. Especially when converting to Canadian dollars.

This cost does provide a super easy method to install, maintain/update, scale, and monitor my Ghost installation. Helm makes things extremely simple when it comes to installing complex, web based applications. Deploying the database, load-balancing incoming requests, scaling as necessary, and exposing metrics, Kubernetes + Helm solve a lot of problems.

The question is, does my application really need all that? To answer the question I set out to change my infrastructure completely. Using a simple VM, Docker, and caching layer, I wanted to see how much I could reduce my costs and still retain, relatively speaking, the ease and automation capability of Kubernetes.

Shameless plug

If you like content on this blog you can support us by using the DO referral link to sign up and get free credit. And you'll support us as well.

Referral link: https://m.do.co/c/590c0c82c1fc

Also you can subscribe directly signing up for free, or support us with a small monthly or yearly fee. Simply click the button at the top right corner.

Implementation

The implementation is essentially going back to running VMs using modern automation tools. I will be using two main tools, Terraform, and Ansible. Terraform will be used to create the VM and connections to other parts of the infrastructure setup. I run this blog on Digital Ocean, so Terraform will be used to create a droplet, and create a monitor alert for this droplet. Ansible will then be used to SSH into the droplet and setup Ghost on this machine.

GitHub - mbbaig/ghost-deployment: A Terraform and Ansible project to deploy the Ghost platform
A Terraform and Ansible project to deploy the Ghost platform - GitHub - mbbaig/ghost-deployment: A Terraform and Ansible project to deploy the Ghost platform
Github link

The repository above contains all the code for the project. The blogInfra.tf file contains the Terraform spec for the infrastructure. The playbook folder contains all the Ansible files. The commands below can be used to deploy the project on Digital Ocean.

terraform apply --var="do_token=$GHOST_DIGITAL_OCEAN_TOKEN" --var="ssh_key_id=$GHOST_SSH_KEY_ID" --var="alert_email=$GHOST_ALERT_EMAIL"

The GHOST_DIGITAL_OCEAN_TOKEN, GHOST_SSH_KEY_ID, and GHOST_ALERT_EMAIL must be set for a successful deployment.

The following command can be used to run the playbook and deploy Ghost and the database.

ansible-playbook -i "$GHOST_SERVER_IP," --user root ./playbook.production.yml

The SSH key must match the key used with the Terraform command.

Cloudflare is the DNS and caching layer for the blog as well.

Future improvements

In the near future I will be making updates to this project to make the following updates.

  • Automate deployment
  • Add Cloudflare to the terraform spec
  • Update the playbook to be idempodent

Subscribe

I will making this content like this and more so please consider supporting me by subscribing.

Sign up by using the button at the top right corner. Also consider supporting with a small monthly fee.

Happy Hacking :)