Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading output ordering for target_group_name and target_group_arn #72

Open
gcool-info opened this issue May 17, 2023 · 0 comments
Open

Comments

@gcool-info
Copy link

Suppose we have the following setup:

resource "aws_lb_listener" "alb_80" {
  load_balancer_arn = module.alb.arn
  port              = "80"
  protocol          = "HTTP"

  default_action {
    type             = "forward"
    target_group_arn = module.fargate.target_group_arn[0]
  }
}

resource "aws_lb_listener" "alb_81" {
  load_balancer_arn = module.alb.arn
  port              = "81"
  protocol          = "HTTP"

  default_action {
    type             = "forward"
    target_group_arn = module.fargate.target_group_arn[1]
  }
}

module "fargate" {
  source = "../../"

  name_prefix = "ecs-fargate-example"
  ...

  target_groups = [
    {
      target_group_name = "B"
      container_port    = 80
    },
    {
      target_group_name = "A"
      container_port    = 81
    }
  ]

  ...
}

The above code reads as though:

  • aws_lb_listener.alb_80 will map to target_group_name = "B"
  • aws_lb_listener.alb_81 will map to target_group_name = "A"

However, I think the opposite actually happens. The module uses the following code for outputs:

output "target_group_arn" {
  description = "The ARN of the Target Group used by Load Balancer."
  value       = [for tg_arn in aws_lb_target_group.task : tg_arn.arn]
}

The for expression can modify the ordering of the input target_groups list. From terraform's documentation:

Because for expressions can convert from unordered types (maps, objects, sets) to ordered types (lists, tuples), Terraform must > choose an implied ordering for the elements of an unordered collection.

For maps and objects, Terraform sorts the elements by key or attribute name, using lexical sorting.

Can you confirm whether my understanding of the problem is correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant