Dhaval Patel
Dhaval Patel

Reputation: 131

Not able to attach Target id(launch configurtion) to the target group of network load balancer in Terraform

I am configuring a Terraform script for creating HA rabbitmq setup with auto scaling groups for all the instances. For creating auto scaling group I am creating launch configuration which will create auto scalable instance.

I want to use a network load balancer in front of this instance so I have to create target group and target group attachments. Here I have to provide target_id(launch configuration id) to attach it with target group. But when applying script it shows the following error:

Error registering targets with target group: ValidationError: Instance ID 'rabbit' is not valid status code: 400, request id: 1cad37a8-b1da-416a-bc11-f50ae6b83cd2

Terraform script

resource "aws_lb" "rabbit" {
  name = "${local.cluster_name}-lb"
  load_balancer_type = "network"
  internal = "false"
  subnets = aws_subnet.subnet.*.id
  enable_cross_zone_load_balancing = "true"
  tags = {
    Name = "${local.cluster_name}-lb"
  }
}

resource "aws_lb_listener" "http" {
    load_balancer_arn = aws_lb.rabbit.arn
    protocol = "TCP"
    port = "80"

    default_action {
        type = "forward"
        target_group_arn = aws_lb_target_group.TCP80.arn
    }
}

resource "aws_lb_target_group" "TCP80" {
  name = "${local.cluster_name}-TCP80"
  vpc_id = aws_vpc.vpc.id
  target_type = "instance"

  protocol = "TCP"
  port = "80"

  health_check {
    protocol = "TCP"
    port     = 80

    # NLBs required to use same healthy and unhealthy thresholds
    healthy_threshold   = 3
    unhealthy_threshold = 3

    # Interval between health checks required to be 10 or 30
    interval = 10
  }
}

resource "aws_lb_target_group_attachment" "TCP80" {
  count = var.controller_count
  target_group_arn = aws_lb_target_group.TCP80.arn
  target_id        = aws_launch_configuration.rabbit.id
  port             = 80
}

resource "aws_launch_configuration" "rabbit" {
  name = "rabbit"
  image_id    = data.aws_ami.ubuntu.id
  instance_type = var.instance_type
  key_name    = "key_name"

  security_groups = [
      aws_security_group.rabbit-nodes.id,
  ]
}

resource "aws_autoscaling_group" "rabbit-node" {
  #name = "${var.name}-${var.environment_tag}-"
  name ="rabbit"
  #count = var.instance_count
  launch_configuration = aws_launch_configuration.rabbit.name
  vpc_zone_identifier  = aws_subnet.subnet.*.id
  min_size             = var.min_size
  max_size             = var.max_size
  desired_capacity     = var.desired_size
  termination_policies = ["OldestLaunchConfiguration", "Default"]
  #load_balancers       = ["${aws_lb.rabbit}"]

  health_check_type         = "EC2"
  health_check_grace_period = 300

  lifecycle {
    create_before_destroy = true
  }
}

Upvotes: 0

Views: 4325

Answers (1)

ydaetskcoR
ydaetskcoR

Reputation: 56987

The aws_lb_target_group_attachment resource is for connecting existing EC2 instances, ECS tasks or Lambda functions to a load balancer target group:

target_id (Required) The ID of the target. This is the Instance ID for an instance, or the container ID for an ECS container. If the target type is ip, specify an IP address. If the target type is lambda, specify the arn of lambda.

If you want to have all your instances in an autoscaling group be automatically attached to a target group then you can specify this with the target_group_arns parameter on the aws_autoscaling_group resource:

resource "aws_autoscaling_group" "rabbit-node" {
  #name = "${var.name}-${var.environment_tag}-"
  name ="rabbit"
  #count = var.instance_count
  launch_configuration = aws_launch_configuration.rabbit.name
  vpc_zone_identifier  = aws_subnet.subnet.*.id
  min_size             = var.min_size
  max_size             = var.max_size
  desired_capacity     = var.desired_size
  termination_policies = ["OldestLaunchConfiguration", "Default"]

  target_group_arns    = [aws_lb_target_group.TCP80.arn]

  health_check_type         = "EC2"
  health_check_grace_period = 300

  lifecycle {
    create_before_destroy = true
  }
}

Upvotes: 3

Related Questions