what-is-terraform-and-how-terraform-works
Getting your Trinity Audio player ready...

What is Terraform and How it Works?

In this article we are going to discuss about terraform.This will be really helpful to understand many thing related to terraform like what is terraform?, how terraform works? why to use terraform? and many more .

Topics covered in this article is as below.

  • Brief History of Terraform?

  • What is Terraform?

  • How Terraform Works?

  • Why to use Terraform?

  • Difference between Terraform and CloudFormation

  • Major Terraform Components

  • Terraform WorkFlow Lifecycle

  • Syntax Of Terraform Configuration File

  • How to Install Terraform in Linux?

  • Most Useful  Basic Common Terraform Commands

  • Conclusion

Brief History of Terraform?

As all of you must have idea about the growth of cloud technology in last decade rapidly situation arises to manage their cloud infrastructure efficiently once including big corporate organizations started migrating their IT infrastructure to Cloud with the help of major cloud services providers like aws,azure,gcp as part of shared responsibility.

Automation is the best option for organizations have big IT infrastructure as they have variying need of IT infrastructure and need some sorts of automation as manual deployment leads to inconsistencies and lot of man power requirements and eliminating human error possibilities.To address above mentioned conserns is addressed by Terraform.

Terraform is cloud infrastructure provisioning automation tool developed by HashiCorp in 2014.Terrform tool is developed in GO Language by Mitchell Hasimoto.Terraform supports declarative configuration language named as HCL(HashiCorp Configuration Language) on frontend level.

What is Terraform?

Terraform is nothing but an cloud infrastructure provisioning tool used for cloud automation means you can build or change cloud based infrastructure.You can automate your cloud infrastructure using terraform tool.It is also helpful for version control as well so you can track history of modifications.

Terraform helps to manage cloud infrastructure effectively and reduces workload and eliminate human error possibilities.It is declarative kind of language and it is open source.

You need not to specify action details you just need to pass statements and it will perform actions based on instructions.

To summarize how Terraform helps in Cloud Automation is as below

1.Terraform Helps in Making Cloud Automation Writings Very Easy.
2.Terraform Helps in Making Infrastructure Modifications Very Easy.
3.Infrastructure as Code makes your infrastructure more reliable.
4.Infrastructure as Code makes your cloud infrastructure more manageable.
5.You can easily replicate to various environments makes it best suitable as part of your disaster recovery solutions.
6.Terraform provides best staging environments.
7.Terraform manage states of infrastructure efficiently.
8.Terraform maintains infrastructure modifications history using version control.

How Terraform Works?

In general Terraform connects to Cloud Providers and then create infrastructure as defined via codes.

Core Engine of terraform mainly takes config parameter file named as TF-Config which have details what to configure and State file.

It makes an plan like what needs to be created/modified or removed.There is two state defined current state and desired state. It works with most of the cloud infrastructure services providers like AWS,Azure,GCP etc.You can check this link to have more clarity how terraform works.

Why to use Terraform?

Below are few main reasons why to use terraform.Below are few major advantages of using terraform.

  • It is Open Source:  You can customize your infrastructure requirements no need to pay except for enterprise license.
  • Multi-tier Application: You can launch and destroy multi-tier application using single commands.Using same you can deploy as well means it is reusable.
  • IAC Benifts : Infrastructure As Code is the latest trends and it becomes basic needs for enterprise level organizations as it is really difficult to meet out any surge in demand of IT infrastructure,DR solutions with effectiveness,ease and consistency in provisioning and keeping check on billing as well.
  • Automated Deployments : This is the basic requirements of organizations still needs to be carried out .Do you recall OnPremises infrastructures and such tools like Kickstart,Jumpstart. So on cloud platforms same capability needed where Terraform fits in.
  • Consistent Environments : Consistency in environment is one of the key factors you need to maintain for best performance and also to manage them efficiently.
  • Repeatable Process: You can develop your own stretegy for repeatable process for smooth Cloud Infrastructure.
  • Reusable Components : You can develop your own reusable components. Just for example take the case of disaster recovery you can develop infrastructure as code in form of teraform may be good excercise needed at once but will be ready if needed to replicate your environment in case of any disaster to maintain operability and availability of IT infrastructure .You need not to keep it always active and paying heavily for another replicated setup.It will cut cost of IT infrastrucure by 50%.
  • Documented Architecture :  Version Control features like GIT it helps to have everything documented.It maintains history of all the modifications etc.

Difference between Terraform and CloudFormation

Here is the basic difference between Terraform and CloudFormation.As you start using it you will be presented with the features you can use with Terraform as compared to CloudFormation like tools from Cloud Infrastructure providers like AWS, Azure, GCP, VMware, Oracle & Alibaba as well.

CloudFormation

Terraform

It is free for AWS users.Their will be no extra billings to use it in your AWS environment. Terraform is also free and open source.
Cloud Environment Dependency.It only works in AWS Cloud Environment. Terraform supports multiple Cloud Environment including AWS,Azure & GCP as well.Also for many other cloud infrastructure services providers.

CloudFormation is the way to go for AWS environments.

Terraform is authorised AWS,Azure,VMware, GCP,Oracle and even Alibaba.It is way more advanced compared to CloudFormation with additional features.This you can treat advanced version of cloudformation like tools from clod service providers. 

Major Terraform Components

You can categorize Terraform into four major categories.Major Terraform Components as as below.

  • Terraform Executable

  • Terroform Files

  • Terraform Plugins

  • Terraform State

Above components kind of self explanatory.One of the very important instructions while working with terraform is that never ever change state file manually as it is puposefully build by terrform and you always focus to change any thing in terraforms only. Terraform Plugins is the backend support of cloud environment like AWS,Azure & GCP.

One of the best feature about terraform is that it provides opportunity to do dry run and see what is going to be implemented .Also it provides facility to fix indentations issues in script or config file you are creating for deployment purpose.

Terraform WorkFlow Lifecycle

Terraform Workflow simply means sequence in which you can use to start working on terraform.Terraform workflow lifecycle can be grouped into 5 steps and these are as below.

  • WRITE            –   Write Terraform Config File 
  • INIT                 –   Initialize Terraform
  • PLAN               –   Dry Test Terraform /Terraform Plan
  • APPLY             –   Apply Terraform
  • DESTROY       –  Destroy Terraform Implementations

Syntax Of Terraform Configuration File

Terraform config file is the building block of automation.This must have .tf extention.There are terraform commands which helps you to ensure right indentation for the file like teraform fmt command.Even terraform provides command to validate your config file and know in advance what it is going to do .In terms of programming language it is called dry run.

Lets see what should be ideal syntax you can follow for creating your first deployment config file.As soon as you experiment and include component in your provisioning length of config file grows.One thing to remeber considering security and better implementation not to include your credentials openly instead of storing them in variables.You can divide them in 6 main categories 

Variables
You can define credentials variables, default region variables.
Provider
You can define variables like providers and define credentials holding variables 
Datasource
Define your provisioning here like instances 
filter
You can define filters here.It could be one or many.
Resources
You can define resources types here what you are going to use and from where
Output
You can define output here

How to Install Terraform in Linux?

This is really very easy .Here is the example how to install terraform in linux. I am considering here Red Hat Enterprise Linux. 

How To List Availability of Terraform in YUM Repository

# yum --enablerepo=* list terraform

How To Install Terraform in RHEL Using YUM Repository
Please refer to below example for how to install Terraform in linux using your YUM repository.You need to confirm with yes and that`s it terraform will gets installed with any dependencies involved with it.

# yum --enablerepo=* install terraform

How To Verify Terraform Installation in Linux (RHEL)

Like all other utility installations verifications which comes in mind is finding program versions.Same here as well.You can verify terraform installations in rhel you can do as below.

# terraform -v
Terraform v0.13.3
#terraform -version
Terraform v0.13.3

You can see above both is equivalent you can use either of it which ever you like 
to use to find out version of terraform.

# terraform -help
Usage: terraform [-version] [-help] <command> [args] 
The available commands for execution are listed below.The most common, useful
commands are shown first, followed byless common or more advanced commands
.If you're just gettingstarted with Terraform, stick with the common commands. For 
theother commands,please read the help and docs before usage.
#

Most Useful  Basic Common Terraform Commands

Refering to previous section you have been already introduced with most useful basic common terraform commands .For example if anyone ask you what is the terraform version? you are using in your cloud environment you can easily find out the same with -v or -version option.You can refer to above examples.For reference i will add below as well.

How To Find What Terraform Version You Are Using?

You can see above both is equivalent you can use either of it which ever you like to use to find out version of terraform.

# terraform -v 
Terraform v0.13.3 
#terraform -version 
Terraform v0.13.3 

Like all other linux basic commands you will have may similarities for terrafom commands as well.For instance terrform -help which will give you most common terraform commands list you can explore.Terraform list of commands are not vaste like linux command which are huge. Please refer to below example where you can see syntax of terraform commands.

How to Find Most Common Terraform Commands?

# terraform -help 
Usage: terraform [-version] [-help] <command> [args]
The available commands for execution are listed below.The most common, useful
commands are shown first, followed byless common or more advanced commands
. If you're just gettingstarted with Terraform, stick with the common commands. For
theother commands ,please read the help and docs before usage. 
#

Lets discuss more basic terraform commands to explore more into terraform cli.

How to Initialize Terraform?

This is the first command you have to run for initializing your configuration file you have created for infrastructure proviioning in specified directory.

# terraform init

How To Verify Terraform Implementation & Creating State File

# terraform plan

Using terraform plan command you can run dry test of script and see what resources it is going to provision before creating resources.You will get opportunity to eliminate any error in your config file.You can use terraform validate command and terraform fmt command here to ensure correctness of your script.

# terraform validate
Success! The configuration is valid.
# terraform fmt main.tf

This is helpful to fix any indentation issues in your main config file. 

Once you run plan command you need to focus plan area like below.Let us understand from below.See below output it shows 0 to add 0 to change 0 to destroy .It should change based on your config file.

Plan: 0 to add, 0 to change, 0 to destroy

How To Create/Deploy Cloud Infrastructure

# terraform apply

APPLY command exactly provisions Cloud Infrastructure exactly same way as you have definded in your configuration.So it leads to consistent deployments and eliminates any human error in deployment.

How To Destroy Cloud Infrastructure Provisioned using Terraform

# terraform destroy
==========================================
====Output is truncated for better visibility.=====
Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.There is 
no undo.Only 'yes' will be accepted to confirm.
Enter a value: yes

Once confirmed with yes with no other options available and you are done.

This is last step of terraform provisioning lifecycle.This gives ease to remove or you can say destroy the provisioned with the help of single command to have good control over billings.You just need to confirm only option available.

There are few other basic terraform commands not too many .Once you start practicing you will definately presented with those as requirement comes.

Conclusion

We went through most of the topics in this article more presizely brief history of Terraform?, What is Terraform?, How Terraform Works?, Why to use Terraform?, Difference between Terraform and CloudFormation?, Major Terraform Components?, How to Install Terraform in Linux?, Most Useful Basic Common Terraform Commands?.Terraform is an open source cloud infrastructure automation tool which is latest and having better featured as compared to cloud infrastructure providers like AWS,Azure & GCP .

Current IT industry is focusing more on open source developments which gives littlebit freedom to be specific specialization.For example if you are well aware aboud CloudFormation and working on AWS after some time you may get GCP or Azure environment then again you need to re-invent the wheel so its better to go with Terraform .

Once you start exploring you will get familiar with each features.Cloud is new normal now so you must learn about cloud automation stuff which you can use terraform instead of vendor provided automation tool because you never know with any job change you may or may not have specific cloud services in organization you need to work in future.

Terraform is totally different from other IT automations tools like ansible which focused application based automations while terraform focuses on IT infrastructure provisioning automations.