Why Use A Terraform Template?
After publishing our introductory article on Terraform templates, we received questions about "why use a Terraform template?" We explain why Terraform templates are useful.
The past few months, we’ve covered various Terraform and DevOps concepts through our blogs.
After we published our article on “Terraform Templates”, we had questions like--” Why should I use a Terraform template?” In simple terms, our answer would be: to spin off your infrastructure on the cloud as code, in a simple and efficient manner. But, is it really that simple?
We believe the topic deserved an entire blog to fully explain why Terraform templates are a necessity.
Interested in learning more about Terraform templates? Join our Slack to discuss the topic in-depth and meet other DevOps community members.
A Brief Background on Terraform Templates--IT Infrastructure? Why not code it?
Remember when IT infrastructure was set up manually? Engineers had to physically get and configure the hardware as per their OS. They would then have to deploy their application on these servers. It was probably around 2 decades ago, a time when most of us at InfraCode had not even started working in IT. But, as the size of infrastructure kept growing, they had to add more servers and more hardware in order to continue supporting their applications. This meant more physical space was required, more manual work, and maintenance that just kept getting crazier.
What was the solution? Cloud and Utility computing came into existence. The infrastructure was now virtual. It was hosted on the cloud and the service providers started providing these services as a utility computing service. This meant that the users would only pay for the resources or services that they used and never a flat price.
Source: Utility Computing
With cloud and virtual resources in the picture, the next challenge was maintaining this ever-growing field digitally. That’s how the idea of maintaining infrastructure as code came into existence. Infrastructure engineers and administrators were enamored with the idea.
Infrastructure as Code (IaC) is the management of your infrastructure in a descriptive model, where the same source code generates the same binary. IaC enables the fact that your infrastructure is governed by the same rules as the rest of your code. There are two ways to achieve this:
1. Declarative: In this, we define the resources and their properties in a declarative manner like any other functional programming language. Example- Terraform.
2. Imperative: When it comes to this approach, we are rather specifying the commands that need to be executed to achieve a particular configuration. Example- AWS CDK.
The declarative approach has been accepted more widely than the imperative approach because it focuses on the question of “what” is required, rather than the question of “how” to achieve a desired state. This makes the declarative an easy and efficient way of coding your infrastructure.
Terraform is Born, And Then Came Terraform Templates
In all the hustle and bustle of setting up your Infrastructure as Code on the available cloud providers and with more providers entering the market all the time, HashiCorp created the Terraform tool. Terraform is an open-source tool that is specifically dedicated to creating, modifying, and even deleting infrastructure on the cloud, focusing on IaC. Users who attempt to build their infrastructure use a declarative language known as HashiCorp Configuration Language (HCL).
This new tool came with the possibility of writing your infrastructure in a descriptive way inside a “Terraform Template”. A Terraform template is a collection of different files that together, define the state of your infrastructure. We’ve already covered how to create these templates, so let’s dive into the reasons why to use them:
1. Cloud Agnostic: AWS? Azure? GCP? Some other cloud? No worries, Terraform templates support most clouds. For a discussion of the value-add of Cloud Agnosticism, read our latest article. Check the registry to discover what cloud providers are currently supported.
2. Declarative approach: Yes, all you need to do is declare the state you want to achieve. How it’s achieved isn’t a concern for the user. It makes the life of DevOps simpler in comparison to other popular tools such as Ansible and Chef which follow an imperative approach. In the imperative approach, we need to define how the state has to be achieved procedurally. Terraform is typically used as a provisioning tool so we don’t have to be bothered with configuring the servers. It instead provisions your complete infrastructure on its own.
3. Self-documentation: Since it’s declarative in nature, it is simple to read and usually no additional documentation is required. However, to make the Terraform templates more user-friendly you may add comments to your code.
4. The State File: We know what you are thinking. “These InfraCode people just won’t stop talking about the state file!” Well, that’s because of just how amazing the “Terraform State” file is. Every bit of configuration that you’ve applied, changed, or deleted gets logged. You have a history of what exactly happened to your configuration. That is how maintenance becomes easy, reliable, and well accounted for.
5. No additional server: Many popular tools such as Puppet, Chef, etc, require you to host an additional server in order to maintain the state of your infrastructure. All the changes that will happen in the future are applied to this server and then they are pushed to the rest of your servers. This means additional maintenance and cost. But, this is not the case with Terraform. It does not require any additional servers to maintain the state and everything is already a part of your complete configuration.
6. Cloud provider is your agent: It is not just about hosting additional servers; certain tools even require you to install additional agents. These agents will be responsible for installing the updates made to your configuration, again leading to additional cost and maintenance. In TF, your cloud provider acts as your agent and does this activity for you.
With so much to offer, personally, we feel Terraform is currently one of the best IaC tools. Does it have flaws? Yes, of course, every tool is flawed in some way. But that does not make it a bad tool, it makes it a tool that will evolve and become better as time passes.
We are excited to help others learn Terraform and contribute to trying to make the lives of DevOps engineers a little easier. We have a very active Slack community of DevOps engineers. We invite you to join us there to discuss, learn, and contribute.