Terraform : Why and How to Use It

source: unsplash

There are a lot of infrastructures that can be created interactively using UI. However, you need to click several buttons and type in several fields to configure an infrastructure with the UI provided. Even to familiarise the provider’s customers to the infrastructures UI, some of the providers conduct a seminar about step by step to create infrastructures with the providers’ services.

Apart from the familiarity of the providers’ UI, you’ll also find it difficult to change the infrastructures configurations once you have massive infrastructures. Let’s say you want to create a cluster with a specific configurations but you are not sure whether you already created that cluster or not. It will consume you minutes of clicking the UI button to only find the information that the cluster already existed or not. Imagine if you want to create 10 or more clusters, how many hours will you spend to just check each clusters’ configurations in your infrastructures to make sure that there won’t be any duplication 😱

But hey Aya, what if the configurations were easily accessible so that we don’t need to check it?

Okay, okay, let’s say your team documented all the configurations well. But still, if you are responsible in making configuration changes for 100 or more clusters that your team owns, it definitely will make you beg to be assigned for another job 😕

Welp, right now you might think that operations job is tedious. *It is, sometimes 😛* But worry not babe, Terraform is now here to rescue and introduce you to IaC, Infrastructure as Code.

In short, Terraform is

A tool created by hashicorp to ease operations people in building, changing, and versioning infrastructures.

Actually there are other more reasons on why you need to consider using infrastructure as a code or Terraform. But since I already elaborated some of them, let’s see how we can use Terraform!

How to Use Terraform

Actually, terraform already provided us a comprehensible tutorial on their website! Check it out here!

Even if the tutorial is well written, let’s discuss what things need to be done to create a resource. The following written snippet of the examples are typed in main.tf file.

Define the provider

Before writing the infrastructure we want to establish, we need to define what is the provider which responsible for understanding the API and exposing the resources of the infrastructures. Providers in Terraform are not only liable on creating infrastructures, but also creating platforms and services. For further reading about the providers in Terraform, please refer to this link.

To define a provider, you need to incorporate the credentials needed to call the API of creating resources. The way of declaring the credentials depends on the provider you use.

Here is the example of declaring provider in GCP.

Write the resources

After your desired provider is set, you are now able to define the resources. The resources that are available, once again, are depending on the provider you use. In declaring resources you need to type in the resource type and resource name.

As you can see from the example of declaring resource in GCP above, the resource that we want to create is a compute network in GCP and we give the name for that resource “vpc_network”. For some resources, you also need to define the arguments within the block of that resources. From the given example, we are passing the resource name in the GCP which is “terraform-network”.

You might get confused that you are declaring two names for the same resource. Actually, the name argument that you are passing is different from the name of the resource that you declare after typing the resource type. The name argument will be shown in your google console while the resource name will be accessible within your terraform.

For example you want to create another resource that need to pass the GCP compute network name in google console, you can simply pass the name as shown below.

Init, plan and apply the resources!

After the terraform configurations are ready (the resources you declared), you need to run terraform init command in the directory where terraform configurations files exist to initialise the local settings.

If you installed terraform with version below 0.12, after running terraform init command, you need to run terraform plan . By running terraform plan , terraform will show you the execution plan that terraform will do once you approve its plan. When you are running terraform plan there are 4 execution that terraform will probably do and terraform will show you the symbols of the execution

  1. ~ : terraform will do some changes within the resource
  2. + : terraform will create the resource you request
  3. - : terraform will destroy the resource you remove
  4. -/+ : terraform will replace the resource with the new configurations

Once you see the execution plan, you need to decide whether you want to apply the plan or not. If you want to proceed with the plan, you simply have to run terraform apply command. If the installed terraform on your machine are 0.12 or above, you don’t need to run terraform plan since once you run terraform apply , terraform will show you the execution plan and need your approval by prompting you to type “yes” with the plan.

To conclude, Terraform is a convenient way to create infrastructures, platforms, and services once you need to manage them in large scale. With Terraform, you don’t need to manually create them one by one, instead, you only need to declare the terraform configurations and run the terraform command and voila, they are automatically managed and established.

References

Software Engineer | Traveler | Lifelong Learner

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store