<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Harshal Sonar</title>
    <description>The latest articles on DEV Community by Harshal Sonar (@harshaldevops).</description>
    <link>https://dev.to/harshaldevops</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3350175%2F886ea8ae-7a63-4ed5-b072-2617843c1fd5.jpg</url>
      <title>DEV Community: Harshal Sonar</title>
      <link>https://dev.to/harshaldevops</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/harshaldevops"/>
    <language>en</language>
    <item>
      <title>Getting Started with Terraform: Deploy Your First Azure Virtual Machine</title>
      <dc:creator>Harshal Sonar</dc:creator>
      <pubDate>Sun, 13 Jul 2025 07:19:09 +0000</pubDate>
      <link>https://dev.to/harshaldevops/getting-started-with-terraform-deploy-your-first-azure-virtual-machine-3lli</link>
      <guid>https://dev.to/harshaldevops/getting-started-with-terraform-deploy-your-first-azure-virtual-machine-3lli</guid>
      <description>&lt;p&gt;Introduction&lt;br&gt;
Terraform is a tool that helps you create and manage cloud resources using simple code. Instead of clicking around in the Azure portal, you write a file that tells Azure what to build.&lt;/p&gt;

&lt;p&gt;In this blog, I will show you how to create your first virtual machine on Microsoft Azure using Terraform — step by step.&lt;/p&gt;

&lt;p&gt;Why Use Terraform?&lt;/p&gt;

&lt;p&gt;Easy to use: Write code to manage infrastructure.&lt;/p&gt;

&lt;p&gt;Repeatable: Use the same code to create the same resources anytime.&lt;/p&gt;

&lt;p&gt;Works with many clouds: Azure, AWS, Google Cloud, and more.&lt;/p&gt;

&lt;p&gt;Track changes: See what will happen before applying changes.&lt;/p&gt;

&lt;p&gt;Step 1: Install Terraform&lt;br&gt;
Go to terraform.io&lt;/p&gt;

&lt;p&gt;Download Terraform for your computer&lt;/p&gt;

&lt;p&gt;Install and open your terminal/command prompt&lt;/p&gt;

&lt;p&gt;Check if Terraform is installed by typing:&lt;br&gt;
Run below command in bash for checking version.&lt;/p&gt;

&lt;p&gt;terraform version&lt;/p&gt;

&lt;p&gt;Step 2: Write Your Terraform File&lt;br&gt;
Create a new folder for your project. Inside that folder, create a file called main.tf.&lt;/p&gt;

&lt;p&gt;Copy and paste this code into main.tf:&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;p&gt;Copy&lt;br&gt;
  hclCopyEditprovider "azurerm" {&lt;br&gt;
    features {}&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;resource "azurerm_resource_group" "mygroup" {&lt;br&gt;
    name     = "myResourceGroup"&lt;br&gt;
    location = "East US"&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;resource "azurerm_virtual_machine" "myvm" {&lt;br&gt;
    name                  = "myVM"&lt;br&gt;
    location              = azurerm_resource_group.mygroup.location&lt;br&gt;
    resource_group_name   = azurerm_resource_group.mygroup.name&lt;br&gt;
    network_interface_ids = []&lt;br&gt;
    vm_size               = "Standard_DS1_v2"&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;storage_image_reference {
  publisher = "Canonical"
  offer     = "UbuntuServer"
  sku       = "18.04-LTS"
  version   = "latest"
}

storage_os_disk {
  name              = "myOsDisk"
  caching           = "ReadWrite"
  create_option     = "FromImage"
  managed_disk_type = "Standard_LRS"
}

os_profile {
  computer_name  = "myVM"
  admin_username = "azureuser"
  admin_password = "ChangeYourPassword123!"
}

os_profile_linux_config {
  disable_password_authentication = false
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
Step 3: Run Terraform Commands&lt;br&gt;
Open your terminal and run:&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;p&gt;Copy&lt;br&gt;
  bashCopyEditterraform init    # Prepare your project&lt;br&gt;&lt;br&gt;
  terraform plan    # See what Terraform will do&lt;br&gt;&lt;br&gt;
  terraform apply   # Create your resources&lt;br&gt;
Step 4: Check Your Azure Portal&lt;br&gt;
After terraform apply finishes, log in to your Azure Portal to see the virtual machine created by Terraform.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
That’s it! You created your first Azure virtual machine using Terraform code. With Terraform, you can manage all your cloud resources easily and keep track of changes using code.&lt;/p&gt;

&lt;p&gt;Add Canonical Link (To Avoid Duplicate SEO Issues)&lt;br&gt;
&lt;a href="https://harshalsonar.hashnode.dev/getting-started-with-terraform-deploy-your-first-azure-virtual-machine" rel="noopener noreferrer"&gt;https://harshalsonar.hashnode.dev/getting-started-with-terraform-deploy-your-first-azure-virtual-machine&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>🌩️ Mastering Terraform: 30+ Essential Commands and Azure Interview Questions (2025 Guide)</title>
      <dc:creator>Harshal Sonar</dc:creator>
      <pubDate>Sun, 13 Jul 2025 07:15:39 +0000</pubDate>
      <link>https://dev.to/harshaldevops/mastering-terraform-30-essential-commands-and-azure-interview-questions-2025-guide-hma</link>
      <guid>https://dev.to/harshaldevops/mastering-terraform-30-essential-commands-and-azure-interview-questions-2025-guide-hma</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;🚀 What is Terraform?&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;🔧 Terraform Commands You Must Know (With Azure Examples)&lt;/p&gt;

&lt;p&gt;Here’s a list of the most used Terraform commands, what they do, and how to use them with Azure:&lt;/p&gt;

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

&lt;p&gt;Copy&lt;/p&gt;

&lt;p&gt;Copy&lt;br&gt;
provider "azurerm" {&lt;br&gt;
  features {}&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;resource "azurerm_resource_group" "example" {&lt;br&gt;
  name     = "rg-terraform-demo"&lt;br&gt;
  location = "East US"&lt;br&gt;
}&lt;br&gt;
Commands to run:&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;p&gt;Copy&lt;br&gt;
terraform init&lt;br&gt;
terraform plan&lt;br&gt;
terraform apply&lt;br&gt;
🎯 Terraform Interview Questions with Azure Context&lt;br&gt;
🔰 For Freshers&lt;br&gt;
What does terraform init do?&lt;br&gt;
Initializes the working directory and downloads the Azure provider.&lt;/p&gt;

&lt;p&gt;How to validate Terraform config?&lt;br&gt;
Run terraform validate.&lt;/p&gt;

&lt;p&gt;What does terraform apply do?&lt;br&gt;
Provisions resources defined in .tf files on Azure.&lt;/p&gt;

&lt;p&gt;Which command destroys Azure infrastructure?&lt;br&gt;
terraform destroy&lt;/p&gt;

&lt;p&gt;How to display output like VM IP?&lt;br&gt;
terraform output&lt;/p&gt;

&lt;p&gt;How do you format code to fix indentation errors?&lt;br&gt;
terraform fmt&lt;/p&gt;

&lt;p&gt;How to show current state in readable form?&lt;br&gt;
terraform show&lt;/p&gt;

&lt;p&gt;What is the use of terraform version?&lt;br&gt;
It shows installed Terraform version.&lt;/p&gt;

&lt;p&gt;Does Terraform support parameters like other scripting languages?&lt;br&gt;
✅ Yes! Terraform uses input variables to parameterize code.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;p&gt;Copy&lt;br&gt;
 variable "location" {&lt;br&gt;
   description = "Azure region"&lt;br&gt;
   default     = "East US"&lt;br&gt;
 }&lt;/p&gt;

&lt;p&gt;resource "azurerm_resource_group" "example" {&lt;br&gt;
   name     = "rg-terraform-demo"&lt;br&gt;
   location = var.location&lt;br&gt;
 }&lt;br&gt;
Ways to pass values:&lt;/p&gt;

&lt;p&gt;CLI: terraform apply -var="location=West Europe"&lt;/p&gt;

&lt;p&gt;terraform.tfvars:&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;p&gt;Copy&lt;br&gt;
  location = "Central India"&lt;br&gt;
👨‍💻 For Experienced Professionals&lt;br&gt;
How to import existing Azure resource group into Terraform?&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

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

&lt;p&gt;Copy&lt;/p&gt;

&lt;p&gt;Copy&lt;br&gt;
terraform workspace new dev&lt;br&gt;
terraform workspace select dev&lt;br&gt;
How to mark an Azure VM to be recreated on next apply?&lt;br&gt;
terraform taint azurerm_linux_virtual_machine.myvm&lt;/p&gt;

&lt;p&gt;What’s the use of terraform state rm?&lt;br&gt;
Removes a resource from Terraform state (without deleting it in Azure).&lt;/p&gt;

&lt;p&gt;How do you handle multiple Azure subscriptions in a single config?&lt;br&gt;
Use provider alias:&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;p&gt;Copy&lt;br&gt;
provider "azurerm" {&lt;br&gt;
  alias           = "prod"&lt;br&gt;
  subscription_id = ""&lt;br&gt;
  features        = {}&lt;br&gt;
}&lt;br&gt;
How to securely provide credentials to Terraform for Azure?&lt;br&gt;
Set env variables like:&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;p&gt;Copy&lt;br&gt;
export ARM_CLIENT_ID=""&lt;br&gt;
export ARM_CLIENT_SECRET=""&lt;br&gt;
export ARM_SUBSCRIPTION_ID=""&lt;br&gt;
export ARM_TENANT_ID=""&lt;br&gt;
What is remote state and how to store it in Azure?&lt;br&gt;
Use Azure Blob Storage:&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;p&gt;Copy&lt;br&gt;
backend "azurerm" {&lt;br&gt;
  resource_group_name  = "rg-tfstate"&lt;br&gt;
  storage_account_name = "tfstorage123"&lt;br&gt;
  container_name       = "tfstate"&lt;br&gt;
  key                  = "terraform.tfstate"&lt;br&gt;
}&lt;br&gt;
🔹 Terraform Interview Questions (Updated)&lt;br&gt;
🟠 Q: What is a Terraform Module?&lt;br&gt;
A:&lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;There are 3 types of modules:&lt;/p&gt;

&lt;p&gt;Root module – The main .tf files in your working directory.&lt;/p&gt;

&lt;p&gt;Child module – A reusable module called by the root or another module.&lt;/p&gt;

&lt;p&gt;Remote module – Modules pulled from the Terraform Registry or GitHub.&lt;/p&gt;

&lt;p&gt;✅ Example:&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;p&gt;Copy&lt;br&gt;
module "network" {&lt;br&gt;
  source     = "./modules/network"&lt;br&gt;
  vnet_name  = "my-vnet"&lt;br&gt;
  subnet_ids = ["subnet1", "subnet2"]&lt;br&gt;
}&lt;br&gt;
💡 Benefits of using modules:&lt;/p&gt;

&lt;p&gt;Code reuse&lt;/p&gt;

&lt;p&gt;Better organization&lt;/p&gt;

&lt;p&gt;Easier team collaboration&lt;/p&gt;

&lt;p&gt;Scalability for complex infrastructure&lt;/p&gt;

&lt;p&gt;🔴 Q: What happens if the Terraform backend state file is deleted? How do you recover it?&lt;br&gt;
A:&lt;br&gt;
If the Terraform state file (terraform.tfstate) is deleted, Terraform loses track of the real infrastructure. This can lead to serious issues like:&lt;/p&gt;

&lt;p&gt;Terraform thinks nothing exists and may try to recreate resources.&lt;/p&gt;

&lt;p&gt;Plans become unreliable.&lt;/p&gt;

&lt;p&gt;You lose visibility into dependencies and changes.&lt;/p&gt;

&lt;p&gt;🛠 How to Fix It:&lt;/p&gt;

&lt;p&gt;Restore from Backup (Recommended):&lt;/p&gt;

&lt;p&gt;If you're using a remote backend like AWS S3, Azure Blob, or Terraform Cloud, these usually have versioning enabled.&lt;/p&gt;

&lt;p&gt;Simply roll back to the last working version of the .tfstate file.&lt;/p&gt;

&lt;p&gt;Manual State Rebuild (Advanced):&lt;/p&gt;

&lt;p&gt;Use terraform import to re-associate existing infrastructure with a new state file.&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;p&gt;Copy&lt;br&gt;
    terraform import azurerm_resource_group.example /subscriptions/xxx/resourceGroups/your-rg&lt;br&gt;
This is tedious and prone to errors — use only if backups are unavailable.&lt;br&gt;
✅ Best Practice:&lt;/p&gt;

&lt;p&gt;Always use remote backends with versioning and state locking enabled to prevent accidental state loss.&lt;/p&gt;

&lt;p&gt;🧠 Final Tips for Readers&lt;/p&gt;

&lt;p&gt;Practice Terraform with Azure free tier.&lt;/p&gt;

&lt;p&gt;Use terraform plan before every apply.&lt;/p&gt;

&lt;p&gt;Store secrets securely using Key Vault or environment variables.&lt;/p&gt;

&lt;p&gt;Automate provisioning with GitHub Actions or Azure DevOps.&lt;/p&gt;

&lt;p&gt;Bookmark &lt;a href="https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs" rel="noopener noreferrer"&gt;https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add Canonical Link (To Avoid Duplicate SEO Issues)&lt;br&gt;
&lt;a href="https://harshalsonar.hashnode.dev/mastering-terraform-30-essential-commands-and-azure-interview-questions-2025-guide" rel="noopener noreferrer"&gt;https://harshalsonar.hashnode.dev/mastering-terraform-30-essential-commands-and-azure-interview-questions-2025-guide&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
