我们如何 "aws_alb_target_group.tenant[each.key].arn" 在 terraform 输出值中声明?
How we can "aws_alb_target_group.tenant[each.key].arn" declare in terraform output value?
resource "aws_alb_target_group" "test" {
for_each = var.microservices
name = "${each.key}-tg"
port = each.value
protocol = "HTTP"
vpc_id = var.vpc_id
target_type = "ip"
health_check {
healthy_threshold = "3"
interval = "30"
protocol = "HTTP"
matcher = "200"
timeout = "3"
path = "/"
unhealthy_threshold = "2"
}
tags = {
Name = "${each.key}-tg"
}
}
resource "aws_alb_listener" "test" {
for_each = var.microservices
load_balancer_arn = aws_lb.main.arn
port = "80"
protocol = "HTTP"
default_action {
target_group_arn = aws_alb_target_group.test[each.key].arn
type = "forward"
}
}
我已经使用 for_each 为多个微服务创建了多个目标组,但在输出中如何获取所有目标组?
output "new_target_group" {
value = aws_alb_target_group.main[each.key].arn
}
我希望我的输出中包含所有目标组 请帮助我。
您可以使用 for
表达式遍历导出资源的对象并构建包含所有目标组及其关联 ARN 的映射:
output "new_target_groups" {
value = { for target_group in aws_alb_target_group.main : target_group.name => target_group.arn }
}
结果将如下所示:
new_target_groups = { "${each.key}-tg" = <target_group_arn> }
每个目标组都有一个键值对。
如果您的目标是获得包含所有 ARN 的列表,您可以这样使用 values
function with the splat
运算符:
output "new_target_group" {
value = values(aws_alb_target_group.test)[*].arn
}
resource "aws_alb_target_group" "test" {
for_each = var.microservices
name = "${each.key}-tg"
port = each.value
protocol = "HTTP"
vpc_id = var.vpc_id
target_type = "ip"
health_check {
healthy_threshold = "3"
interval = "30"
protocol = "HTTP"
matcher = "200"
timeout = "3"
path = "/"
unhealthy_threshold = "2"
}
tags = {
Name = "${each.key}-tg"
}
}
resource "aws_alb_listener" "test" {
for_each = var.microservices
load_balancer_arn = aws_lb.main.arn
port = "80"
protocol = "HTTP"
default_action {
target_group_arn = aws_alb_target_group.test[each.key].arn
type = "forward"
}
}
我已经使用 for_each 为多个微服务创建了多个目标组,但在输出中如何获取所有目标组?
output "new_target_group" {
value = aws_alb_target_group.main[each.key].arn
}
我希望我的输出中包含所有目标组 请帮助我。
您可以使用 for
表达式遍历导出资源的对象并构建包含所有目标组及其关联 ARN 的映射:
output "new_target_groups" {
value = { for target_group in aws_alb_target_group.main : target_group.name => target_group.arn }
}
结果将如下所示:
new_target_groups = { "${each.key}-tg" = <target_group_arn> }
每个目标组都有一个键值对。
如果您的目标是获得包含所有 ARN 的列表,您可以这样使用 values
function with the splat
运算符:
output "new_target_group" {
value = values(aws_alb_target_group.test)[*].arn
}