Getting started with Terraform

Priyanka Salunke
4 min readApr 17, 2021

Let’s begin our discussion with Infrastructure as Code (IaC). IaC is the process of managing and provisioning computer data centers through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools.

Goal of Infrastructure as Code: The goal of IaC is to make things faster by eliminating manual processes and eliminating the slack in the process.

Examples of Infrastructure as Code: AWS CloudFormation, HashiCorp Terraform, Red Hat Ansible, Chef, Puppet, SaltStack and many others.

You can follow this link to get information about the Infrastructure as Code (IaC).

What is Terraform ?

Terraform by HashiCorp

Terraform is a tool for provisioning infrastructure(or managing Infrastructure as Code). It supports multiple providers(E.g.: AWS, Google Cloud, Azure, OpenStack, etc.).

Terraform is an open-source infrastructure as code (IaC) software tool that provides a consistent CLI workflow to manage hundreds of cloud services. Terraform is a tool developed by HashiCorp for building, changing, and versioning infrastructure safely and efficiently.

For example, you are working in GCP and would like to spin up several VM instances of a specific type. You would define the type and number of instances in a configuration file and Terraform would use that to communicate with the API to create those instances. The same file could then be used to modify the configuration, for example increasing or decreasing the number of instances.

Why do we use Terraform ?

We use a Terraform feature called modules to combine multiple infrastructure components into large, reusable, and shareable chunks. Terraform module is a set of Terraform configuration files in a single directory. Even a simple configuration consisting of a single directory with one or more .tf (terraform) files is a module.

Comparison of IaC Tools

Let’s start the installation of Terraform and write our first configuration. Check this link to get information about the installation process of the Terraform.

It is important to note that terraform is a declarative language. It means that we need to define what infrastructure we want and terraform will figure out how to create it. All terraform code ends with the extension of .tf.

Terraform Workflow

Terraform core workflow has 3 important steps:

  1. Write : Write infrastructure as code using declarative configuration files.
  2. Plan : Check whether the execution plan for a configuration matches your expectations before provisioning or changing infrastructure by running “terraform plan” command.
  3. Apply : Apply changes to cloud providers with “terraform apply” to reach the desired state of the configuration.

Example

Now, Let’s create a terraform configuration file named as “main.tf” and include the configuration.

provider "google" {
project = "{{YOUR_GCP_PROJECT}}"
region = "us-central1"
zone = "us-central1-a"
}

Provider : Terraform supports lot of providers such as Google Cloud Platform, AWS, Azure and many more. Here, I used GCP cloud provider. Remember to replace “YOUR_GCP_PROJECT” with the name of your project.

After this, create a new VM instance. For this, add the following to config file:

resource "google_compute_instance" "vm_instance" {
name = "terraform-instance"
machine type = "f1-micro"

boot_disk {
initialize_params {
image = "Debian-cloud/debian-9"
}
}

network_interface {
# A default network is created for all GCP projects
network = "default"
access_config {
# Ephemeral IP
}
}
}

Resource : The resource block defines a resource that exists within the infrastructure. A resource might be a physical component such as VM instance, or it can be a logical resource such as a Heroku application or any other application.

So, Here we define a resource google_compute_instance which gives us a virtual Machine instance. The machine type for this VM will be f1-micro and we will install Debian-cloud/debian-9 as the boot disk image.

Commands:

  1. Initialization: The first command to run the new configuration is terraform init. This command will install all the dependencies required for our provider. Run the following:
terraform init

2. Plan: Now, we can check whether the written configurations will result into our desired needs. To check it, run the following:

terraform plan

3. Apply: This command will show us all the changes that the configuration will apply. After verifying whether it is needed or not. Finally, run the following:

terraform apply

This will apply all of our changes made in configuration to GCP. We can go to GCP console and see that a virtual machine with the specified configurations is created successfully.

You can also check that the file named “terraform.tfstate” is created in directory structure. This is an important file which stores the state of our configurations so that when we apply our configuration the next time, it will check on this file and make changes only to those resources whose configuration is changed. We must not edit this file manually, terraform will do it for us automatically.

There is so much we can do with Terraform. You can check more about the concepts of Terraform from documentation.

Connect with me:

LinkedIn: https://www.linkedin.com/in/priyankasalunke25

Keep Learning, Keep Growing!!

--

--