Introduction
- Managing Infrastructure in Information Technology (IT) is the most complicated and very challenging process.
- Infrastructure is nothing but a collection of services that provides a runtime environment, either on any cloud providers or local on premises.
- Platform as a Service (PaaS) provides a runtime environment. It allows Programmers to easily create, test, run and deploy their applications. On PaaS Managing the services manually for the environment is very difficult.
- Here comes Infrastructure as Code (IaC). And for the IaC tool Terraform comes in. But what is IaC and Terraform? Let’s dive into this article and learn more about it.
Infrastructure as Code (IaC)
- Before moving on to Terraform, Let’s have some understanding about IaC. It helps for managing a complete infrastructure in an automation way.
- If you create an infrastructure manually, you have to create all the services one by one, it takes a lot of time and more work.
- But If you write a code for your infrastructure, you can create it by a single click and you can manage it very easily.
What is Terraform?
- Terraform is one of the leading tools for Infrastructure as Code. It is an open source tool that will be used by more than 150 cloud providers.
- AWS has its own IaC called Cloud Formation which is something like Terraform. But Terraform is more useful than Cloud formation. It will be used by the most popular automation tool Jenkins which is Cloud Formation not supported.
- Terraform uses a simple script language HCL (Hashicorp Configuration Language) which is provided by hashicorp.
- This is simple Human readable language.
- So, using terraform we can deploy, manage multiple services on any cloud providers like AWS, Azure and it supports multi cloud deployments also.
- You can find the official terraform code in GitHub using the following link
https://github.com/hashicorp/terraform
Workflow of Terraform
Workflow of terraform is the best and simplest way of managing infrastructure. Basically it has three steps of workflow.
Write ( Infrastructure as code )
This is the first stage of workflow, writing code for creating resources. Here we can create simply one EC2 Instance to very complicated infrastructure like Deploying an application on Kubernetes cluster. All of these are written in a language called HCL.
Plan ( Preview changes before applying )
This is to create an execution plan for the code, so developers can check what is going to be created using their code. This stage gives the full details about the code that developers wrote. Before going to apply the code to the cloud, definitely have to check the plan of the code. This is the best practice for the production.
Apply ( creating Reproducible infrastructure )
This is the final stage of deploying infrastructure to the cloud. Once developers will execute the plan for the code, they have manually confirmed to apply it on cloud. If they confirm it, then it will successfully create services based on the terraform code. So, using terraform you have to just write a code for the services that you need, and execute a plan to review the services that are going to create and apply it.
Terraform Components
-
Variables
These are used as input-variables. It allows you to give values to multiple resources by single value. It is a key-value pair.
-
Provider
This is an important part in terraform code, to interact with cloud services that you have defined in this section with APIs.
-
State
This is the one that caches all the information and the related configurations of the resources that are created by the terraform code.
-
Resources
The resource block is the main for creating any services on cloud. Every service has its own resource block.
-
Data source
This block helps to gather the information about the existing resources on the cloud in terraform code. These information can be used by other resources in code.
-
Module
It is a folder that contains all the defined configurations of terraform templates. It will be used multiple times for infrastructure. This above image represents the folder structure of the module. This image uses that folder to create infrastructures.
-
Outputs
It gives an output value of a specific resource’s detail. These will be return values of modules that can be used by other configurations.
Benefits of Using Terraform
- Terraform is a tool for building, Replicating and modifying the infrastructure safely and more efficiently in minimal time.
- Terraform is an open source service. It can create infrastructures in a multi cloud environment for more than 150+ cloud services like AWS, Azure, GCP, Digital Ocean, etc.
- It reduced the time to provision the infrastructures on cloud.
- We can reuse the configuration. Using the same configuration for an infrastructure we can create multiple environments.
- For automation process terraform is one of the biggest parts in that. It will easily integrate with the CI-CD process.
- Users can easily roll back to the previous configuration. Because, all the configurations in terraform are version controlled and the state is managed.
- Since no human intervention is needed other than building code the possibilities of occurring error is very negligible.
- Terraform uses simple commands which are easy to understand
- Ex: terraform init terraform refresh terraform plan terraform apply