What Is Terraform?

This article explores Infrastructure as Code, why is it important, its advantages, and then talks about a about a powerful tool to use (IaC) in practice – Terraform. We'll explore Terraform and its various key features. Lastly, we'll go through a hands-on process of Terraform Workflow with the Write, Plan, and Apply with storing it in version control.  

Don’t Repeat Yourself (DRY) 

In the previous article, Design Principles, we discussed DRY and many other software design principles. Don’t Repeat Yourself or DRY is basically an integral principle in software design and programming that states that one shouldn’t duplicate or rewrite the same functionality twice. It aims to minimize the repetition of software patterns. It is widely acknowledged that repeating code all over your application is problematic. With the DRY principle, a system can be designed with a single, authoritative and unambiguous representation for every piece of knowledge.   

We programmers conventionally do not like to repeat. We practice that on all fronts of life. Numerous among us send Birthday and Festivity Wishes through automation. So, what if we could do that for Infrastructure setup such as Azure.  

What is Infrastructure as Code? 

Infrastructure as Code also known as IaC started as a concept of modeling the physical infrastructure using code that has the capability for designing, implementing, and deploying application infrastructure following the best software practices. The IaC process help manages and provision the computer data centers through readable definition files for machines instead of physical human interaction with the hardware configuration and related tools.  

Advantages of Infrastructure as Code 

One of the prime benefits of IaC is that it enables continuous delivery. Moreover, it also helps to solve the problem of environment drift. Furthermore, the IaC enables testing of application in production-like environments extremely convenient. The IaC helps reduce the cost both in terms of finance as well of the effort of human resources. With the removal of the compulsory inclusion of human interaction and manual components, the engineers, developers, and operation executives can now focus on other enterprise tasks with the automation of the mundane infrastructure setup work. Besides, the automation also largely reduced the risks of human errors, manual misconfiguration, and increase reliability with a better culture of implementation of DevOps that has now been an industry standard for quite some time by 2021, all thanks to IaC.  

What is Terraform? 

Terraform enables collaboration within the teams of organizations with CI/ CD pipeline automation through the capability as a software tool for infrastructure as a code. It is open-source software and allows users to define and supply the data center infrastructure through declarative configuration language which is known as the HashiCorp Configuration Language (HCL) as well as through JSON. Hundreds of Cloud Services can be managed through the CLI workflow provided by Terraform.  

Key Features of Terraform  

As an Infrastructure as Code tool Terraform helps to create a blueprint for infrastructure setup. It makes version sharing possible and reusable across teams to make sure all teams as same infrastructure in place. Moreover, the generation of execution plan can be done before any changes are implemented. All the required steps that should be taken care of before the implementation can be thoroughly planned and then well executed with the capabilities provided by Terraform. Furthermore, terraform allows efficient resource building. All the manual tedious tasks of building and management of resources of infrastructure can now be automated with Terraform. Furthermore, complex changes can now be applied easily with all the functionalities accessible through Terraform.  

If you want to learn more about Terraform, watch this video,

The Terraform Workflow 

The Terraform Workflow can be broken down into core three steps. In fact, there are three different ways of Terraform workflow, however. All separate processes for different needs and situations as per for Individual Practitioner, For Teams and Terraform Cloud. Let us have a look at Individual Practitioner below.  

Write 

The Terraform configuration can be written just like we write code, in any editor we choose to. Moreover, there is a common practice to store the configuration in version control.  

# Creating repository
$ git init my-infra && cd my-infra

Initialized empty Git repository in /.../my-infra/.git/

# Writing initial config
$ vim main.tf

# Initialize Terraform
$ terraform init

Initializing provider plugins...
# .....
Terraform has been successfully initialized!

For any changes to edit the configuration or review plans as well as making additional edits and repeat throughout the authoring, a tight feedback loop is extremely useful for running test commands and editing code which can be done with the following.  

# Making edits to configuration - config
$ vim main.tf

# Reviewing plan
$ terraform plan

# Making additional edits, and repeat
$ vim main.tf

Plan 

As the Write step is completed with all the feedback loops necessary, the work should be committed and sent for review.  

$ git add main.tf
$ git commit -m 'Managing infrastructure as code!!!'

[main (root-commit) f735520] Managing infrastructure as code through Terraform!!!
 1 file changed, 1 insertion(+)

For final review, we run the command, terraform apply 

$ terraform apply

An execution plan has been generated and is shown below.
# ...

Apply 

After the final recheck, we can setup the real infrastructure with Terraform.  

Do you want to perform these actions?

  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.
  Enter a value: yes

# ...

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

 

$ git remote add origin https://github.com/*user*/*repo*.git
$ git push origin main

Note: Push the version control repository to a remote location to keep it safe. Also, notice how we performed it with simple configuration write just like writing scripts and code. This is what Infrastructure as Code (IaC) means – a terrific example with Terraform. 

Conclusion

In this article, we learned about Infrastructure as a Code (IaC), the advantages of IaC and then dived into Terraform. We understood what Terraform is and explored its capabilities and key features. Moreover, we learned about the Terraform Workflow – the steps of Writing, Planning, and Applying and then went through a hands-on experience to storing it in version control.  


Similar Articles