๐Ÿ“˜ Terraform Configuration Files (.tf): The Building Blocks of Infrastructure as Code

If Infrastructure as Code (IaC) were a language, then Terraform Configuration Files (.tf) would be its dictionary.

Terraform, created by HashiCorp, uses a special file format called .tf files, written in HCL (HashiCorp Configuration Language). These files define what your infrastructure should look like in a simple, human-readable, and declarative way.

Instead of manually creating servers, networks, or storage, you just write Terraform code in .tf files, and Terraform takes care of the heavy lifting.

This article will cover:

  • โœ… What Terraform Configuration Files are.
  • โœ… Why HCL (HashiCorp Configuration Language) is important.
  • โœ… Structure of .tf files.
  • โœ… 3 unique real-world examples.
  • โœ… Tips to remember this concept for exams/interviews.
  • โœ… Why learning .tf files is crucial for DevOps and cloud careers.

๐Ÿ“‚ What are Terraform Configuration Files (.tf)?

Terraform configuration files are plain-text files written in HCL (HashiCorp Configuration Language) or JSON. They:

  • Define resources (like AWS EC2, Azure VM, GCP network).
  • Manage providers (cloud platforms such as AWS, Azure, GCP, DigitalOcean).
  • Describe variables, outputs, and modules.
  • Act as the blueprint for your cloud infrastructure.

By default, these files use the .tf extension.

Example filenames:

  • main.tf โ†’ Main configuration file.
  • variables.tf โ†’ Stores input variables.
  • outputs.tf โ†’ Stores outputs for referencing.
  • provider.tf โ†’ Configures cloud providers.

โš™๏ธ Why HCL (HashiCorp Configuration Language)?

HCL is designed specifically for infrastructure:

  • Human-readable: Easier to learn compared to JSON or YAML.
  • Declarative: You define what you want, Terraform figures out how.
  • Flexible: Works across multiple cloud providers.
  • Extensible: Supports modules, loops, conditionals, and variables.

๐Ÿ‘‰ Think of HCL as a mix between JSON (structured) and a human-friendly scripting language.


๐Ÿ“ Structure of a Terraform Configuration File

A .tf file usually contains:

  1. Provider Block

    • Defines which cloud youโ€™re working with.
    provider "aws" {
    region = "us-east-1"
    }
  2. Resource Block

    • Defines what resource to create.
    resource "aws_instance" "my_vm" {
    ami = "ami-0c55b159cbfafe1f0"
    instance_type = "t2.micro"
    }
  3. Variables Block

    • Reusable inputs for flexibility.
    variable "instance_type" {
    default = "t2.micro"
    }
  4. Outputs Block

    • Display useful information after deployment.
    output "public_ip" {
    value = aws_instance.my_vm.public_ip
    }

๐Ÿ›  3 Unique Examples of Terraform Configuration Files

Letโ€™s go beyond the basics with 3 real-world examples.


โœ… Example 1: Deploy an AWS EC2 Instance with Elastic IP

provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "my_vm" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "Terraform-EC2"
}
}
resource "aws_eip" "my_eip" {
instance = aws_instance.my_vm.id
}

๐Ÿ‘‰ What it does:

  • Creates an EC2 instance.
  • Attaches an Elastic IP so the server always has the same public IP.

โœ… Example 2: Deploy an Azure Virtual Network and VM

provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "rg" {
name = "terraform-rg"
location = "East US"
}
resource "azurerm_virtual_network" "vnet" {
name = "terraform-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}
resource "azurerm_linux_virtual_machine" "vm" {
name = "terraform-vm"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
size = "Standard_B1s"
admin_username = "adminuser"
network_interface_ids = []
admin_password = "P@ssw0rd1234!"
}

๐Ÿ‘‰ What it does:

  • Creates a resource group.
  • Creates a virtual network (VNet).
  • Deploys a Linux VM inside Azure.

โœ… Example 3: Deploy a Kubernetes Nginx Deployment

provider "kubernetes" {
config_path = "~/.kube/config"
}
resource "kubernetes_deployment" "nginx" {
metadata {
name = "nginx-deployment"
labels = {
app = "nginx"
}
}
spec {
replicas = 2
selector {
match_labels = {
app = "nginx"
}
}
template {
metadata {
labels = {
app = "nginx"
}
}
spec {
container {
image = "nginx:latest"
name = "nginx"
port {
container_port = 80
}
}
}
}
}
}

๐Ÿ‘‰ What it does:

  • Connects to a Kubernetes cluster.
  • Creates a deployment with 2 replicas of Nginx pods.

๐ŸŽฏ Why Are Terraform Configuration Files Important?

  1. Infrastructure Consistency

    • Same .tf files can create identical environments across dev, test, and prod.
  2. Automation at Scale

    • Instead of manual clicks, use .tf to automate hundreds of resources.
  3. Cross-Cloud Flexibility

    • Write once, use with AWS, Azure, GCP, Kubernetes.
  4. Version Control with Git

    • Store .tf files in repositories for collaboration.
  5. Disaster Recovery

    • Recreate entire infrastructure from .tf files.
  6. Compliance & Security

    • Ensure policies and infrastructure are consistent with IaC.

๐Ÿง  How to Remember Terraform Configuration Files (Exam & Interview Tips)

Hereโ€™s a memory technique (mnemonic): T.F.I.L.E.S

  • T โ€“ Terraformโ€™s blueprint of infrastructure
  • F โ€“ File extension is .tf
  • I โ€“ Infrastructure defined in HCL
  • L โ€“ Logical blocks: provider, resource, variables, outputs
  • E โ€“ Easy to read and version control
  • S โ€“ Scalable across multiple clouds

๐Ÿ‘‰ Interview Example Answer: โ€œTerraform configuration files, usually with a .tf extension, are written in HCL and define the desired infrastructure. For example, a .tf file can describe an EC2 instance in AWS, a VNet in Azure, or even a Kubernetes deployment. These files make infrastructure reproducible, version-controlled, and scalable.โ€


๐Ÿ“š Best Practices for .tf Files

  • Keep .tf files modular (use main.tf, variables.tf, outputs.tf).
  • Store .tfstate securely (prefer remote state in S3, Azure Blob, or GCS).
  • Use Terraform modules for reusable infrastructure code.
  • Always run terraform plan before terraform apply.
  • Follow naming conventions and use comments for clarity.

๐Ÿ”ฎ Future of Terraform Configuration Files

  • AI-Assisted IaC โ€“ Tools like ChatGPT can auto-generate .tf files.
  • GitOps Integration โ€“ .tf + Git for full automation pipelines.
  • Policy as Code โ€“ Adding compliance/security rules inside .tf.
  • Multi-Cloud Expansion โ€“ Single .tf defining infra across multiple providers.

๐Ÿ“ Summary

  • Terraform Configuration Files (.tf) are the heart of Terraform, written in HCL.
  • They define infrastructure in a declarative, human-readable way.
  • Structure includes provider, resource, variable, and output blocks.
  • Examples include AWS EC2, Azure VM + VNet, and Kubernetes Deployment.
  • Easy to remember using T.F.I.L.E.S mnemonic.
  • Importance: automation, scalability, consistency, and cross-cloud flexibility.

โœ… Mastering .tf files is the first step to becoming a Terraform expert. Whether youโ€™re preparing for interviews, certifications, or real-world DevOps work, understanding .tf configuration files is non-negotiable.