Introduction
Infrastructure as Code (IaC) is essential for managing cloud infrastructure efficiently. Terraform, an open-source IaC tool, allows you to define, deploy, and manage Azure resources in a repeatable and automated way.
In this guide, we’ll walk through deploying an Azure Virtual Machine (VM) using Terraform, covering:
✔ Defining a resource group
✔ Creating a virtual network and subnet
✔ Configuring a public IP and network interface
✔ Deploying a Windows Virtual Machine
✔ Running Terraform commands to provision infrastructure
By the end, you’ll have a fully functional Azure VM managed via Terraform, making deployments more efficient and scalable. 🚀
Prerequisites
Before getting started, ensure you have:
✅ An active Azure subscription
✅ Azure CLI installed (az --version)
✅ Terraform installed (terraform -v)
✅ Editor (VS Code recommended with Terraform extension)
Step 1: Initialize the Terraform Project
Create a new directory for your Terraform project and navigate into it:
|  |  | 
Create the Terraform configuration file:
|  |  | 
Open main.tf in your editor to begin defining your infrastructure.
Step 2: Configure the Azure Provider
At the top of main.tf, define the Azure provider:
|  |  | 
This tells Terraform to use the Azure Resource Manager (AzureRM) provider for managing Azure resources.
Step 3: Define the Resource Group
A Resource Group is required to organize and manage related Azure resources. Define it in main.tf:
|  |  | 
Step 4: Create a Virtual Network and Subnet
Define a Virtual Network (VNet) and Subnet for networking within the Resource Group:
|  |  | 
Step 5: Configure a Public IP Address
To allow external access, create a Public IP address:
|  |  | 
Step 6: Define a Network Interface (NIC)
The VM needs a Network Interface (NIC) to connect to the Subnet and Public IP:
|  |  | 
Step 7: Define the Virtual Machine
Before defining the VM, create variables for secure credential handling:
|  |  | 
Now, define the Windows Virtual Machine:
|  |  | 
Step 8: Deploy the Virtual Machine with Terraform
1️⃣ Authenticate with Azure
Log in using the Azure CLI:
|  |  | 
If you have multiple subscriptions, set the correct one:
|  |  | 
2️⃣ Initialize Terraform
Run terraform init to prepare Terraform for deployment:
|  |  | 
3️⃣ Preview Changes with Terraform Plan
|  |  | 
4️⃣ Apply the Terraform Configuration
|  |  | 
Step 9: Get the VM Public IP and Connect
Retrieve the public IP:
|  |  | 
Use Remote Desktop (RDP) to connect to the Windows VM:
|  |  | 
Step 10: Clean Up Resources
|  |  | 
Conclusion
We successfully deployed an Azure Virtual Machine using Terraform, covering:
✅ Infrastructure as Code (IaC) principles
✅ Defining a Virtual Network, Subnet, and Public IP
✅ Deploying a Windows VM securely
✅ Running Terraform commands to manage resources
Next Steps:
🔹 Use Azure Key Vault to store secrets securely.
🔹 Implement remote state management using Azure Storage.
🔹 Set up CI/CD pipelines with GitHub Actions for automated deployments.
🚀 Have any questions or suggestions? Drop them in the comments! 💬
🔥 Want to automate more Azure resources? Stay tuned for future Terraform guides! 🚀