DEV Community

Smallsun2025
Smallsun2025

Posted on

Deploy Windows VM with NSG and IIS using Terraform on Azure

🧭 Introduction(引言)
Deploying a Windows Virtual Machine (VM) with a Network Security Group (NSG) and installing IIS is a common scenario for setting up demo environments or learning web server basics. In this tutorial, we’ll use Terraform to fully automate this setup on Azure — ideal for anyone learning cloud infrastructure as code (IaC).

🗂 Project Structure
Here’s a quick overview of the project files:
terraform-windows-vm-iis/
├── main.tf # Core resource definitions
├── variables.tf # Variable declarations
├── terraform.tfvars # Variable values
├── outputs.tf # Output info (IP address, etc.)
├── iis-install.ps1 # Startup script to install IIS

🔧 What the Terraform Code Does
Creates a Resource Group to hold all the infrastructure.

Deploys a Windows Virtual Machine using azurerm_windows_virtual_machine.

Creates and attaches a Public IP, Network Interface, NSG, and associates everything together.

Installs IIS on the VM automatically by running a PowerShell script (iis-install.ps1) via the custom_data property.

🚀 How to Deploy
Make sure you have:

Azure CLI installed and logged in (az login)

Terraform installed

Then follow these steps:
terraform init
terraform plan
terraform apply

When prompted, type yes.

After a few minutes, you’ll get the public IP output. Paste that into your browser, and you should see the default IIS welcome page!

✅ Conclusion
With just a few lines of Terraform, you've deployed a fully functional IIS server on Azure. This is a perfect beginner-friendly project to understand how to provision VMs, work with NSGs, and automate server initialization.

🔜 Coming Next...
How to manage Azure VMs with Run Command

Adding custom domain and SSL to IIS

Creating reusable Terraform modules

If you found this useful, feel free to ⭐️ the repo and share feedback!

Top comments (0)