Terraform:在模块中使用 for_each

Jol*_*lly 2 azure terraform terraform-provider-azure

我正在使用 terraform 版本 0.14.3。我有一个用于创建 Azure 网络接口卡的模块,如下所示:

resource "azurerm_network_interface" "nic" {

  name                = var.nic_name
  location            = var.location
  resource_group_name = var.rg_name

  ip_configuration {
    name                          = var.ipconfig_name
    subnet_id                     = var.subnet_id
    private_ip_address_allocation = "Dynamic"
  }
}
Run Code Online (Sandbox Code Playgroud)

其输出定义为:

output "nic_id" {
     value = azurerm_network_interface.nic.id 
}
Run Code Online (Sandbox Code Playgroud)

我在这个父模块中调用这个模块:

module "NIC" {
  source = "./NIC"
  for_each = var.nics

  nic_name      = each.value.nic_name
  location      = "eastus2"
  rg_name       = "abc-test-rg"
  ipconfig_name = each.value.ipconfig_name
  subnet_id     = <subnet_id>
}

output "nic_ids" {
  value = [for k in module.NIC.nic_id : k.id]
} 
Run Code Online (Sandbox Code Playgroud)

NIC 值定义如下:

nics = {
  nic1 = {
    nic_name      = "abc-nic-1"
    ipconfig_name = "nic-1-ipconfig"
  }
}
Run Code Online (Sandbox Code Playgroud)

我想循环 NIC 输出 ID,并希望显示它们。当我运行上面的代码时,我在terraform plan中收到以下错误:

Error: Unsupported attribute

  on main.tf line 15, in output "nic_ids":
  15:   value = [for k in module.NIC.nic_id : k.id]
    |----------------
    | module.NIC is object with 1 attribute "nic1"

This object does not have an attribute named "nic_id".
Run Code Online (Sandbox Code Playgroud)

我该如何解决它?

Mar*_*ins 6

您的module "NIC"块已for_each设置,因此module.NIC模块中其他位置的符号是从实例键到输出对象的映射,而不仅仅是单例模块的​​单个输出对象。

Terraform 的错误消息试图通过以下消息引起人们的注意:

  • module.NIC is object with 1 attribute "nic1"

请注意,nic1这是来自您的键var.nics,而不是模块中定义的输出值之一。

假设nic_id您在此处显示的输出是该模块中定义的唯一输出,则该module.NIC值的形状将如下所示:

{
  nic1 = {
    nic_id = "eni-e5aa89a3"
  }
}
Run Code Online (Sandbox Code Playgroud)

听起来您想生成一个如下所示的值:

{
  nic1 = "eni-e5aa89a3"
}
Run Code Online (Sandbox Code Playgroud)

如果是这样,获得该结果的合适表达式如下:

output "nic_ids" {
  value = { for k, nic in module.NIC : k => nic.nic_id }
} 
Run Code Online (Sandbox Code Playgroud)

上面的意思是:为模块的每个实例生成一个包含一个元素的映射NIC,其键是模块实例键,其值是nic_id输出值。

或者,如果哪个 id 属于哪个实例并不重要,那么您可以生成一组无序的 id,如下所示:

output "nic_ids" {
  value = toset([for nic in module.NIC : nic.nic_id])
} 
Run Code Online (Sandbox Code Playgroud)

在这种情况下,for表达式仅定义一个局部符号nic,它表示模块实例对象,因为它不对实例键执行任何操作。这里toset表示 ID 不按任何特定顺序:这并不是严格必要的,但我认为这是一个很好的做法,以确保依赖于该值的任何其他 Terraform 代码不会无意中依赖于当前的任意顺序id 的值,如果您在var.nics.