skip to Main Content

I get the following error at the end of the output.

Error: Error registering targets with target group: ValidationError: Instance ID ‘arn:aws:elasticloadbalancing:us-west-2:993276023740:targetgroup/ilab-app-loadbalancer/52c7cb8bc3d39e61’ is not valid
status code: 400, request id: abedd3c2-d905-4af4-9a32-c1e30a9e4e1d

with aws_lb_target_group_attachment.ilab_tg_attach,
on resilience.tf line 48, in resource "aws_lb_target_group_attachment" "ilab_tg_attach":
48: resource "aws_lb_target_group_attachment" "ilab_tg_attach" {

Here is the code I am using in terraform:
Note: Funny thing is that it creates the targetgroup attachments and it works, it adds two hosts.

resource "aws_lb_target_group_attachment" "ilab_tg_attach" {
  target_id        = aws_lb_target_group.ilab_alb.id
  target_group_arn = aws_lb_target_group.ilab_alb.arn
  port             = 80
}

resource "aws_autoscaling_group" "ilab_asg" {
  name                      = "ilab_asg"
  vpc_zone_identifier       = ["${aws_subnet.ilab_subnet_pub_a.id}", "${aws_subnet.ilab_subnet_pub_b.id}"]
  launch_configuration      = aws_launch_configuration.ilab_lt.name
  min_size                  = 2
  max_size                  = 2
  health_check_grace_period = 300
  health_check_type         = "ELB"
  force_delete              = true
  target_group_arns         = [aws_lb_target_group.ilab_alb.arn]
  tag {
    key                 = "Name"
    value               = "EC2 Instance"
    propagate_at_launch = true
  }
}

2

Answers


  1. Chosen as BEST ANSWER

    Thanks all for the help. For anyone else trying to get this working properly here is the full code for my alb/asg.

    #ALB,ASG,LT design = always up scenario, single server
    #ALB 
    resource "aws_lb_target_group" "ilab_alb" {
      name        = "ilab-app-loadbalancer"
      port        = 80
      protocol    = "HTTP"
      target_type = "instance"
      vpc_id      = aws_vpc.imaginelabtf.id
      health_check {
        interval            = 10
        path                = "/"
        protocol            = "HTTP"
        timeout             = 5
        healthy_threshold   = 5
        unhealthy_threshold = 2
      }
    }
    resource "aws_lb" "ilab_lb" {
      name               = "ilab-lb"
      internal           = false
      load_balancer_type = "application"
      security_groups = [
        "${aws_security_group.albonly.id}",
      ]
      subnets = ["${aws_subnet.ilab_subnet_pub_a.id}", "${aws_subnet.ilab_subnet_pub_b.id}"]
      tags = {
        name = "ilab-AppLoadBalancer"
      }
    }
    
    #Launch template
    resource "aws_launch_configuration" "ilab_lt" {
      name_prefix     = "ilab_launch_template"
      image_id        = data.aws_ami.server_ami.id #might be wrong
      instance_type   = "t2.micro"
      key_name        = "imagine-key"
      security_groups = ["${aws_security_group.allow_http_ssh.id}"]
      user_data       = file("userdata.tpl")
    
    }
    resource "aws_lb_listener" "ilab_lb_listener" {
      load_balancer_arn = aws_lb.ilab_lb.arn
      port              = 80
      protocol          = "HTTP"
    
      default_action {
        type             = "forward"
        target_group_arn = aws_lb_target_group.ilab_alb.arn
      }
    
    
    }
    
    resource "aws_autoscaling_group" "ilab_asg" {
      name                      = "ilab_asg"
      vpc_zone_identifier       = ["${aws_subnet.ilab_subnet_pub_a.id}", "${aws_subnet.ilab_subnet_pub_b.id}"]
      launch_configuration      = aws_launch_configuration.ilab_lt.name
      min_size                  = 2
      max_size                  = 2
      health_check_grace_period = 300
      health_check_type         = "ELB"
      force_delete              = true
      target_group_arns         = [aws_lb_target_group.ilab_alb.arn]
      tag {
        key                 = "Name"
        value               = "Ilab LB Instance"
        propagate_at_launch = true
      }
    }
    
    /*resource "aws_autoscaling_attachment" "ilab_asg_attachment" {
      autoscaling_group_name = aws_autoscaling_group.ilab_asg.id
      alb_target_group_arn   = aws_lb.ilab_lb.arn
    }*/
    #ASG Policies 
    resource "aws_autoscaling_policy" "ilab_cpu_policy_up" {
      name                   = "ilab_cpu_policy_scale_up"
      autoscaling_group_name = aws_autoscaling_group.ilab_asg.name
      adjustment_type        = "ChangeInCapacity"
      scaling_adjustment     = 1
      cooldown               = "300"
      policy_type            = "SimpleScaling"
    }
    
    resource "aws_autoscaling_policy" "ilab_cpu_policy_down" {
      name                   = "ilab_cpu_policy_scale_down"
      autoscaling_group_name = aws_autoscaling_group.ilab_asg.name
      adjustment_type        = "ChangeInCapacity"
      scaling_adjustment     = -1
      cooldown               = 300
      policy_type            = "SimpleScaling"
    }
    
    #cloudwatch alarms
    resource "aws_cloudwatch_metric_alarm" "ilab_cla_alarm" {
      alarm_name          = "ilab_alarm_cpu"
      alarm_description   = "Alarm When CPU is higher than 80%"
      comparison_operator = "GreaterThanOrEqualToThreshold"
      evaluation_periods  = "2"
      metric_name         = "CPUUtilization"
      namespace           = "AWS/EC2"
      period              = "120"
      statistic           = "Average"
      threshold           = "80"
    
      dimensions = {
        "AutoScalingGroupName" = "${aws_autoscaling_group.ilab_asg.name}"
      }
      actions_enabled = true
      alarm_actions   = ["${aws_autoscaling_policy.ilab_cpu_policy_up.arn}"]
    }
    resource "aws_cloudwatch_metric_alarm" "ilab_cla_normal" {
      alarm_name          = "ilab_normal_cpu"
      alarm_description   = "Alarm When CPU has dropped to 30%"
      comparison_operator = "GreaterThanOrEqualToThreshold"
      evaluation_periods  = "2"
      metric_name         = "CPUUtilization"
      namespace           = "AWS/EC2"
      period              = "120"
      statistic           = "Average"
      threshold           = "30"
    
      dimensions = {
        "AutoScalingGroupName" = "${aws_autoscaling_group.ilab_asg.name}"
      }
      actions_enabled = true
      alarm_actions   = ["${aws_autoscaling_policy.ilab_cpu_policy_down.arn}"]
    }
    
    #sns topics
    
    resource "aws_sns_topic" "ilab_sns" {
      name         = "ilab_sns_cpu_alarm"
      display_name = "ILab CPU Alarm on ASG Instnace"
    } #email subscription must be done in AWS console as it is not available in terraform.
    
    resource "aws_autoscaling_notification" "ilab_asg_notice" {
      group_names = ["${aws_autoscaling_group.ilab_asg.name}"]
      topic_arn   = aws_sns_topic.ilab_sns.arn
      notifications = [
        "autoscaling:EC2_INSTANCE_LAUNCH",
        "autoscaling:EC2_INSTANCE_TERMINATE",
        "autoscaling:EC2_INSTANCE_LAUNCH_ERROR",
        "autoscaling:EC2_INSTANCE_TERMINATE_ERROR",
      ]
    }
    

  2. You get a validation error because target_id should be the ID of the target your are trying to register, not the target group itself.

    From the terraform documentation:

    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 the target type is alb, specify the arn of alb.

    The reason why it registers two targets is that the autoscaling group actually tries to do its job by creating two EC2 instances (min_size = 2). Since you are using an ASG, you probably would want to get rid of the aws_lb_target_group_attachment all together.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search