Terraform 为每个模块引用来自a_的输出
我有一个模块,每个模块都定义了一个变量,其输出如下:Terraform 为每个模块引用来自a_的输出,terraform,terraform-provider-azure,Terraform,Terraform Provider Azure,我有一个模块,每个模块都定义了一个变量,其输出如下: output "nic_ids" { value = [for x in azurerm_network_interface.nic : x.id] } 我的目标是将上述NIC id传递到VM模块,并在NIC id和VM之间进行1:1映射(test-NIC-1应仅连接到VM-1,test-NIC-2应连接到VM-2等) 我得到以下错误: Error: each.value cannot be used in th
output "nic_ids" {
value = [for x in azurerm_network_interface.nic : x.id]
}
我的目标是将上述NIC id传递到VM模块,并在NIC id和VM之间进行1:1映射(test-NIC-1应仅连接到VM-1,test-NIC-2应连接到VM-2等)
我得到以下错误:
Error: each.value cannot be used in this context
on main.tf line 58, in module "vm":
58: nic_ids = module.nic[each.value.id].nic_ids
A reference to "each.value" has been used in a context in which it
unavailable, such as when the configuration no longer contains the value in
its "for_each" expression. Remove this reference to each.value in your
configuration to work around this error.
我用这个作为参考。
您可以建议吗?您可以使用
计数将上述NIC id列表传递给VM模块
module "vm" {
source = "./modules/vms"
vm_names = ["aaa","bbb"]
nic_ids = module.nic.network_interface_nics
}
module "nic" {
source = "./modules/nic"
nic_names = ["nic1","nic2"]
}
VM模块中的main.tf
:
resource "azurerm_virtual_machine" "vm-windows" {
count = length(var.vm_names)
name = var.vm_names[count.index]
resource_group_name = data.azurerm_resource_group.vm.name
location = var.location
vm_size = var.vm_size
network_interface_ids = [ var.nic_ids[count.index] ]
...
}
NIC模块中的输出.tf
:
output "network_interface_nics" {
description = "ids of the vm nics provisoned."
value = [for x in azurerm_network_interface.nic : x.id]
}
谢谢南希,是的,这是一种方法。我更喜欢使用for_each,以避免使用count时出现的索引问题。
resource "azurerm_virtual_machine" "vm-windows" {
count = length(var.vm_names)
name = var.vm_names[count.index]
resource_group_name = data.azurerm_resource_group.vm.name
location = var.location
vm_size = var.vm_size
network_interface_ids = [ var.nic_ids[count.index] ]
...
}
output "network_interface_nics" {
description = "ids of the vm nics provisoned."
value = [for x in azurerm_network_interface.nic : x.id]
}