I came to know Terraform a few years ago and I quickly realized the advantages of this platform.
In this article I will explain what Terraform is, when to use it and some important lessons learned over the years.
What is terraform?
“Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently.” Excerpt from https://www.terraform.io/ website.
This means that instead of manually setting up your environment in the cloud provider’s console (e.g AWS, Azure, GCP), you are using an automated terraform script.
Important note: Terraform is intended to t
What are the advantages of using Terraform?
- Terraform allows you to quickly build and replicate environments in the cloud provider. Obviously, manual work is error prone. A script saves time on the second setup and helps maintain the state and versions of your infrastructure.
- Terraform manages infrastructure state for you. In terraform you don’t need to check whether the item is already created or not. Terraform automatically checks it for you, adds missing resources and removes resources that are no longer a part of your script.
- Using the cloud provider platform’s API (AWS Cloud Formation for example) you can also automate creation of cloud infrastructure. So, what is the benefit of terraform?
- Terraform is cross platform in the sense that it allows maintaining resources in several cloud providers in a single script and using a common syntax.
- Terraform has an excellent echo system with many prebuilt modules that can save you a lot of time.
- Terraform script is quite advanced in comparison to other APIs.
Ok, sounds good, what are the caveats?
- If you fantasized about true cross cloud platform, this is not the case. As of today, and in my opinion this is not going to change soon, each cloud provider requires using a different set of resources (resources are Terraform’s term for any infrastructure object). Which means that you can’t take AWS script and use it in GCP.
Got it, I want to use Terraform, what are the essentials?
- Use remote state – Terraform state keeps record of the current state of your infrastructure and is vital to the proper use if terraform. Using remote state in a secure location such as S3 would make sure you keep versions of your state file and that you won’t lose it due human error.
- Your script should always be a module – Terraform modules are reusable code blocks that allow setting up various resources together. I suggest to write all your code inside a module. The reason behind it is that as soon as you have 2 environments to maintain you need to use different command line arguments to run the script (since each environment requires a different state file and variables file). Using different command line arguments is highly error prone and can lead to serious issues such as deploying resources to the wrong environment, overwriting state file and more.
When using a module, you keep using the same commands for each of the different environments you are maintaining.
- Refer to platform provider when you can’t find terraform examples online – Although terraform is a popular tool, there will be times when you can’t find the proper code examples in the official documentation or online. In this event, referring to the providers documentation (e.g AWS, Azure, GCP) is the best way to understand the parameters that you need to send.
- Terraform is an excellent tool for IaaS.
- By understanding Terraform’s best practices and caveats described above you will be able to efficiently build and maintain your cloud infrastructure.
Feel free to share your thoughts!
Eyal is the founder and CEO of Sinapis Technologies, a boutique software company, specializing in web based development and cloud technologies.