Let's start Terraform !
Tags: devops, microservices, architecture, computerscience
Published: 2022-08-17 12:51:00

The article discusses the purpose, usage, and basics of [Terraform](https://www.terraform.io/). It will be walking through the concept of Infrastructure as Code, and mainly learn to get started with Terraform.

## Introduction 👋🏻
The definition of Terraform as per their [website](https://www.terraform.io/intro):

> "It is an infrastructure as code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share. You can then use a consistent workflow to provision and manage all of your infrastructure throughout its life cycle."

Terraform is a tool that is used in many forms in the industry. Most of the companies use it to develop their **Infrastructure as Code**, while its uses don't only limit to this!

## Infrastructure as Code (IaC)
It is managing and provisioning of infrastructure through code instead of the hectic manual processes. In any company, or even personal project hosting one's product/project on the internet is necessary. Cloud providers in today's world give easy access to deploy your code on the internet and make the project/product available to the public in simple steps. But:
- What if the VM's instance fails, or the Database is deleted? 
- What if one wants to migrate to another cloud provider?

For any failure or migration, only way is to recreate the resource manually each time. IaC is a real-life saver in such scenarios. One file that helps to recreate the entire Infrastructure anytime.

## Why Terraform? 🦾 
Terraform not only saves the Infrastructure as code. The tool saves the infrastructure and manages the resources as declared. It orchestrates the changes and detects any drift in configuration. Also ensures the resources with the required specifications are not only up but also connected as declared.
Steps it follows in the process:
### Write🖋: 
Mention the configuration of the architecture in <your>.tf files

### Plan🤔:
It will create a plan to create the changes mentioned by the DevOps Engineer/Architect. One can also make changes in the plan and apply as wanted.

### Apply 🪄:
It applies the changes the scripts asked for and moving forward from this point maintains this state perpetually until advised else wise. 

Official Video on **Why Terraform**: [Here](https://youtu.be/h970ZBgKINg)


![Terrafrom managin plans](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qka9d4odf01nvhpeu7j.png)

## Terraform's History 👨🏻‍🦳
As per [Mitchell Hashimoto](https://twitter.com/mitchellh), the co-creator of HashiCorp Terraform. Terraform was built once AWS came up with the [Cloudformation](https://aws.amazon.com/cloudformation/).
He saw a need of an open source, cloud-agnostic solution; a tool that could provide the same workflows, no matter what cloud you were using. [Here](https://youtu.be/RNHQ91afYkE)

## Using Terraform 💻
Before starting with the basics, it's my responsibility as a developer to inform you about better sources. Hashicorp maintains amazing resources and tutorials to get anyone started: [Here](https://learn.hashicorp.com/collections/terraform/aws-get-started?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS)

Now, there are three most basic things to learn about Terraform:

-  **Terraform Commands**
One might learn about all the Terraform CLI commands over [Here](https://www.terraform.io/cdktf/cli-reference/commands)
Let's discuss the basic commands:

> <u>**terraform init**</u> :  It is used to initialize a working directory containing Terraform configuration files. Not exactly but it's like _git init_ command (just a metaphor)

> **<u>terraform plan</u>**: This step will actually create a plan to orchestrate and spin up resources as described in the scripts

> **<u>terraform apply</u>**: It is used to apply the changes. It will continue to keep the system in the same state until said to do so.

-  **Terraform Scripts** 📄
Yes, these are not very different from scripts in any other language. Like Python or any other programming language, we import packages called as *Terraform Providers*. These scripts use *HCL(Hashicorp Configuration Language)* Language. We can have variables and modules that are begin imported from other 
files as well.
<br>

-  **Terraform Providers** 📟
These are like npm or python libraries that are saved on [Terraform Registry](https://registry.terraform.io/), there are several providers available as per the requirements. Some are Official while others are community-built. It basically handles all the parameters passed to it in the terraform scripts. It will build the plan to apply the requested changes. 

> This is a basic overview of Terraform:

![Terraform Working](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x8dng5dm4hvbgd5lpsng.jpeg)

The Terraform Provider has further four most basic functions of Create, Read, Update and Delete Resources. We will probably talk more about Terraform providers and my failed attempt at it in another article.

## Conclusion:
Terraform is one of the tools in the DevOps domain. 
Is it the best tool or the only tool one needs to learn?
Absolutely not! 
**As Developers, it's our work to learn and utilise several tools, but it's also our responsibility to not evangelise a technology as magic.**

Terraform is a very good alternative when a smaller company wants to have a setup to automatically manage resources on the cloud. Since this is an automated way, even bigger company can have their smaller products and projects based on Terraform.
There is always an argument on Terraform being used for critical systems. According to me, it is always better to have custom in-house projects on a large scale.

Problems without using Terraform:
- Manually creating Cloud resources.
- Keeping a check on the resources, via UDP health checks.
- Manually restart services in case of failure.
- Reconnecting the pieces again every time after a failure.
- The migration of the entire infrastructure was a nightmare.

## 5 Problems one Solution -> **Terraform**
![Terraform](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3hrixgoqbi9hs9qcvhmd.jpeg) 

If you still wanna take a deeper dive in Terraform, take a look into this: [Here](https://spacelift.io/blog/terraform-tutorial)


Thanks for reading my article :)

> P.S.: All these great images are built using DALL-E