简体   繁体   English

每个资源的 for_each 循环的输出

[英]Outputs from for_each loop for each resource

I am having a hard time figuring out how to make an output for each target group resource that this code creates.我很难弄清楚如何为这段代码创建的每个目标组资源输出。 I'd like to be able to reference each one individually in other modules.我希望能够在其他模块中单独引用每一个。 It sounds like for_each stores it as a map, so my question is how would I get the arn for targetgroup1 and targetgroup2?听起来 for_each 将它存储为地图,所以我的问题是如何获得 targetgroup1 和 targetgroup2 的 arn? Terraform normally refers to outputs by resource name, so I am struggling with that in this scenario and also how to refer to these individual arns. Terraform 通常按资源名称指代输出,因此在这种情况下我正在努力解决这个问题,以及如何引用这些单独的 arn。 Would I also need to work the outputs into the for_each or could I drop it into the output.tf file?我是否还需要将输出处理到 for_each 中,还是可以将其放入 output.tf 文件中?

locals {
  target_groups_beta = {
    targetgroup1 = {
      name = "example",
      path = "/",
      environment = "Beta"
    }
    targetgroup2 = {
      name = "example2",
      path = "/",
      environment = "Beta"
    }
    }
  }

resource "aws_lb_target_group" "target-group" {
  for_each = local.target_groups_beta
  name     = "example-${each.value.name}-"
  port     = 80
  protocol = "HTTP"
  vpc_id   = var.vpc-id
  deregistration_delay = 5

  tags = {
    Environment = "${each.value.environment}"
  }

  health_check{
  healthy_threshold = 2
  unhealthy_threshold = 2
  timeout = 10
  interval = 15
  path = each.value.path
  }
}

I receive the following error when trying to do it in the output.tf file without a key value, but when I input one such as value = "${aws_lb_target_group.target-group[0].arn}" it says it's invalid.我在没有键值的情况下尝试在 output.tf 文件中执行此操作时收到以下错误,但是当我输入诸如 value = "${aws_lb_target_group.target-group[0].arn}" 之类的值时,它说它无效。 Error without key value below:下面没有键值的错误:

Error: Missing resource instance key错误:缺少资源实例键

on modules\\targetgroups\\output.tf line 2, in output "tg_example_beta": 2: value = "${aws_lb_target_group.target-group.arn}"在 modules\\targetgroups\\output.tf 第 2 行,在输出“tg_example_beta”中:2:value = “${aws_lb_target_group.target-group.arn}”

Because aws_lb_target_group.target-group has "for_each" set, its attributes must be accessed on specific instances.由于 aws_lb_target_group.target-group 设置了“for_each”,因此必须在特定实例上访问其属性。

For example, to correlate with indices of a referring resource, use: aws_lb_target_group.target-group[each.key]例如,要与引用资源的索引相关联,请使用:aws_lb_target_group.target-group[each.key]

The aws_lb_target_group.target-group generated will be a map, with key values of targetgroup2 and targetgroup1 .生成的aws_lb_target_group.target-group将是一个映射,键值为targetgroup2targetgroup1

Therefore, to get the individual target group details you can do:因此,要获取单个目标组的详细信息,您可以执行以下操作:

output "target-group1-arn" {
  value = aws_lb_target_group.target-group["targetgroup1"].arn
}

To return both as a map:要将两者都作为地图返回:

output "target-groups-arn-alternatice" {
  value = {for k, v in aws_lb_target_group.target-group: k => v.arn}
}
target-groups-arn-alternatice = {
  "targetgroup1" = "arn:aws:elasticloadbalancing:us-east-1:xxxx:targetgroup/example-example/285b26e15221b113"
  "targetgroup2" = "arn:aws:elasticloadbalancing:us-east-1:xxxx:targetgroup/example-example2/075bd58359e4c4b2"
}

To return both as a list (order will be same as for keys function):将两者作为列表返回(顺序与keys功能相同):

output "target-groups-arn" {
  value = values(aws_lb_target_group.target-group)[*].arn
}
target-groups-arn = [
  "arn:aws:elasticloadbalancing:us-east-1:xxxx:targetgroup/example-example/285b26e15221b113",
  "arn:aws:elasticloadbalancing:us-east-1:xxxx:targetgroup/example-example2/075bd58359e4c4b2",
]

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

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