top of page

Understanding Terraform: From Basics to Advanced for DevOps Roles

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: Basic to Advance
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:


  1. Automation: Set up resources automatically.

  2. Consistency: Reduce human errors by having the same configurations every time.

  3. Scalability: Easily modify and scale infrastructure by updating code.

  4. Collaboration: Code can be shared and versioned, just like software.



Why Use Terraform?


  1. Cloud Agnostic: Works with many cloud providers like Azure, AWS, Google Cloud, etc.

  2. Declarative Language: You declare the desired state of infrastructure, and Terraform makes it happen.

  3. State Management: Keeps track of resources in a "state file," so you always know the current status of your infrastructure.

  4. 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


  1. Install Terraform and Initialize the Project

    • Create a folder for your project.

    • Inside the folder, create a file called main.tf.


  2. Write the Configuration Add the following code to main.tf:

    hcl

# 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"
  }
}

  1. Initialize Terraform Open a terminal in your project folder and run:

    bash

terraform init
  1. Preview the Plan Check what Terraform will do:

    bash

terraform plan
  1. Apply the Configuration Create the infrastructure:

    bash

terraform apply
  1. Verify in Azure Log in to your Azure account and check for the newly created resources.

  2. Clean Up If you want to delete everything created by Terraform:

    bash

terraform destroy

Going Advanced with Terraform

Once you’re comfortable with the basics, you can explore:


  1. Remote State: Store the state file in a remote location like Azure Blob Storage for collaboration.

  2. Workspaces: Use different environments (e.g., dev, test, prod) in the same configuration.

  3. Modules: Create reusable templates for complex setups.

  4. 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


bottom of page