Debug School

Rajesh Kumar
Rajesh Kumar

Posted on

Terraform Azure — Hands-On Assignments - 1 Windows

Assignment 1: Deploy Windows VM and Install IIS Using Terraform Provisioner

Objective: Create a Windows Server VM in Azure and automatically configure IIS Web Server using Terraform.

Requirements:

Create an Azure Resource Group, VNet, Subnet, NSG, Public IP, and Windows VM.

Use a Terraform provisioner to install IIS Web Server.

Configure the IIS service to start automatically and ensure it is running.

Allow HTTP traffic on port 80 through the NSG.

Test IIS by accessing http:// in a browser.

Submission: Write complete, standalone Terraform code and paste it into the comment section, along with the successful test result.

Top comments (6)

Collapse
 
sakshi_gadekar profile image
Sakshi Gadekar •

main.tf

`# --------------------------------------------------

1. Resource Group

--------------------------------------------------

resource "azurerm_resource_group" "sakshi" {
name = var.resource_group_name
location = var.location

tags = {
Owner = "Sakshi"
Assignment = "Terraform-D2-ASS1"
Purpose = "Windows-IIS-Lab"
}
}

--------------------------------------------------

2. Virtual Network

--------------------------------------------------

resource "azurerm_virtual_network" "sakshi" {
name = "vnet-sakshi-d2-ass1"
address_space = ["10.20.0.0/16"]
location = azurerm_resource_group.sakshi.location
resource_group_name = azurerm_resource_group.sakshi.name

tags = {
Owner = "Sakshi"
}
}

--------------------------------------------------

3. Subnet

--------------------------------------------------

resource "azurerm_subnet" "sakshi" {
name = "subnet-sakshi-d2-ass1"
resource_group_name = azurerm_resource_group.sakshi.name
virtual_network_name = azurerm_virtual_network.sakshi.name
address_prefixes = ["10.20.1.0/24"]
}

--------------------------------------------------

4. Network Security Group

--------------------------------------------------

resource "azurerm_network_security_group" "sakshi" {
name = "nsg-sakshi-d2-ass1"
location = azurerm_resource_group.sakshi.location
resource_group_name = azurerm_resource_group.sakshi.name

security_rule {
name = "Allow-HTTP"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = ""
destination_port_range = "80"
source_address_prefix = "
"
destination_address_prefix = "*"
}

tags = {
Owner = "Sakshi"
}
}

--------------------------------------------------

5. Public IP

--------------------------------------------------

resource "azurerm_public_ip" "sakshi" {
name = "pip-sakshi-d2-ass1"
location = azurerm_resource_group.sakshi.location
resource_group_name = azurerm_resource_group.sakshi.name

allocation_method = "Static"
sku = "Standard"

tags = {
Owner = "Sakshi"
}
}

--------------------------------------------------

6. Network Interface

--------------------------------------------------

resource "azurerm_network_interface" "sakshi" {
name = "nic-sakshi-d2-ass1"
location = azurerm_resource_group.sakshi.location
resource_group_name = azurerm_resource_group.sakshi.name

ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.sakshi.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.sakshi.id
}

tags = {
Owner = "Sakshi"
}
}

--------------------------------------------------

7. Associate NSG with Network Interface

--------------------------------------------------

resource "azurerm_network_interface_security_group_association" "sakshi" {
network_interface_id = azurerm_network_interface.sakshi.id
network_security_group_id = azurerm_network_security_group.sakshi.id
}

--------------------------------------------------

8. Windows Virtual Machine

--------------------------------------------------

resource "azurerm_windows_virtual_machine" "sakshi" {
name = "sakshi-d2a1-vm"
computer_name = "sakshi-d2a1-vm"
resource_group_name = azurerm_resource_group.sakshi.name
location = azurerm_resource_group.sakshi.location
size = var.vm_size

admin_username = var.admin_username
admin_password = var.admin_password

network_interface_ids = [
azurerm_network_interface.sakshi.id
]

provision_vm_agent = true

os_disk {
name = "osdisk-sakshi-d2-ass1"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2022-datacenter-g2"
version = "latest"
}

tags = {
Owner = "Sakshi"
Assignment = "Terraform-D2-ASS1"
Purpose = "IIS-Web-Server"
}

# Ensure the NSG association is completed before
# creating the VM and running the IIS provisioner.
depends_on = [
azurerm_network_interface_security_group_association.sakshi
]

# ------------------------------------------------
# 9. Terraform Provisioner
# ------------------------------------------------

provisioner "local-exec" {
interpreter = ["/bin/bash", "-c"]

command = <<-EOT
  set -e
  echo "Installing IIS on ${self.name} using Azure VM Run Command..."

  az vm run-command invoke \
    --resource-group "${self.resource_group_name}" \
    --name "${self.name}" \
    --command-id RunPowerShellScript \
    --scripts "$(cat '${path.module}/install-iis.ps1')" \
    --output json
EOT
Enter fullscreen mode Exit fullscreen mode

}
}`

install-iis.ps1

$ErrorActionPreference = "Stop"

Write-Output "Starting IIS installation for Sakshi's assignment..."

Install IIS and its management tools

Install-WindowsFeature -Name Web-Server -IncludeManagementTools

Configure IIS to start automatically

Set-Service -Name W3SVC -StartupType Automatic

Start IIS

Start-Service -Name W3SVC

Create a custom webpage

$html = @'
<!DOCTYPE html>


Sakshi - Terraform Assignment 1


Hello from Sakshi's Azure Windows VM!


IIS has been installed using a Terraform provisioner.


Assignment: Terraform Azure - Hands-On Assignment 1




'@

Set-Content
-Path "C:\inetpub\wwwroot\index.html"

-Value $html `
-Encoding UTF8

Verify that the IIS service is running

$service = Get-Service -Name W3SVC

if ($service.Status -ne "Running") {
throw "IIS service is not running."
}

if ($service.StartType -ne "Automatic") {
throw "IIS service is not configured for automatic startup."
}

Write-Output "IIS installation completed successfully."
Get-Service -Name W3SVC

Collapse
 
tariq_alghawanmeh_68388ac profile image
tariq alghawanmeh • • Edited


# ============================================
# Project 1
# ============================================

terraform {
  required_version = ">= 1.15.0, < 2.0.0"

  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 5.9.0"
    }
  }
}

provider "azurerm" {
  features {}
}

# VARIABLES

variable "admin_username" {
  description = "Local administrator username on the Windows VM."
  type        = string
  default     = "labadmin"
}

variable "admin_password" {
  description = "Local administrator password on the Windows VM."
  type        = string
  sensitive   = true
}

variable "my_ip_cidr" {
  description = "Your public IP, allowed to reach WinRM."
  type        = string
  default     = "213.49.27.136/32"
}

# Requirement 1: RG, VNet, Subnet, NSG, Public IP, Windows VM

# Resource Group
resource "azurerm_resource_group" "lab" {
  name     = "tg-assignment2"
  location = "westus3"
}

# VNet
resource "azurerm_virtual_network" "lab" {
  name                = "tg-assignment2-vnet"
  location            = azurerm_resource_group.lab.location
  resource_group_name = azurerm_resource_group.lab.name
  address_space       = ["10.30.0.0/16"]
}

# Subnet
resource "azurerm_subnet" "lab" {
  name                 = "tg-assignment2-subnet"
  resource_group_name  = azurerm_resource_group.lab.name
  virtual_network_name = azurerm_virtual_network.lab.name
  address_prefixes     = ["10.30.1.0/24"]
}

# NSG
resource "azurerm_network_security_group" "lab" {
  name                = "tg-assignment2-nsg"
  location            = azurerm_resource_group.lab.location
  resource_group_name = azurerm_resource_group.lab.name
}

# NSG association
resource "azurerm_subnet_network_security_group_association" "lab" {
  subnet_id                 = azurerm_subnet.lab.id
  network_security_group_id = azurerm_network_security_group.lab.id
}

# Public IP
resource "azurerm_public_ip" "windows" {
  name                = "tg-assignment2-pip"
  location            = azurerm_resource_group.lab.location
  resource_group_name = azurerm_resource_group.lab.name
  allocation_method   = "Static"
  sku                 = "Standard"
}

# NIC
resource "azurerm_network_interface" "windows" {
  name                = "tg-assignment2-nic"
  location            = azurerm_resource_group.lab.location
  resource_group_name = azurerm_resource_group.lab.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.lab.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.windows.id
  }
}

# Windows VM
resource "azurerm_windows_virtual_machine" "windows" {
  name                = "tg-win-iis-01"
  location            = azurerm_resource_group.lab.location
  resource_group_name = azurerm_resource_group.lab.name
  size                = "Standard_D2ds_v6"
  admin_username      = var.admin_username
  admin_password      = var.admin_password

  network_interface_ids = [
    azurerm_network_interface.windows.id
  ]

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  # Gen2 image
  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2025-datacenter-g2"
    version   = "latest"
  }

  connection {
    type     = "winrm"
    host     = self.public_ip_address
    user     = var.admin_username
    password = var.admin_password
    port     = 5985
    https    = false
    timeout  = "10m"
  }

  provisioner "local-exec" {
    command = <<-EOT
      az vm run-command invoke \
        --ids ${self.id} \
        --command-id RunPowerShellScript \
        --scripts "Enable-PSRemoting -Force -SkipNetworkProfileCheck" "Set-NetFirewallRule -Name WINRM-HTTP-In-TCP-PUBLIC -RemoteAddress Any" "Set-Item WSMan:\localhost\Service\AllowUnencrypted -Value true" "Set-Item WSMan:\localhost\Service\Auth\Basic -Value true"
    EOT
  }

  # Requirement 2 & 3: Install IIS, start it automatically and make sure it is running
  provisioner "remote-exec" {
    inline = [
      "powershell -Command \"Install-WindowsFeature -Name Web-Server -IncludeManagementTools\"",
      "powershell -Command \"Set-Service -Name W3SVC -StartupType Automatic\"",
      "powershell -Command \"Start-Service -Name W3SVC\"",
    ]
  }
}

# WinRM rule
resource "azurerm_network_security_rule" "winrm" {
  name                        = "Allow-WinRM"
  priority                    = 120
  direction                   = "Inbound"
  access                      = "Allow"
  protocol                    = "Tcp"
  source_port_range           = "*"
  destination_port_range      = "5985"
  source_address_prefix       = var.my_ip_cidr
  destination_address_prefix  = "*"
  resource_group_name         = azurerm_resource_group.lab.name
  network_security_group_name = azurerm_network_security_group.lab.name
}

# Requirement 4: Allow HTTP traffic on port 80 through the NSG
resource "azurerm_network_security_rule" "http" {
  name                        = "Allow-HTTP"
  priority                    = 110
  direction                   = "Inbound"
  access                      = "Allow"
  protocol                    = "Tcp"
  source_port_range           = "*"
  destination_port_range      = "80"
  source_address_prefix       = "*"
  destination_address_prefix  = "*"
  resource_group_name         = azurerm_resource_group.lab.name
  network_security_group_name = azurerm_network_security_group.lab.name
}

output "iis_url" {
  value = "http://${azurerm_public_ip.windows.ip_address}"
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
anirudha_ingole_9c8e0ffd0 profile image
Anirudha ingole •

resource "azurerm_resource_group" "rg" {
name = "rg-iis-demo"
location = var.location
}

resource "azurerm_virtual_network" "vnet" {
name = "vnet-iis-demo"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

address_space = ["10.0.0.0/16"]
}

resource "azurerm_subnet" "subnet" {
name = "subnet-iis"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name

address_prefixes = ["10.0.1.0/24"]
}

resource "azurerm_public_ip" "pip" {
name = "pip-iis"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

allocation_method = "Static"
sku = "Standard"
}

resource "azurerm_network_security_group" "nsg" {
name = "nsg-iis"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}

resource "azurerm_network_security_rule" "rdp" {
name = "Allow-RDP"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"

source_port_range = "*"
destination_port_range = "3389"

source_address_prefix = ""
destination_address_prefix = "
"

resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.nsg.name
}

resource "azurerm_network_security_rule" "http" {
name = "Allow-HTTP"
priority = 110
direction = "Inbound"
access = "Allow"
protocol = "Tcp"

source_port_range = "*"
destination_port_range = "80"

source_address_prefix = ""
destination_address_prefix = "
"

resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.nsg.name
}

resource "azurerm_network_security_rule" "winrm" {
name = "Allow-WinRM"
priority = 120
direction = "Inbound"
access = "Allow"
protocol = "Tcp"

source_port_range = "*"
destination_port_range = "5985"

source_address_prefix = ""
destination_address_prefix = "
"

resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.nsg.name
}

resource "azurerm_subnet_network_security_group_association" "assoc" {
subnet_id = azurerm_subnet.subnet.id
network_security_group_id = azurerm_network_security_group.nsg.id
}

resource "azurerm_network_interface" "nic" {
name = "nic-iis"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.pip.id
}
}

resource "azurerm_windows_virtual_machine" "vm" {
name = "vm-iis"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

size = "Standard_B2s"
admin_username = var.admin_username
admin_password = var.admin_password

network_interface_ids = [
azurerm_network_interface.nic.id
]

os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2025-datacenter-g2"
version = "latest"
}
}

resource "null_resource" "iis_install" {

depends_on = [
azurerm_windows_virtual_machine.vm
]

connection {
type = "winrm"
host = azurerm_public_ip.pip.ip_address
user = var.admin_username
password = var.admin_password

https    = false
insecure = true
timeout  = "15m"
Enter fullscreen mode Exit fullscreen mode

}

provisioner "local-exec" {
command = "echo Installing IIS on ${azurerm_public_ip.pip.ip_address}"
}

provisioner "file" {
source = "install-iis.ps1"
destination = "C:/Windows/Temp/install-iis.ps1"
}

provisioner "remote-exec" {
inline = [
"powershell -ExecutionPolicy Bypass -File C:/Windows/Temp/install-iis.ps1"
]
}
}

Collapse
 
diptarag profile image
Diptarag Saha •

terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.90"
}
}

backend "azurerm" {
resource_group_name = "rg-terraform-lab-dip"
storage_account_name = "erraform"
container_name = "tfstate"
key = "terraform.tfstate"
}
}

provider "azurerm" {
features {}
}

variable "admin_username" {
default = "azureuser"
}

variable "admin_password" {
type = string
sensitive = true
default = "Dip@12345"
}

resource "azurerm_resource_group" "rg" {
name = "iis-assignment-day2-rg"
location = "WEST US 3"
}

resource "azurerm_virtual_network" "vnet" {
name = "iis-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}

resource "azurerm_subnet" "subnet" {
name = "iis-subnet"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.0.1.0/24"]
}

resource "azurerm_network_security_group" "nsg" {
name = "iis-nsg"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

security_rule {
name = "allow-http"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = ""
destination_port_range = "80"
source_address_prefix = "
"
destination_address_prefix = "*"
}

security_rule {
name = "allow-rdp"
priority = 110
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = ""
destination_port_range = "3389"
source_address_prefix = "
"
destination_address_prefix = "*"
}
}

resource "azurerm_subnet_network_security_group_association" "nsg_assoc" {
subnet_id = azurerm_subnet.subnet.id
network_security_group_id = azurerm_network_security_group.nsg.id
}

resource "azurerm_public_ip" "pip" {
name = "iis-vm-pip"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
allocation_method = "Static"
sku = "Standard"
}

resource "azurerm_network_interface" "nic" {
name = "iis-vm-nic"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.pip.id
}
}

resource "azurerm_windows_virtual_machine" "vm" {
name = "iis-win-vm"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
size = "Standard_B2s"
admin_username = var.admin_username
admin_password = var.admin_password
network_interface_ids = [azurerm_network_interface.nic.id]

os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2022-datacenter-azure-edition"
version = "latest"
}

depends_on = [azurerm_subnet_network_security_group_association.nsg_assoc]
}

resource "azurerm_virtual_machine_extension" "install_iis" {
name = "install-iis"
virtual_machine_id = azurerm_windows_virtual_machine.vm.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.10"

settings = < {
"commandToExecute": "powershell -Command \"Install-WindowsFeature -Name Web-Server -IncludeManagementTools; Set-Service -Name W3SVC -StartupType Automatic; Start-Service -Name W3SVC; New-NetFirewallRule -DisplayName 'allow http 80' -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow; Set-Content -Path 'C:\\\\inetpub\\\\wwwroot\\\\iisstart.htm' -Value '

IIS installed via Terraform - test page

'\""
}
SETTINGS
}

output "vm_public_ip" {
value = azurerm_public_ip.pip.ip_address
}

Collapse
 
gauri_thorat_c64a5d5bdfa0 profile image
Gauri Thorat •

terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>4.0"
}
}
}

provider "azurerm" {
features {}
subscription_id = "092c8f39-7113-46c4-8cce-d26a6afd9f67"
}

resource "azurerm_resource_group" "rg" {
name = "rg-gt-demo"
location = "Central India"
}

resource "azurerm_virtual_network" "vnet" {
name = "vnet-demo"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
address_space = ["10.0.0.0/16"]
}

resource "azurerm_subnet" "subnet" {
name = "subnet-demo"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.0.1.0/24"]
}

resource "azurerm_network_security_group" "nsg" {
name = "nsg-demo"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

security_rule {
name = "AllowHTTP"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = ""
destination_port_range = "80"
source_address_prefix = "
"
destination_address_prefix = "*"
}
}

resource "azurerm_public_ip" "pip" {
name = "pip-demo"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
allocation_method = "Static"
sku = "Standard"
}

resource "azurerm_network_interface" "nic" {
name = "nic-demo"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.pip.id
}
}

resource "azurerm_network_interface_security_group_association" "assoc" {
network_interface_id = azurerm_network_interface.nic.id
network_security_group_id = azurerm_network_security_group.nsg.id
}

resource "azurerm_windows_virtual_machine" "vm" {
name = "iis-vm"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
size = "Standard_B2s"

admin_username = "azureadmin"
admin_password = "P@ssw0rd1234!"

network_interface_ids = [
azurerm_network_interface.nic.id
]

os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2022-datacenter"
version = "latest"
}
}

resource "azurerm_virtual_machine_extension" "iis" {
name = "install-iis"
virtual_machine_id = azurerm_windows_virtual_machine.vm.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.10"

settings = <<SETTINGS
{
"commandToExecute": "powershell Add-WindowsFeature Web-Server; Set-Service W3SVC -StartupType Automatic; Start-Service W3SVC"
}
SETTINGS
}

output "public_ip" {
value = azurerm_public_ip.pip.ip_address
}

Collapse
 
loni_hatimuria_48462a7278 profile image
Loni Hatimuria •

main.tf
terraform {
required_version = ">= 1.15.0, < 2.0.0"

required_providers {
  azurerm = {
    source  = "hashicorp/azurerm"
  version = "~> 5.9.0"
}
Enter fullscreen mode Exit fullscreen mode

}
}

provider "azurerm" {
features {}
}

1. Resource Group

resource "azurerm_resource_group" "iis" {
name = "rg-windows-iis-demo"
location = var.location
}

2. Virtual Network

resource "azurerm_virtual_network" "iis" {
name = "vnet-windows-iis"
location = azurerm_resource_group.iis.location
resource_group_name = azurerm_resource_group.iis.name

address_space = ["10.30.0.0/16"]
}

3. Subnet

resource "azurerm_subnet" "iis" {
name = "snet-windows-iis"
resource_group_name = azurerm_resource_group.iis.name
virtual_network_name = azurerm_virtual_network.iis.name

address_prefixes = ["10.30.1.0/24"]
}

4. Network Security Group

resource "azurerm_network_security_group" "iis" {
name = "nsg-windows-iis"
location = azurerm_resource_group.iis.location
resource_group_name = azurerm_resource_group.iis.name

# HTTP website access
security_rule {
name = "Allow-HTTP"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = ""
destination_port_range = "80"
source_address_prefix = "
"
destination_address_prefix = "*"
}

# WinRM access restricted to your public IP
security_rule {
name = "Allow-WinRM"
priority = 110
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = ""
destination_port_range = "5985"
source_address_prefix = var.admin_ip_cidr
destination_address_prefix = "
"
}
}

5. Public IP

resource "azurerm_public_ip" "iis" {
name = "pip-windows-iis"
location = azurerm_resource_group.iis.location
resource_group_name = azurerm_resource_group.iis.name

allocation_method = "Static"
sku = "Standard"
}

6. Network Interface

resource "azurerm_network_interface" "iis" {
name = "nic-windows-iis"
location = azurerm_resource_group.iis.location
resource_group_name = azurerm_resource_group.iis.name

ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.iis.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.iis.id
}
}

7. Associate NSG with NIC

resource "azurerm_network_interface_security_group_association" "iis" {
network_interface_id = azurerm_network_interface.iis.id
network_security_group_id = azurerm_network_security_group.iis.id
}

8. Windows Server VM

resource "azurerm_windows_virtual_machine" "iis" {
name = "vm-windows-iis"
computer_name = "WINIISDEMO"
location = azurerm_resource_group.iis.location
resource_group_name = azurerm_resource_group.iis.name
size = var.vm_size

admin_username = var.admin_username
admin_password = var.admin_password

network_interface_ids = [
azurerm_network_interface.iis.id
]

os_disk {
name = "disk-windows-iis-os"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2022-datacenter"
version = "latest"
}

# Configure a WinRM HTTP listener during VM provisioning.
winrm_listener {
protocol = "Http"
}

depends_on = [
azurerm_network_interface_security_group_association.iis
]
}

9. Enable WinRM and its Windows Firewall rules.

This extension runs inside the VM before remote-exec.

resource "azurerm_virtual_machine_extension" "winrm" {
name = "ConfigureWinRM"
virtual_machine_id = azurerm_windows_virtual_machine.iis.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.10"
auto_upgrade_minor_version = true

protected_settings = jsonencode({
commandToExecute = "powershell.exe -ExecutionPolicy Bypass -Command \"Set-Service WinRM -StartupType Automatic; Start-Service WinRM; winrm quickconfig -q; Enable-NetFirewallRule -DisplayGroup 'Windows Remote Management'\""
})

depends_on = [
azurerm_network_interface_security_group_association.iis
]
}

10. Run commands remotely inside Windows using WinRM.

terraform_data is separate from the VM so the extension can

finish configuring WinRM before the provisioner connects.

resource "terraform_data" "configure_iis" {
triggers_replace = [
azurerm_windows_virtual_machine.iis.id,
azurerm_virtual_machine_extension.winrm.id
]

connection {
type = "winrm"
host = azurerm_public_ip.iis.ip_address
user = var.admin_username
password = var.admin_password

port    = 5985
https   = false
use_ntlm = true
timeout = "20m"
Enter fullscreen mode Exit fullscreen mode

}

provisioner "remote-exec" {
inline = [
"whoami",
"hostname",
"Install-WindowsFeature -Name Web-Server -IncludeManagementTools",
"Set-Service -Name W3SVC -StartupType Automatic",
"Start-Service -Name W3SVC",
"New-NetFirewallRule -DisplayName 'Allow IIS HTTP' -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow -ErrorAction SilentlyContinue",
"Set-Content -Path 'C:\inetpub\wwwroot\index.html' -Value 'IIS successfully installed by Terraform remote-exec!'",
"Get-Service W3SVC"
]
}

depends_on = [
azurerm_virtual_machine_extension.winrm
]
}