Terraform
Tags: devops, terraform, iac
Published: 2024-05-27 11:27:32

Utilizing reusable, shareable, human-readable configuration files, HashiCorp Terraform is an infrastructure as code (IaC) software solution that enables DevOps teams to automate infrastructure provisioning. Infrastructure provisioning may be automated using this technology in both on-premises and cloud scenarios.

### Infrastructure as Code
The process of supplying and controlling IT infrastructure using coding is known as "infrastructure as code" IaC enables DevOps teams to programmatically and automatically manage, monitor, and provide the resources they require, as opposed to manual infrastructure management, when each necessary resource is manually set by a human.

Teams may use Terraform to describe and provision all of the infrastructure's parts using code. Config files, which are readily shared, reused, and versioned, contain the code. In order to manage the whole cloud or data center infrastructure and its resources over the course of their lifespan, the files aid in the creation of a standardized workflow.

Declarative configuration files for Terraform define the final state of the infrastructure. Instead of having to give detailed instructions, which is a laborious and time-consuming procedure, to construct the necessary infrastructure resources, the tool handles the underlying 
logic itself.

It is simple for DevOps teams to accomplish the following since the files codify the application programming interfaces (APIs) for cloud platforms and other services:

![IaC Process](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q8knwzdgw4mefmkutcxw.png)

- Any cloud provider may be used to provision resources.

- Put up compliance and security barriers to harmonize the infrastructure.

- Use defined and dependable procedures to ensure consistency in the provisioning, sharing, and reuse of infrastructure.

- Integrate VCS, ITSM, and CI/CD with the self-service infrastructure. Terraform is capable of managing low-level components like DNS records as well as highlevel infrastructure elements like computation, storage, and networking resources. 

Additionally, it may be used to automatically setup servers, databases, and firewall settings. Teams may manage infrastructure using their favorite programming language, including TypeScript, Python, Go, C#, and Java, with the use of a Cloud Development Kit for Terraform (CDKTF).

### How Terraform works
The ability to construct declarative configuration files using Terraform is made possible by the widely used APIs that are accessible from all major cloud service providers. The Terraform Registry has a list of these suppliers.

![Terraform Providers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/euewbr67sv4kv8o438f3.png)

Teams may utilize the modules, policy libraries, and tasks included in the Registry to easily install standard infrastructure setups and maintain them automatically with code. The process for Terraform consists of three steps:

1. **Write**
A user defines the necessary resources in configuration files at this step. These resources might be spread out throughout several on-premises or cloud settings, as well as between various suppliers and services. 

2. **Plan**
This step starts once the user examines and approves the necessary phases. The steps that will be taken to develop or upgrade the infrastructure are described in the execution plan that Terraform produces in this case.

3. **Apply**
Before Terraform makes modifications to the infrastructure, the plan must be approved by the user. After receiving permission, Terraform executes the suggested procedures in the specified sequence. Before making changes, it will always consider resource dependencies. 
For example, in the event that a user decides to increase the number of virtual machines in a VPC (virtual private cloud), Terraform will first rebuild the VPC before scaling up the VMs

![Terraform Process](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vewk4rpvssubnehftzpe.png)

### Use Cases
- IaC is Terraform's most popular use case. Terraform infrastructure deployments are simple to integrate with current CI/CD procedures. 

- Teams may use Terraform, for instance, to automatically update member pools for load balancing and other crucial networking activities.

- For provisioning across many clouds, Terraform is also helpful. Development teams may use Terraform to provide load balancers in Google Cloud, manage Active Directory (AD) resources in Microsoft Azure, and deploy serverless operations in AWS. 

- Manage Kubernetes clusters in any public cloud (AWS, Azure, Google).

- Enforce policy-as-code before infrastructure components are developed and deployed.

- Use secrets and credentials in Terraform setups automatically.

- Import current infrastructure into a blank Terraform workspace to codify it.

- Transfer state to Terraform to protect it and make it simple for authorized collaborators to access it.