Terraform Azurerm -当虚拟机没有在az区域兼容区域中创建时,使用可用性设置,但是如何设置?

nxowjjhe  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(118)

我想只通过调用一个自己编写的模块来部署一堆资源:

module "transit-gateway-sea" {
  source             = "./modules/transit-gateway"
  location           = "southeastasia"
  vnet_address_space = [local.sea_vnet_address_space]
  subnet_address_spaces = {
    mgmt0 = [cidrsubnets(local.sea_vnet_address_space, 2, 2, 2, 2, )[0]]
    wan0  = [cidrsubnets(local.sea_vnet_address_space, 2, 2, 2, 2, )[1]]
    lan0  = [cidrsubnets(local.sea_vnet_address_space, 2, 2, 2, 2, )[2]]
  }
  bastion_subnet = [cidrsubnets(local.sea_vnet_address_space, 2, 2, 2, 2, )[3]]
  ha_enabled = true
}

在这个模块中会发生一些事情,但要知道的是,我根据区域为局部变量赋值,如下所示:

locals {
  country_code = (var.location == "southeastasia" ? "-sea" :
    var.location == "westeurope" ? "-weu" :
    var.location == "northcentralus" ? "-ncus" :
    var.location == "brazilsouth" ? "-bs" :
    var.location == "northeurope" ? "-neu" :
    ""
  )
  primary_zone = (var.location == "southeastasia" ? "1" :
    var.location == "westeurope" ? "1" :
    var.location == "brazilsouth" ? "1" :
    var.location == "northeurope" ? "1" :
    null
  )
  secondary_zone = (var.location == "southeastasia" ? "2" :
    var.location == "westeurope" ? "2" :
    var.location == "brazilsouth" ? "2" :
    var.location == "northeurope" ? "2" :
    null
  )
}

请在下面查找VM以及可用性集和区域的代码。只有在模块调用期间ha_enabled变量为true时,才会部署辅助VM。相同的逻辑在某种程度上适用于可用性集,但它取决于该区域是否支持可用区域。如果不支持,则应部署可用集,并将两个VM都分配给此可用集。

resource "azurerm_availability_set" "aset" {
  count = local.primary_zone != "1" ? 0 : 1

  name                = "silverpeak-sdwan${local.country_code}-aset"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  lifecycle {
    ignore_changes = [
      tags
    ]
  }
}

resource "azurerm_linux_virtual_machine" "primary-vm" {
  count               = 1
  name                = "silverpeak-sdwan${local.country_code}-primary-vm"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  size                = var.vm_size
  admin_username = "adminuser"
  admin_password = random_password.admin-password-primary.result
  disable_password_authentication = false
  zone                            = local.primary_zone
  encryption_at_host_enabled      = true
  allow_extension_operations      = false
  availability_set_id             = local.primary_zone != "1" ? azurerm_availability_set.aset[count.index].id : null

  network_interface_ids = [
    for nics in azurerm_network_interface.primary-nics : nics.id
  ]

  os_disk {
    name                 = "silverpeak-sdwan${local.country_code}-primary-vm-osdisk"
    caching              = "ReadWrite"
    storage_account_type = var.storage_account_type
  }

  source_image_reference {
    publisher = "silver-peak-systems"
    offer     = "silver_peak_edgeconnect_vwan"
    sku       = "silver_peak_edgeconnect_vwan_8_3_0_14"
    version   = "8.3.0"
  }
  plan {
    name      = "silver_peak_edgeconnect_vwan_8_3_0_14"
    publisher = "silver-peak-systems"
    product   = "silver_peak_edgeconnect_vwan"
  }

  lifecycle {
    ignore_changes = [
      tags
    ]
  }
}

resource "azurerm_linux_virtual_machine" "secondary-vm" {
  count               = var.ha_enabled ? 1 : 0
  name                = "silverpeak-sdwan${local.country_code}-secondary-vm"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  size                = var.vm_size
  admin_username = "adminuser"
  admin_password = random_password.admin-password-secondary.result
  disable_password_authentication = false
  zone                            = local.secondary_zone
  encryption_at_host_enabled      = true
  allow_extension_operations      = false
  availability_set_id             = local.secondary_zone != "2" ? azurerm_availability_set.aset[count.index].id : null

  network_interface_ids = [
    for nics in azurerm_network_interface.secondary-nics : nics.id
  ]

  os_disk {
    name                 = "silverpeak-sdwan${local.country_code}-secondary-vm-osdisk"
    caching              = "ReadWrite"
    storage_account_type = var.storage_account_type
  }

  source_image_reference {
    publisher = "silver-peak-systems"
    offer     = "silver_peak_edgeconnect_vwan"
    sku       = "silver_peak_edgeconnect_vwan_8_3_0_14"
    version   = "8.3.0"
  }
  plan {
    name      = "silver_peak_edgeconnect_vwan_8_3_0_14"
    publisher = "silver-peak-systems"
    product   = "silver_peak_edgeconnect_vwan"
  }

  lifecycle {
    ignore_changes = [
      tags
    ]
  }
}

所以我根据位置部署了一个可用性集或可用性区域。从我的Angular 来看,这是完全有意义的,但我得到了我不明白的错误消息。我希望你们中的一些人能帮助我。它看起来像不知何故azurerm_availability_set.aset是空的,但它不应该基于计数参数中的条件。我希望你们中的一些人能帮助我。

│ Error: Invalid index
│ 
│   on modules/transit-gateway/vm.tf line 51, in resource "azurerm_linux_virtual_machine" "primary-vm":
│   51:   availability_set_id             = local.primary_zone != "1" ? azurerm_availability_set.aset[count.index].id : null
│     ├────────────────
│     │ azurerm_availability_set.aset is empty tuple
│     │ count.index is 0
│ 
│ The given key does not identify an element in this collection value: the
│ collection has no elements.
╵
╷
│ Error: Invalid index
│ 
│   on modules/transit-gateway/vm.tf line 97, in resource "azurerm_linux_virtual_machine" "secondary-vm":
│   97:   availability_set_id             = local.secondary_zone != "2" ? azurerm_availability_set.aset[count.index].id : null
│     ├────────────────
│     │ azurerm_availability_set.aset is empty tuple
│     │ count.index is 0
│ 
│ The given key does not identify an element in this collection value: the
│ collection has no elements.
╵
##[error]Error: Terraform Plan failed with exit code: 1
lx0bsm1f

lx0bsm1f1#

我发现了代码的错误。所以其他用其他或相同资源做类似事情的人都要仔细检查你的条件,仔细检查你代码的逻辑。
对于可用性集资源,我执行了以下操作:count = local.primary_zone != "1" ? 0 : 1
但我应该这样做:count = local.primary_zone == "1" ? 0 : 1
现在它起作用了!现在,当某个区域中的可用性区域不可用时,将创建一个可用性集并自动添加虚拟机。

相关问题