使用不同的nic在Azure中创建多个VM

时间:2020-05-20 08:15:13

标签: azure terraform terraform-provider-azure

我想在具有不同网卡的不同位置创建两个虚拟机。这是我的代码,但是我有一个错误,我不知道为什么,因为在编译代码时建议使用以下解决方案:

variable "locations" {
  type = map(string)
  default = {
    vm1 = "North Europe"
    vm2 = "West Europe"
  }
}

resource "azurerm_network_interface" "main" {
  for_each            = var.locations
  name                = "${each.key}-nic"
  location            = "${each.value}"
  resource_group_name = var.azurerm_resource_group_name

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



resource "azurerm_virtual_machine" "main" {
  for_each              = var.locations
  name                  = "${each.key}t-vm"
  location              = "${each.value}"
  resource_group_name   = var.azurerm_resource_group_name
  network_interface_ids = [azurerm_network_interface.main[each.key]]
  vm_size               = "Standard_D2s_v3"
...
}

错误:

Error: Incorrect attribute value type

  on environment.tf line 68, in resource "azurerm_virtual_machine" "main":
  68:   network_interface_ids = [azurerm_network_interface.main[each.key]]
    |----------------
    | azurerm_network_interface.main is object with 2 attributes
    | each.key is "vm2"

Inappropriate value for attribute "network_interface_ids": element 0: string
required.

1 个答案:

答案 0 :(得分:0)

尝试

network_interface_ids = [azurerm_network_interface.main[each.key].id]