Let's start Terraform !

Pratik Singh - Aug 17 '22 - - Dev Community

The article discusses the purpose, usage, and basics of Terraform. 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:

"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 .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

Terrafrom managin plans

Terraform's History ๐Ÿ‘จ๐Ÿปโ€๐Ÿฆณ

As per Mitchell Hashimoto, the co-creator of HashiCorp Terraform. Terraform was built once AWS came up with the 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

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

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

  • Terraform Commands One might learn about all the Terraform CLI commands over Here Let's discuss the basic commands:

terraform init : It is used to initialize a working directory containing Terraform configuration files. Not exactly but it's like git init command (just a metaphor)

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

terraform apply: 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.

  • Terraform Providers ๐Ÿ“Ÿ
    These are like npm or python libraries that are saved on Terraform Registry, 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

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

If you still wanna take a deeper dive in Terraform, take a look into this: Here

Thanks for reading my article :)

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .