我想通过调用一个自写模块来部署一堆资源:
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时,才会部署辅助虚拟机。相同的逻辑以某种方式应用于可用性集,但这取决于该区域是否支持Avail Zones。如果没有,则应部署Avail Set,并将两个vm分配给此Avail Set。
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
]
}
}
因此,基于位置,我部署可用性集或可用性区域。从我的角度来看,这是绝对有意义的,但我得到我不理解的错误信息。我希望你们中的一些人能帮助我。看起来azurerm_availability_set.aset
是空的,但是根据count参数中的条件,它不应该是空的。我希望你们中的一些人能帮助我。
│ 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
我发现了代码的问题所在。因此,每个使用其他或相同资源做类似事情的人都要仔细检查你的条件,并仔细检查你的代码逻辑。
对于可用性集资源,我这样做:count = local.primary_zone != "1" ? 0 : 1
,但我应该这样做:count = local.primary_zone == "1" ? 0 : 1
,现在它工作了!现在创建可用集,当某个区域内可用分区不可用时自动添加虚拟机。