【问题标题】:How to use for_each with iterator in dynamic block in Terraform?如何在 Terraform 的动态块中使用 for_each 和迭代器?
【发布时间】:2020-07-30 19:38:38
【问题描述】:

我基本上是在尝试这样做:

module "california" {
  source = "./themodule"
  # ...
}

module "oregon" {
  source = "./themodule"
  # ...
}

resource "aws_globalaccelerator_endpoint_group" "world" {
  # ...
  dynamic "endpoint_configuration" {
    for_each = [
      module.california.lb,
      module.oregon.lb
    ]
    iterator = lb
    content {
      endpoint_id = lb.arn
      weight = 100
    }
  }
}

# themodule/main.tf
resource "aws_lb" "lb" {
  # ...
}

output "lb" {
  value = aws_lb.lb
}

我正在从 Terraform 中的子模块输出 lb,并尝试在 for_each 数组中的父模块中使用它,并使用自定义的 iterator 名称。它给了我这个错误:

This object does not have an attribute named "arn".

但它确实具有该属性,它是aws_lb。我在使用这个for_each 和模块设置时做错了什么,我该如何解决?非常感谢!

如果我把它改成这个,它似乎可以工作:

resource "aws_globalaccelerator_endpoint_group" "world" {
  listener_arn = aws_globalaccelerator_listener.world.id

  endpoint_configuration {
    endpoint_id = module.california.lb.arn
    weight = 100
  }
}

【问题讨论】:

  • 迭代器 lb 将只有键和值属性。因此,也许您必须使用lb.value["arn"]

标签: amazon-web-services module iterator terraform


【解决方案1】:

来自docs

迭代器对象(上例中的设置)有两个属性

key 是当前元素的映射键或列表元素索引。如果 for_each 表达式产生一个设置值,则 key 与 value 相同,不应使用。

value是当前元素的值。

基于此,在content 中,您应该按照example 使用lb.value["arn"]。因此,可以尝试以下方法:

    content {
      endpoint_id = lb.value["arn"]
      weight = 100
    }

【讨论】:

    猜你喜欢
    • 2020-12-27
    • 2021-04-14
    • 2021-12-08
    • 1970-01-01
    • 2023-01-26
    • 2021-09-24
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多