使用for_each创建具有唯一 NIC 的不同数量的 VM



我正在尝试使用不同的配置创建不同数量的VM。我在azurerm_windows_virtual_machine资源上设置for_each,并循环浏览tfvars文件中的映射集。变量在模块中设置,但在tfvars文件中定义。

我希望能够创建x个虚拟机,每个虚拟机都连接了一个唯一的NIC。我可以创建虚拟机,但由于NIC不是唯一的,因此配置失败。我尝试使用相同的变量添加for_each,但我得到了以下错误:

Error: Incorrect attribute value type
on ../modules/compute/windows_vm/windows_vm.tf line 96, in resource "azurerm_network_interface_application_security_group_association" "application_security_group_association":
96:   network_interface_id          = [azurerm_network_interface.network_interface[each.key].id]
Inappropriate value for attribute "network_interface_id": string required.

Error: Incorrect attribute value type
on ../modules/compute/windows_vm/windows_vm.tf line 96, in resource "azurerm_network_interface_application_security_group_association" "application_security_group_association":
96:   network_interface_id          = [azurerm_network_interface.network_interface[each.key].id]
Inappropriate value for attribute "network_interface_id": string required.

这是我的代码:

# VM Network Interface
resource "azurerm_network_interface" "network_interface" {
for_each                      = var.servers
name                          = "nic-${var.environment}-${var.directorate}-${var.business_unit}-${var.vm_identifier}${each.value.name}"
resource_group_name           = var.resource_group
location                      = var.location
enable_ip_forwarding          = "false"
enable_accelerated_networking = "false"
ip_configuration {
name                          = "ipconfig1"
subnet_id                     = data.azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
primary                       = "true"
}

}
# Application Security Group
resource "azurerm_application_security_group" "application_security_group" {
name                = "asg-${var.environment}-${var.directorate}-${var.business_unit}-${var.vm_identifier}"
resource_group_name = var.resource_group
location            = var.location
}
resource "azurerm_network_interface_application_security_group_association" "application_security_group_association" {
for_each                      = var.servers
network_interface_id          = [azurerm_network_interface.network_interface[each.key].id]
application_security_group_id = azurerm_application_security_group.application_security_group.id

}
resource "azurerm_network_interface_security_group_association" "network_security_group_association" {
for_each                  = var.servers
network_interface_id      = [azurerm_network_interface.network_interface[each.key].id]
network_security_group_id = azurerm_network_security_group.network_security_group.id
}
# Azure Virtual Machine
resource "azurerm_windows_virtual_machine" "virtual_machine" {
for_each                         = var.servers
name                             = "vm-${var.environment}-${var.vm_identifier}${each.value.name}"
location                         = var.location
resource_group_name              = var.resource_group
zone                             = each.value.zone
size                             = var.vm_size
network_interface_ids            = [azurerm_network_interface.network_interface[each.key].id]
computer_name                    = "${var.vm_identifier}${each.value.name}"
admin_username                   = xxxx
admin_password                   = xxxx
provision_vm_agent               = "true"
source_image_id                  = data.azurerm_shared_image.shared_image.id

boot_diagnostics {
storage_account_uri = data.azurerm_storage_account.diag_storage_account.primary_blob_endpoint
}
os_disk {
name                      = "vm-${var.environment}-${var.directorate}-${var.business_unit}-${var.vm_identifier}-os${each.value.name}"
caching                   = "ReadWrite"
storage_account_type      = "Premium_LRS"
}
depends_on = [azurerm_network_interface.network_interface]
}

在for_each中使用并在模块变量中设置的根模块内的变量.tf:

variable "servers" {
description = "Variable for defining each instance"
}

模块中映射到每个环境的每个tfvar的变量:

variable "desktop_servers" {
description = "Variable for defining each instance"
}
variable "db_servers" {
description = "Variable for defining each instance"
}

然后在tfvars中定义上述内容如下:

desktop_servers = {
"Server_1" = {
name = 1,
zone = 1
}
"Server_2" = {
name = 2,
zone = 2
}
"Server_3" = {
name = 3,
zone = 3
}
}
db_servers = {
"Server_1" = {
name = 1,
zone = 1
}
}

您正在向network_interface_id分配一个列表,但它应该只是一个字符串。

因此,代替

network_interface_id          = [azurerm_network_interface.network_interface[each.key].id]

应该是

network_interface_id          = azurerm_network_interface.network_interface[each.key].id

最新更新