Review Azure VM network-interface attachment

Attach a valid NIC and configure subnet, NSG and routing controls separately.

Description

An Azure VM connects to a subnet through a NIC. Set network_interface_ids to the resource ID of the NIC the VM should use.

Potential impact

An empty list prevents deployment. Once a NIC is attached, its subnet and NIC security rules still determine what traffic can reach the VM.

Remediation

Create the NIC with the intended subnet and IP configuration, then reference its ID from the VM. Check the NIC and subnet NSGs and routes, and test the required connections from an authorized client.

Examples

These excerpts use the legacy azurerm_virtual_machine resource. The second attaches a NIC with a dynamic private IP. Supply the omitted OS disk, image and credentials, and review the password authentication left enabled in both.

Before

hcl
resource "azurerm_virtual_machine" "example" {
  name                  = "${var.prefix}-vm"
  location              = azurerm_resource_group.main.location
  resource_group_name   = azurerm_resource_group.main.name
  network_interface_ids = []
  vm_size               = "Standard_DS1_v2"

  os_profile_linux_config {
    disable_password_authentication = false
  }
}

After

hcl
resource "azurerm_network_interface" "example" {
  name                = "${var.prefix}-nic"
  location            = azurerm_resource_group.main.location
  resource_group_name = azurerm_resource_group.main.name

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = azurerm_subnet.internal.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_virtual_machine" "example" {
  name                  = "${var.prefix}-vm"
  location              = azurerm_resource_group.main.location
  resource_group_name   = azurerm_resource_group.main.name
  network_interface_ids = [azurerm_network_interface.example.id]
  vm_size               = "Standard_DS1_v2"

  os_profile_linux_config {
    disable_password_authentication = false
  }
}

References