Terraform - Join multi-count resources for ASG

Viewed 16

See structure of the terraform for context

################### terraform.hcl
  InstanceCFG = [
    {
      "Name" = "instance_0001"
      "Alias" = "cookie"
    },
    {
      "Name" = "instance_0002"
      "Alias" = "cake"
    },
    {
      "Name" = "instance_0003"
      "Alias" = "cupcake"
    },
    {
      "Name" = "instance_0004"
      "Alias" = "chocolate"
    },
    {
      "Name" = "instance_0005"
      "Alias" = "icecream"
    }
  ]

  NLBCFG = [
    {
      "Name"      = "8000-tg"
      "Port"      = "8000"
      "Protocol"  = "TCP"
    },
    {
      "Name"      = "9000-tg"
      "Port"      = "9000"
      "Protocol"  = "TCP"
    },
  ]
################### loadbalancer.tf
resource "aws_lb_target_group" "tg" {
    count    = length(var.NLBConfig)
    name     = "${var.NLBConfig[count.index]["Name"]}"
    port     = "${var.NLBConfig[count.index]["Port"]}"
    protocol = "${var.NLBConfig[count.index]["Protocol"]}"
    vpc_id   = var.VPCID
}

resource "aws_lb_listener" "lb_listener" {
    count               = length(var.NLBConfig)
    load_balancer_arn   = aws_lb.lb.arn
    port                = "${var.NLBConfig[count.index]["Port"]}"
    protocol            = "${var.NLBConfig[count.index]["Protocol"]}"

    default_action {
        type             = "forward"
        target_group_arn = aws_lb_target_group.tg[count.index].arn
    }
}

################### autoscalinggroup.tf
# AWS AutoScaling Group
resource "aws_autoscaling_group" "asg" {
    count                = length(var.ClientConfig)
    name                 = "${var.ClientConfig[count.index]["Name"]}_${local.name_postfix}"
    min_size             = 1
    max_size             = 1
    desired_capacity     = 1
    force_delete         = true
    termination_policies = [ OldestLaunchConfiguration ]
    health_check_type    = "EC2" # TODO: Change to ELB for Healthchecks
    health_check_grace_period = 300
    target_group_arns = [ aws_lb_target_group.tg.arn, aws_lb_target_group.tg_wss.arn ]  # < Issue HERE!
}

I am able to create two groups of resources separately, 5 ASGs and 3 Target Groups, these numbers will change constantly but I need to make sure X number of ASG instances can join X number of target groups independently of one another, is this even possible?

1 Answers

I find the answer as follow: Note I am using two count statements in two different resources.

################### loadbalancer.tf

# Consolidate Target Groups ARNs for ASG
data "aws_lb_target_group" "data_tg" {
    depends_on = [ aws_lb_target_group.tg ]
    count = length(var.NLBCFG)
    arn  = aws_lb_target_group.tg[count.index].arn
}

################### autoscalinggroup.tf

# AWS AutoScaling Group
resource "aws_autoscaling_group" "asg" {
    count                = length(var.ClientConfig)
    name                 = "${var.ClientConfig[count.index]["Name"]}_${local.name_postfix}"
    min_size             = 1
    max_size             = 1
    desired_capacity     = 1
    force_delete         = true
    termination_policies = [ OldestLaunchConfiguration ]
    health_check_type    = "EC2" # TODO: Change to ELB for Healthchecks
    health_check_grace_period = 300
    target_group_arns = [ data.aws_lb_target_group.data_tg[*].arn ]  # < Issue HERE!
}
Related