<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1639769139832878&amp;ev=PageView&amp;noscript=1">

Introduction to Terraform

Nitin Kumar Kashyap, DevOps Engineer, Calance | 14 Dec 2023

In this blog we will learn about Terraform - why we need terraform, file structure and its basic terminologies. 

Introduction   

Terraform is an open-source infrastructure as code (IaC) tool that allows you to manage and provision cloud infrastructure in a declarative manner. Terraform is an incredibly powerful tool that enables you to create and manage infrastructure on any cloud platform, including Amazon Web Services (AWS), Microsoft Azure, Google Cloud Platform (GCP), and many others.  

In this blog, we will cover why we need terraform, some of the basic terminologies that you should know before diving into Terraform. We will also cover file structure of Terraform.  

 

Why we need Terraform  

As more organizations adopt cloud infrastructure, it becomes increasingly important to manage and provision infrastructure in a scalable, automated way. This is where terraform comes in, as it allows organizations to manage infrastructure as code, which has following benefits: 

 

Consistency  

Terraform allows you to define your infrastructure in a declarative manner, which means that your infrastructure configuration is more predictable and consistent. This makes it easier to manage and maintain your infrastructure over time.  

 

 Scalability  

As your infrastructure grows, managing it manually becomes increasingly difficult and time-consuming. Terraform allows you to define and manage your infrastructure in a programmatic way, which makes it easier to scale your infrastructure as required.  

 

 Collaboration  

By defining infrastructure as code, it becomes easier for teams to collaborate and work together. Terraform configuration files can be version controlled and shared among team members, which makes it easier to manage infrastructure changes and maintain consistency across environments. 

 

Flexibility  

Terraform supports a wide range of cloud providers, including AWS, Azure, and GCP, which gives organizations flexibility in choosing the cloud platform that best fits their needs. Terraform also supports on-premises infrastructure, which allows organizations to manage their entire infrastructure in a consistent way.  

 

Automation  

Terraform enables organizations to automate the provisioning and management of infrastructure, which reduces the risk of manual errors and saves time. This automation also makes it easier to enforce security and compliance policies.  

 

Overall, terraform is a powerful tool that allows organizations to manage infrastructure as code, which provides consistency, scalability, collaboration, flexibility, and automation. These benefits make Terraform an essential tool for managing cloud infrastructure in a modern, scalable way.  

 

 File Structure of Terraform  

The file structure of Terraform consists of a set of configuration files that define the desired state of infrastructure. The configuration files are organized into a directory structure that typically follows the recommended Terraform file layout.  

 

Here is an overview of the recommended Terraform file structure:  

Let's look at the purpose of each file and directory:    

 

- main.tf: This is the main Terraform configuration file that defines the infrastructure resources to be managed. It typically includes resource blocks that define the desired state of each resource.  

- variables.tf: This file defines the input variables that are used in the `main.tf` file. Input variables are used to parameterize the Terraform configuration, which makes it more reusable and flexible.  

- outputs.tf: This file defines the output values that are generated by the Terraform configuration. Output values are used to display information about the managed resources, such as their IP addresses or DNS names.  

- terraform.tfvars: This file defines the input variable values that are used in the `main.tf` file. This file is used to set the values of input variables, which can be different for each environment.  

- terraform.tfstate: This file contains the current state of the managed resources. This file is automatically created and updated by Terraform as resources are created, updated, or deleted.  

- README.md: This file provides documentation for the Terraform configuration, including instructions for running and managing the configuration.  

- modules/: This directory contains reusable Terraform modules that define infrastructure components. Modules can be used across multiple Terraform configurations, which makes it easier to standardize infrastructure components across different environments.  

- environments/: This directory contains separate Terraform configurations for different environments, such as development, staging, and production. Each environment has its own set of input variable values and state file.  

- .gitignore: This file specifies files and directories that should be ignored by version control, such as the `terraform.tfstate` and `terraform.tfvars` files.  

 

Overall, this file structure provides a standardized way to organize Terraform configurations, which makes it easier to manage and maintain infrastructure as code over time.  

 

Basic Terminologies of Terraform  

 

Provider  

A provider in Terraform represents a cloud platform or service. Terraform has built-in providers for all major cloud platforms such as AWS, Azure, GCP, and many others. Providers contain the necessary configuration information to authenticate and interact with the cloud service.  

 

Resource   

A resource is a component of infrastructure that Terraform manages. Resources can be anything from virtual machines, databases, and storage buckets to networking components such as virtual networks and subnets. Resources are defined in a Terraform configuration file using a specific syntax that describes the desired state of the resource.  

 

Module  

A module is a self-contained Terraform configuration that can be used to define a specific set of infrastructure resources. Modules can be reused across multiple Terraform configurations, making it easy to standardize infrastructure components across different environments.  

 

 State  

Terraform maintains a state file that contains information about the current state of the managed infrastructure. The state file is used to track changes to infrastructure resources over time and to ensure that the desired state is maintained. The state file can be stored locally or remotely, depending on the configuration.  

 

Plan  

A Terraform plan is a preview of the changes that will be applied to the infrastructure resources. When you run `terraform plan`, Terraform will analyze the current state of the resources and the desired state as defined in the configuration files and generate an execution plan that describes the changes that will be made.   

 

Apply  

The `terraform apply` command is used to apply the changes described in the execution plan to the infrastructure resources. This command should be used with caution, as it will modify the state of the resources and can have unintended consequences if not used correctly.  

 

Conclusion   

At this point, we have gained a clear understanding of what Terraform is and the reasons why it is a valuable tool. We have also delved into the fundamental concepts of Terraform, including its file structure and key terminologies. This foundational knowledge is crucial for proficiently utilizing Terraform in the management and provisioning of infrastructure. 

With Terraform, the process of defining and controlling infrastructure as code becomes more straightforward, allowing for enhanced infrastructure maintenance and scalability in the long run. This empowers you to efficiently manage your infrastructure by defining it in code, ensuring it remains manageable and adaptable as your needs evolve.