简体   繁体   中英

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

I have created multiple target groups using for_each for multiple microservice but in output how can take those all target groups arn?

output "new_target_group" {
  value = aws_alb_target_group.main[each.key].arn
}

I want all target groups arn in my output Kindly help me on it.

You can use a for expression to iterate through the object of your exported resource and construct a map with all of the target groups and their associated ARNs:

output "new_target_groups" {
  value = { for target_group in aws_alb_target_group.main : target_group.name => target_group.arn }
}

and the result will appear like:

new_target_groups = { "${each.key}-tg" = <target_group_arn> }

with a key-value pair for each target group.

If your goal is to get a list with all the ARNs, you can use the values function with the splat operator as such:

output "new_target_group" {
  value = values(aws_alb_target_group.test)[*].arn
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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