Terraform
Basics & Fundamentals
- Infrastructure as Code (IaC)
- Declarative Syntax in IaC
- Terraform Configuration Files
- Terraform CLI
- Terraform Init
- Terraform Plan
- Terraform Apply
- Terraform Destroy
Providers & Resources
๐ 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:
-
Provider Block
- Defines which cloud youโre working with.
provider "aws" {region = "us-east-1"} -
Resource Block
- Defines what resource to create.
resource "aws_instance" "my_vm" {ami = "ami-0c55b159cbfafe1f0"instance_type = "t2.micro"} -
Variables Block
- Reusable inputs for flexibility.
variable "instance_type" {default = "t2.micro"} -
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?
-
Infrastructure Consistency
- Same
.tf
files can create identical environments across dev, test, and prod.
- Same
-
Automation at Scale
- Instead of manual clicks, use
.tf
to automate hundreds of resources.
- Instead of manual clicks, use
-
Cross-Cloud Flexibility
- Write once, use with AWS, Azure, GCP, Kubernetes.
-
Version Control with Git
- Store
.tf
files in repositories for collaboration.
- Store
-
Disaster Recovery
- Recreate entire infrastructure from
.tf
files.
- Recreate entire infrastructure from
-
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 (usemain.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
beforeterraform 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.