Skip to content

Misleading output ordering for target_group_name and target_group_arn #72

@gcool-info

Description

@gcool-info

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions