简体   繁体   English

Terraform:在模块中使用 for_each

[英]Terraform : Using for_each in module

I am using terraform version 0.14.3.我正在使用 terraform 版本 0.14.3。 I have a module for creating an Azure Network Interface Card, as below:我有一个用于创建 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"
  }
}

Its output is defined as:其output定义为:

output "nic_id" {
     value = azurerm_network_interface.nic.id 
}

I am calling this module in this parent module:我在这个父模块中调用这个模块:

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]
} 

The NIC values are defined as below: NIC 值定义如下:

nics = {
  nic1 = {
    nic_name      = "abc-nic-1"
    ipconfig_name = "nic-1-ipconfig"
  }
}

I want to loop around the NIC output IDs, and want them displayed.我想循环 NIC output ID,并希望它们显示。 When I run above code, I get below error in terraform plan :当我运行上面的代码时,在terraform 计划中出现以下错误:

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".

How do I get around it?我该如何解决?

Your module "NIC" block has for_each set, and so the module.NIC symbol elsewhere in the module is a mapping from instance keys to output objects, rather than just a single output object as for a singleton module. Your module "NIC" block has for_each set, and so the module.NIC symbol elsewhere in the module is a mapping from instance keys to output objects, rather than just a single output object as for a singleton module.

Terraform's error message is attempting to draw attention to that with the following message: Terraform 的错误消息试图通过以下消息引起人们的注意:

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

Notice that nic1 here is a key from your var.nics , and not one of the output values defined in your module.请注意,此处的nic1是来自您的var.nics的键,而不是模块中定义的 output 值之一。

Assuming that the nic_id output you showed here is the only one defined in that module, the module.NIC value would be shaped something like this:假设您在此处显示的nic_id output 是该模块中唯一定义的,那么module.NIC值的形状将如下所示:

{
  nic1 = {
    nic_id = "eni-e5aa89a3"
  }
}

It sounds like you instead want to produce a value shaped like this:听起来你想产生一个像这样的值:

{
  nic1 = "eni-e5aa89a3"
}

If so, a suitable expression to get that result would be the following:如果是这样,获得该结果的合适表达式如下:

output "nic_ids" {
  value = { for k, nic in module.NIC : k => nic.nic_id }
} 

The above means: produce a mapping with one element for each instance of the NIC module, whose key is the module instance key and whose value is the nic_id output value.上面的意思是:为NIC模块的每个实例生成一个映射,其键为模块实例键,其值为nic_id output 值。

Alternatively, if it doesn't matter which id belongs to which instance then you could produce an unordered set of ids, like this:或者,如果哪个 id 属于哪个实例并不重要,那么您可以生成一组无序的 id,如下所示:

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

In this case the for expression only defines a local symbol nic , which represents the module instance object, because it doesn't do anything with the instance key.在这种情况下, for表达式只定义了一个本地符号nic ,它表示模块实例 object,因为它对实例键没有任何作用。 The toset here is to represent that the IDs are not in any particular order: that isn't strictly necessary but I think it's a good practice to make sure that any other Terraform code depending on that value doesn't inadvertently depend on the current arbitrary ordering of the ids, which might change in future if you add or remove elements in var.nics .这里的toset表示 ID 没有任何特定的顺序:这不是绝对必要的,但我认为确保任何其他 Terraform 代码取决于该值不会无意中依赖于当前任意id 的顺序,如果您在var.nics中添加或删除元素,将来可能会改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM