DEV Community

Harshal Sonar
Harshal Sonar

Posted on

🌩️ Mastering Terraform: 30+ Essential Commands and Azure Interview Questions (2025 Guide)

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:

Command Description Azure Example
terraform init Initializes your Terraform project and downloads the Azure provider terraform init
terraform plan Shows what changes Terraform will make before actually applying terraform plan
terraform apply Applies the configuration and provisions Azure infrastructure terraform apply
terraform destroy Destroys all Azure resources managed by Terraform terraform destroy
terraform validate Validates configuration for syntax or logic errors terraform validate
terraform fmt Automatically formats .tf files terraform fmt
terraform output Displays output variables (like Azure VM IP) terraform output
terraform show Shows the Terraform state or plan in readable form terraform show
terraform graph Generates a visual graph of your infrastructure dependencies terraform graph
terraform version Shows your current Terraform version terraform version
terraform login Logs into Terraform Cloud or Enterprise terraform login
terraform providers Lists all providers used in your config (like azurerm) terraform providers
βš™οΈ Advanced Terraform Commands
Command Use Case
terraform import Import existing Azure resources into Terraform state
terraform taint Mark Azure resource for recreation
terraform untaint Remove tainted status
terraform state list List all Azure resources in Terraform state
terraform state rm Remove a resource from the state file (without destroying it)
terraform state mv Move a resource from one module or name to another
terraform workspace new Create a new workspace (e.g., dev, staging)
terraform workspace select Switch to an existing workspace
terraform workspace list List all workspaces
terraform get Download/update modules
terraform plan -out=tfplan Save plan for approval or automation
terraform apply tfplan Apply the pre-approved plan
TF_LOG=DEBUG terraform apply Enable verbose debug logging
☁️ Example: Creating Azure Resource Group with Terraform

Copy

Copy
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "example" {
name = "rg-terraform-demo"
location = "East US"
}
Commands to run:

Copy

Copy
terraform init
terraform plan
terraform apply
🎯 Terraform Interview Questions with Azure Context
πŸ”° For Freshers
What does terraform init do?
Initializes the working directory and downloads the Azure provider.

How to validate Terraform config?
Run terraform validate.

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

Which command destroys Azure infrastructure?
terraform destroy

How to display output like VM IP?
terraform output

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

How to show current state in readable form?
terraform show

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

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

Example:

Copy

Copy
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:

Copy

Copy
location = "Central India"
πŸ‘¨β€πŸ’» For Experienced Professionals
How to import existing Azure resource group into Terraform?

Copy

Copy
terraform import azurerm_resource_group.example /subscriptions//resourceGroups/
How do workspaces help in managing environments in Azure?
Separate state files for dev, test, prod:

Copy

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

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

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

Copy

Copy
provider "azurerm" {
alias = "prod"
subscription_id = ""
features = {}
}
How to securely provide credentials to Terraform for Azure?
Set env variables like:

Copy

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

Copy

Copy
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:

Copy

Copy
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:

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.

Manual State Rebuild (Advanced):

Use terraform import to re-associate existing infrastructure with a new state file.

Copy

Copy
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

Practice Terraform with Azure free tier.

Use terraform plan before every apply.

Store secrets securely using Key Vault or environment variables.

Automate provisioning with GitHub Actions or Azure DevOps.

Bookmark https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs

Add Canonical Link (To Avoid Duplicate SEO Issues)
https://harshalsonar.hashnode.dev/mastering-terraform-30-essential-commands-and-azure-interview-questions-2025-guide

Top comments (0)