๐ŸŒฉ๏ธ Mastering Terraform: 30+ Essential Commands and Azure Interview Questions (2025 Guide)

Harshal SonarHarshal Sonar
6 min read

Whether you're a DevOps fresher or an experienced engineer preparing for Azure cloud interviews, this blog is your one-stop guide for mastering Terraform CLI commands with real Azure examples and frequently asked interview questions.

๐Ÿš€ What is Terraform?

Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It enables you to safely and predictably create, change, and improve infrastructure using simple configuration files. Terraform works across cloud providers like Azure, AWS, GCP, and more.

๐Ÿ”ง Terraform Commands You Must Know (With Azure Examples)

Hereโ€™s a list of the most used Terraform commands, what they do, and how to use them with Azure:

CommandDescriptionAzure Example
terraform initInitializes your Terraform project and downloads the Azure providerterraform init
terraform planShows what changes Terraform will make before actually applyingterraform plan
terraform applyApplies the configuration and provisions Azure infrastructureterraform apply
terraform destroyDestroys all Azure resources managed by Terraformterraform destroy
terraform validateValidates configuration for syntax or logic errorsterraform validate
terraform fmtAutomatically formats .tf filesterraform fmt
terraform outputDisplays output variables (like Azure VM IP)terraform output
terraform showShows the Terraform state or plan in readable formterraform show
terraform graphGenerates a visual graph of your infrastructure dependenciesterraform graph
terraform versionShows your current Terraform versionterraform version
terraform loginLogs into Terraform Cloud or Enterpriseterraform login
terraform providersLists all providers used in your config (like azurerm)terraform providers

โš™๏ธ Advanced Terraform Commands

CommandUse Case
terraform importImport existing Azure resources into Terraform state
terraform taintMark Azure resource for recreation
terraform untaintRemove tainted status
terraform state listList all Azure resources in Terraform state
terraform state rmRemove a resource from the state file (without destroying it)
terraform state mvMove a resource from one module or name to another
terraform workspace newCreate a new workspace (e.g., dev, staging)
terraform workspace selectSwitch to an existing workspace
terraform workspace listList all workspaces
terraform getDownload/update modules
terraform plan -out=tfplanSave plan for approval or automation
terraform apply tfplanApply the pre-approved plan
TF_LOG=DEBUG terraform applyEnable verbose debug logging

โ˜๏ธ Example: Creating Azure Resource Group with Terraform

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "rg-terraform-demo"
  location = "East US"
}

Commands to run:

terraform init
terraform plan
terraform apply

๐ŸŽฏ Terraform Interview Questions with Azure Context

๐Ÿ”ฐ For Freshers

  1. What does terraform init do?
    Initializes the working directory and downloads the Azure provider.

  2. How to validate Terraform config?
    Run terraform validate.

  3. What does terraform apply do?
    Provisions resources defined in .tf files on Azure.

  4. Which command destroys Azure infrastructure?
    terraform destroy

  5. How to display output like VM IP?
    terraform output

  6. How do you format code to fix indentation errors?
    terraform fmt

  7. How to show current state in readable form?
    terraform show

  8. What is the use of terraform version?
    It shows installed Terraform version.

  9. Does Terraform support parameters like other scripting languages?
    โœ… Yes! Terraform uses input variables to parameterize code.

    Example:

     variable "location" {
       description = "Azure region"
       default     = "East US"
     }
    
     resource "azurerm_resource_group" "example" {
       name     = "rg-terraform-demo"
       location = var.location
     }
    

    Ways to pass values:

    • CLI: terraform apply -var="location=West Europe"

    • terraform.tfvars:

        location = "Central India"
      

๐Ÿ‘จโ€๐Ÿ’ป For Experienced Professionals

  1. How to import existing Azure resource group into Terraform?

    terraform import azurerm_resource_group.example /subscriptions/<sub-id>/resourceGroups/<rg-name>
    
  2. How do workspaces help in managing environments in Azure?
    Separate state files for dev, test, prod:

    terraform workspace new dev
    terraform workspace select dev
    
  3. How to mark an Azure VM to be recreated on next apply?
    terraform taint azurerm_linux_virtual_machine.myvm

  4. Whatโ€™s the use of terraform state rm?
    Removes a resource from Terraform state (without deleting it in Azure).

  5. How do you handle multiple Azure subscriptions in a single config?
    Use provider alias:

    provider "azurerm" {
      alias           = "prod"
      subscription_id = "<sub-id>"
      features        = {}
    }
    
  6. How to securely provide credentials to Terraform for Azure?
    Set env variables like:

    export ARM_CLIENT_ID=""
    export ARM_CLIENT_SECRET=""
    export ARM_SUBSCRIPTION_ID=""
    export ARM_TENANT_ID=""
    
  7. What is remote state and how to store it in Azure?
    Use Azure Blob Storage:

    backend "azurerm" {
      resource_group_name  = "rg-tfstate"
      storage_account_name = "tfstorage123"
      container_name       = "tfstate"
      key                  = "terraform.tfstate"
    }
    

๐Ÿ”น Terraform Interview Questions (Updated)


๐ŸŸ  Q: What is a Terraform Module?

A:
A Terraform module is a group of Terraform configuration files used together. Modules help you organize and reuse code across different environments like dev, staging, and production.

There are 3 types of modules:

  • Root module โ€“ The main .tf files in your working directory.

  • Child module โ€“ A reusable module called by the root or another module.

  • Remote module โ€“ Modules pulled from the Terraform Registry or GitHub.

โœ… Example:

module "network" {
  source     = "./modules/network"
  vnet_name  = "my-vnet"
  subnet_ids = ["subnet1", "subnet2"]
}

๐Ÿ’ก Benefits of using modules:

  • Code reuse

  • Better organization

  • Easier team collaboration

  • Scalability for complex infrastructure


๐Ÿ”ด Q: What happens if the Terraform backend state file is deleted? How do you recover it?

A:
If the Terraform state file (terraform.tfstate) is deleted, Terraform loses track of the real infrastructure. This can lead to serious issues like:

  • Terraform thinks nothing exists and may try to recreate resources.

  • Plans become unreliable.

  • You lose visibility into dependencies and changes.

๐Ÿ›  How to Fix It:

  1. Restore from Backup (Recommended):

    • If you're using a remote backend like AWS S3, Azure Blob, or Terraform Cloud, these usually have versioning enabled.

    • Simply roll back to the last working version of the .tfstate file.

  2. Manual State Rebuild (Advanced):

    • Use terraform import to re-associate existing infrastructure with a new state file.
    terraform import azurerm_resource_group.example /subscriptions/xxx/resourceGroups/your-rg
  • This is tedious and prone to errors โ€” use only if backups are unavailable.

โœ… Best Practice:

Always use remote backends with versioning and state locking enabled to prevent accidental state loss.

๐Ÿง  Final Tips for Readers

1
Subscribe to my newsletter

Read articles from Harshal Sonar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harshal Sonar
Harshal Sonar