What is Terraform?

Features, Advantages, and Key Terminologies Explained

Introduction

In the history of cloud computing and infrastructure as code, Terraform has emerged as a popular tool for provisioning, managing, and automating infrastructure. Terraform, developed by HashiCorp, enables organizations to define infrastructure in a declarative configuration language and manage it across various service providers.

In this blog, we will cover:

  • What is Terraform?
  • Key Features and Advantages of Terraform
  • Essential Terraform Terminologies

What is Terraform?

Terraform is an open-source infrastructure-as-code tool developed by HashiCorp. It allows users to define and provision infrastructure using a high-level configuration language known as HashiCorp Configuration Language (HCL), or JSON. Terraform enables infrastructure to be treated as code, making it possible to automate the deployment and management of both cloud and on-premises resources in a consistent manner.

Unlike traditional approaches, where manual configuration is needed, Terraform empowers DevOps teams to define resources such as virtual machines, storage, networks, and more in declarative code. This eliminates the risk of configuration drift and ensures that infrastructure is always in a consistent, predictable state. Terraform is widely used for managing infrastructure on major cloud platforms like AWS, Azure, GCP, and even on-premises solutions like VMware.

Features and Advantages of Terraform

  1. Infrastructure as Code (IaC)

Terraform lets you describe your infrastructure using configuration files, making it easy to automate infrastructure management. You can version control your infrastructure, much like you would application code, allowing for clear documentation and collaboration among teams.

  1. Multi-Cloud Support

One of Terraform’s standout features is its ability to manage infrastructure across multiple cloud providers. You can use Terraform to deploy resources on AWS, Azure, Google Cloud, and even other platforms like Oracle Cloud and OpenStack, all within the same workflow. This multi-cloud flexibility helps companies avoid vendor lock-in and enables hybrid-cloud strategies.

  1. State Management

Terraform keeps track of your infrastructure’s state in a file known as the “state file”. This state file enables Terraform to know which resources have been created, updated, or deleted, ensuring that future modifications to your infrastructure are done in a controlled and predictable way. This feature minimizes human errors and simplifies resource tracking.

  1. Execution Plans

Before making changes to your infrastructure, Terraform generates an execution plan that shows you exactly what it will do. This “plan” step enables you to review changes before they are applied, reducing the risk of mistakes.

  1. Resource Graphs

Terraform builds a resource dependency graph internally, meaning it knows the relationships between resources. This allows Terraform to provision resources in parallel whenever possible, reducing the time it takes to deploy complex infrastructure.

  1. Modules

Terraform modules allow you to group resources and reuse them across different parts of your infrastructure. For example, you can create a module for a virtual network and reuse that module whenever a new network is needed. Modules make your code more DRY (Don’t Repeat Yourself), maintainable, and scalable.

  1. Extensible with Plugins

Terraform uses providers, which are plugins that interface with different platforms, services, and APIs. HashiCorp maintains providers for major cloud services, but the community also contributes plugins for smaller, more niche platforms.

  1. Immutable Infrastructure

Terraform advocates for immutable infrastructure, meaning infrastructure changes should result in the recreation of resources rather than in-place modifications. This helps ensure consistency and reduces drift over time.

Different Terraform Terminologies

Understanding the core terminologies in Terraform is essential for grasping its functionality. Below are some of the most commonly used terms. We have used AWS Cloud as an example to explain these terms.

1.Provider

A provider in Terraform is a plugin that allows you to manage and interact with APIs of external platforms (such as AWS, Azure, or Google Cloud). Providers are responsible for the lifecycle of resources within Terraform. Each provider requires configuration before use, typically specifying authentication credentials and the region or project it should operate in.

  • provider “aws”: This line specifies that the provider is AWS. Terraform uses providers to interact with cloud platforms and other services. Here, we’re telling Terraform to use AWS as the provider.
  • region = “us-west-2”: Within the provider configuration, we set the region attribute to us-west-2. This is the AWS region where Terraform will create and manage resources. In this case, us-west-2 refers to the AWS region in Oregon, USA.

2.Resource

A resource represents the components of your infrastructure, such as servers, databases, or DNS records. Each resource type is defined within a provider and is configured using attributes.

  • resource “aws_instance” “web”: This line declares a resource block, specifying an AWS EC2 instance. aws_instance is the resource type (specific to AWS), and “web” is the name given to this resource within Terraform. The name “web” is an identifier for this instance that can be referenced elsewhere in your Terraform configuration.
  • ami = “ami-123456”: This sets the Amazon Machine Image (AMI) ID for the EC2 instance. The AMI ID represents a specific operating system image, which determines what software and configurations the EC2 instance will start with. The “ami-123456” value here is a placeholder; in a real-world scenario, you would replace it with a valid AMI ID for the desired OS (e.g., Ubuntu, Amazon Linux).
  • instance_type = “t2.micro”: This specifies the instance type, which determines the hardware configuration for the instance (e.g., CPU, memory). t2.micro is a low-cost instance type often used for testing or development due to its minimal resources.

3.Module

A module is a container for multiple resources that are used together. Modules can be called multiple times, with different inputs, to deploy multiple resources of the same type.

  • module “network”: This line defines a module block with the name network. Modules in Terraform help you organize infrastructure by grouping resources into reusable components. Here, network is the name given to this module, which can be referenced elsewhere in your configuration.
  • source = “./modules/network”: This specifies the location of the module’s source files. ./modules/network indicates a relative path to a directory where the network module’s code is stored. The module may define resources like VPCs, subnets, or other networking-related infrastructure components.
  • cidr_block = “10.0.0.0/16”: This is a variable passed into the module. The variable cidr_block typically defines the CIDR (Classless Inter-Domain Routing) block for a network. In this case, 10.0.0.0/16 represents a private IP address range, often used for a VPC (Virtual Private Cloud) in AWS. The module would use this value to configure a network with this IP range.

4. State

The state is how Terraform keeps track of your real infrastructure. It maps your configuration to the real-world resources you’ve created. The state is stored in a file, usually named terraform.tfstate.

The following is a sample tfstate file, displaying only the first few lines. As we can see, it tracks an AWS EC2 instance with the reference name ‘example’.

5. Data Source

A data source allows Terraform to query information from outside sources (like cloud providers) without changing infrastructure. Data sources are useful for importing existing infrastructure data for use in other resources.

  • data “aws_ami” “example” declares a data source of type aws_ami with the name example.
  • most_recent = true: This attribute specifies that Terraform should retrieve the most recent AMI available that matches the given criteria.
  • owners = [“self”]: This attribute specifies that the AMI should be owned by the account currently running this Terraform code (“self” refers to the current AWS account).
  1. Variables

Variables in Terraform allow you to parameterize your configurations so that they can be reused across environments. Input variables can be defined and passed during the execution.

  • Variable block defines a variable named instance_type.
  • The default attribute sets its default value to “t2.micro”, which is a specific instance type in Amazon Web Services (AWS) for virtual machines (EC2 instances). Users can override this default value when they run the Terraform configuration.
  • Resource block defines a resource of type aws_instance with the label web.
  • ami = “ami-123456” specifies the Amazon Machine Image (AMI) ID to be used for the instance. This ID refers to a specific virtual machine image in AWS (you would replace “ami-123456” with a real AMI ID).
  • instance_type = var.instance_type sets the instance_type attribute of this instance to the value of var.instance_type, which points to the variable defined above. By default, this would be “t2.micro”, unless another value is provided.
  1. Outputs

Outputs are values that are returned after Terraform runs. They are helpful for passing data between different parts of your configuration or simply for displaying important information.

  • output “instance_ip” defines an output variable named instance_ip.
  • value = aws_instance.web.public_ip specifies the value that should be displayed or exported. Here, aws_instance.web.public_ip refers to the public_ip of an AWS EC2 instance resource named web.

After applying this configuration, Terraform will display the public IP address of the web instance.

Conclusion

Terraform is a powerful tool that brings the principles of software engineering to infrastructure management, making it more scalable, repeatable, and easier to maintain. With its support for multi-cloud environments, extensibility, and declarative approach, Terraform has become an essential tool in the DevOps ecosystem. By understanding its features and key terminologies, you can leverage Terraform to build and manage cloud and on-premises infrastructure efficiently.

By – Aman Kumar

Leave a Comment

Your email address will not be published.