Understanding Terraform: From Basics to Advanced for DevOps Roles
- WeeklyTechReview

- Jan 17
- 4 min read
Updated: 5 days ago
Terraform is a powerful open-source tool widely used in DevOps for managing and provisioning infrastructure. It simplifies creating, managing, and updating cloud resources. In this blog, we’ll break down Terraform from the basics to advanced levels in simple English so even someone without an IT background can grasp it.
What is Terraform?

Terraform is an Infrastructure as Code (IaC) tool. This means you can write code to define and manage your infrastructure (like servers, networks, databases) instead of manually creating and configuring resources.
Think of it as a way to create a blueprint for your cloud infrastructure and have it automatically built for you.
What is IaC (Infrastructure as Code)?
Imagine building a house with detailed instructions or blueprints rather than doing everything manually. Similarly, IaC lets you create and manage infrastructure using code. Instead of clicking buttons in the cloud provider’s console (e.g., Azure Portal), you write scripts to define what you need.
Advantages of IaC:
Automation: Set up resources automatically.
Consistency: Reduce human errors by having the same configurations every time.
Scalability: Easily modify and scale infrastructure by updating code.
Collaboration: Code can be shared and versioned, just like software.
Why Use Terraform?
Cloud Agnostic: Works with many cloud providers like Azure, AWS, Google Cloud, etc.
Declarative Language: You declare the desired state of infrastructure, and Terraform makes it happen.
State Management: Keeps track of resources in a "state file," so you always know the current status of your infrastructure.
Reusability: Share and reuse configurations through modules.
Terraform Components Explained
To understand Terraform, you need to know its key components:
1. Provider
A provider is a plugin that interacts with a specific cloud (like Azure, AWS) to create resources.
Example: Azure provider is used to manage Azure resources.
2. Configuration Files
Terraform uses .tf files written in HashiCorp Configuration Language (HCL). These files define the infrastructure you want.
Example: A file defining a virtual machine in Azure.
3. State
Terraform keeps a record of your infrastructure’s current state in a file called a "state file" (terraform.tfstate). This ensures Terraform knows what’s already created and prevents duplicate resources.
4. Resources
These are the building blocks of your infrastructure, like a virtual machine, storage, or network.
5. Modules
Modules are reusable pieces of Terraform code. Think of them as templates for common infrastructure setups.
6. Commands
Some important Terraform commands:
terraform init: Prepares Terraform by downloading the necessary provider plugins.
terraform plan: Shows what changes Terraform will make to your infrastructure.
terraform apply: Applies the changes to create or update infrastructure.
terraform destroy: Deletes all resources created by Terraform.
A Basic Terraform Project: Setting Up an Azure Virtual Machine
Let’s walk through a simple project where we use Terraform to create a virtual machine (VM) in Azure.
Prerequisites
An Azure account.
Install Terraform on your system (download from the official website).
Steps to Build the Project
# Specify the Azure provider
provider "azurerm" {
features {}
}
# Define the resource group
resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "East US"
}
# Define a virtual network
resource "azurerm_virtual_network" "example" {
name = "example-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}
# Define a subnet
resource "azurerm_subnet" "example" {
name = "example-subnet"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = ["10.0.1.0/24"]
}
# Define a virtual machine
resource "azurerm_linux_virtual_machine" "example" {
name = "example-vm"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
size = "Standard_DS1_v2"
admin_username = "azureuser"
admin_password = "P@ssword123!"
network_interface_ids = [
azurerm_network_interface.example.id,
]
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
}
# Network Interface
resource "azurerm_network_interface" "example" {
name = "example-nic"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.example.id
private_ip_address_allocation = "Dynamic"
}
}Initialize Terraform Open a terminal in your project folder and run:
bash
terraform initPreview the Plan Check what Terraform will do:
bash
terraform planApply the Configuration Create the infrastructure:
bash
terraform applyVerify in Azure Log in to your Azure account and check for the newly created resources.
Clean Up If you want to delete everything created by Terraform:
bash
terraform destroyGoing Advanced with Terraform
Once you’re comfortable with the basics, you can explore:
Remote State: Store the state file in a remote location like Azure Blob Storage for collaboration.
Workspaces: Use different environments (e.g., dev, test, prod) in the same configuration.
Modules: Create reusable templates for complex setups.
Integrations: Combine Terraform with tools like Jenkins or GitHub Actions for automation.
Conclusion
Terraform is an essential tool for modern DevOps engineers. By learning Terraform, you can save time, improve consistency, and simplify infrastructure management. Start with small projects, like creating a virtual machine, and gradually explore advanced features like modules and remote state.
With Terraform, you’re not just managing infrastructure; you’re embracing a future where everything can be automated and codified.








Comments