๐ฉ๏ธ 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
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
What does
terraform init
do?
Initializes the working directory and downloads the Azure provider.How to validate Terraform config?
Runterraform 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:
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
How to import existing Azure resource group into Terraform?
terraform import azurerm_resource_group.example /subscriptions/<sub-id>/resourceGroups/<rg-name>
How do workspaces help in managing environments in Azure?
Separate state files fordev
,test
,prod
: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:provider "azurerm" { alias = "prod" subscription_id = "<sub-id>" features = {} }
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=""
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:
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.
- Use
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 everyapply
.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
Subscribe to my newsletter
Read articles from Harshal Sonar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
