Understanding The Fundamental of Terraform.

Introduction

Terraform is an IaC tool that will help us build, change and keep track of the infrastructure in the cloud and on-premise server. Terraform help in creating and managing the resources in the cloud or on-premise with the use of API. Providers enable Terraform to work with virtually any platform or service with an accessible API. The hashicorp has written thousands of providers to manage many different types of resources and services.

Why terraform?

Terraform offers extensive support for various cloud providers, allowing organizations to manage infrastructure across different environments seamlessly, enabling multi-cloud or hybrid strategies.

HCL supports a declarative approach to infrastructure provisioning. This is also the reason to learn and use Terraform as it is easy to understand also.

Terraform Workflow

Terraform workflow consists of three stages:

  1. Write

Here we write a configuration to define the resources for managing the infrastructure among various cloud platforms.

  1. Plan

Terraform will create an execution plan describing the infrastructure it will create, update, or destroy based on the configuration we wrote.

  1. Apply

Terraform will then apply those plans and perform those actions.

HCL Basics

We will look at the basic structure of the Hashi Corp Language.

Here in the above HCL the "resource" is the Block Name, "local_file" is the Resource Type, and "pet" is the resource name.

The filename and content in the block are called an argument.

After that, we will do terraform init which will initialize the terraform backend, download the required provider's plugins, and resolve and download any necessary module dependencies.

Terraform supports many Providers and each provider has its own resource type and in that resource type, they have their own argument. We do not need to memorize it we can simply check the documentation provided by the hashi corp.

Different Configuration File

Apart from main.tf we can create many other files which have their own purpose for making them more manageable and readable.

Each file has its own purpose as stated above in Fig.

Variables

variables.tf, is where we define the input variables for your Terraform configuration. In this file, we define the variables, their types, default values, and any constraints or validation rules associated with them.

There are many types of variables in terraforming:

String Variables, Number Variables, Boolean Variables

List Variable:

List variables allow us to define an ordered collection of values. Lists are enclosed in square brackets [] and can contain elements of any type, including strings, numbers, or even other lists.

Map Variables:

Map variables enable us to define a collection of key-value pairs. Maps are enclosed in curly braces {}, and the keys and values can be of any type.

We can use and access variables by using var.variable_name keyword.

Attribute Reference

We can also use the reference as an attribute in the resources block. We can look at which attribute it is returning and use it according to using the command $(resource_type.reource_name.attribute).

Outputs

Output variables allow us to define values that are extracted from your infrastructure configuration.

output "<name>" {
  value = <value>
}

Terraform StateFile

The Terraform state file is a crucial component of a Terraform project. It is a record of the resources managed by Terraform and their current state. The state file is created and updated each time we run a Terraform command that modifies the infrastructure.

Terraform state file is not uploaded in the public repository as it is the single source of truth for the terraform so we store the terraform state file in a secure location like AWS S3 Bucket, terraform own cloud, etc.

It tracks the resource configuration, its relationships, and the current attribute values. Terraform state can be shared among team members to facilitate collaboration and enable consistent infrastructure management.

The state file includes a locking mechanism to prevent modification in the state file by many people at the same time which may corrupt the state file which is not good for the company. Performing the locking mechanism it ensures no concurrent modification happens.

Lifecycle Rules

Lifecycle rules are used to define certain behaviors and actions related to resource management during different stages of the resource's lifecycle.

Data Sources

Data sources allow Terraform to use information defined outside of Terraform, defined by another separate Terraform configuration, or modified by functions.

data "<data_source_type>" "<data_source_name>" {
  <configuration_parameters>
}

we can access it using data.data_source_type.data_source_name.attribute .

Data sources are used to fetch information from external systems, APIs, or infrastructure platforms.

Modules

Modules are a key component that enables us to reuse configurations, promoting code reusability.

A module in Terraform is a collection of resources, variables, and other Terraform constructs that are grouped together to represent a specific infrastructure component or a set of related resources.

Conclusion

In conclusion, Terraform is an amazing IaC tool for managing infrastructure using code. It simplifies the deployment and configuration of resources across different cloud platforms. We learn about the fundamental of terraform and now in the coming days we will practice terraform with aws. Check out my 30dyas AWS series in here.