I have this snippet from my code:
this will create 3 NICs as defined in number_of_resources
resource "azurerm_network_interface" "nic" {
count = var.number_of_resources
name = "nic-${element(var.resourcese_name, count.index)}"
location = var.location
resource_group_name = var.rg
ip_configuration {
subnet_id = azurerm_virtual_network.main_net.*.subnet[0].id
}
}
The VM block, this will create 3 VMs:
resource "azurerm_windows_virtual_machine" "VMs"{
count = var.number_of_resources
name = "VM-${element(var.resources_name, count.index)}"
resource_group_name = var.rg
location = var.location
network_interface_ids = [?]
}
Each VM requires its own Network Interface Card (NIC) to be attached to the same subnet. I have already defined the necessary variables and resources for creating the VMs and their respective NICs.
However, I'm facing an issue when trying to associate each VM with its corresponding NIC using their IDs.
Same issue when trying to associate nsg with NICs:
resource "azurerm_network_interface_security_group_association" "example" {
count = var.number_of_resources
network_interface_id = azurerm_network_interface.nic[count.index + 1].id
network_security_group_id = azurerm_network_security_group.main-nsg[count.index + 1].id
}
Variable.tf:
variable "rg" {
description = "The default resource group for this proj"
type = string
default = "rg_ADLab"
}
variable "location"{
description = "the default location for this proj"
type = string
default = "East US"
}
variable "resources_name" {
type = list(string)
default = [ "DC","Client1", "Client2" ]
}
variable "number_of_resources"{
type = number
default = 3
}
Based on the code provided in the quesion, this should work:
resource "azurerm_windows_virtual_machine" "VMs"{
count = var.number_of_resources
name = "VM-${element(var.resources_name, count.index)}"
resource_group_name = var.rg
location = var.location
network_interface_ids = [azurerm_network_interface.nic[count.index].id]
}