3

I'm trying to create na NLB using Terraform v0.11.0 (my application doesn't use HTTP, so I cannot use an ALB). Looking in the Hashcorp documentation, I could create the following code:

resource "aws_lb" "lb" {
  name = "test"
  internal = false
  enable_deletion_protection = true
  load_balancer_type = "network"
  ip_address_type = "ipv4"
  subnet_mapping {
    subnet_id = "${data.aws_subnet.sn-app-1.id}"
    allocation_id = "${aws_eip.eip-1.id}"
  }
  subnet_mapping {
    subnet_id = "${data.aws_subnet.sn-app-2.id}"
    allocation_id = "${aws_eip.eip-2.id}"
  }
}

resource "aws_lb_target_group" "lbtg" {
  name     = "test"
  port     = "8080"
  protocol = "TCP"
  vpc_id   = "${data.aws_vpc.vpc.id}"
  deregistration_delay = "300"
  health_check {
    interval = "300"
    port = "8080"
    protocol = "TCP"
    timeout = "10"
    healthy_threshold = "10" 
    unhealthy_threshold= "10" 
  }
}

resource "aws_lb_listener" "front_end" {
  load_balancer_arn = "${aws_lb.lb.arn}"
  port              = "8080"
  protocol          = "TCP"
  default_action {
    target_group_arn = "${aws_lb_target_group.lbtg.arn}"
    type             = "forward"
  }
}

resource "aws_autoscaling_group" "asg" {
  name  = "test"
  vpc_zone_identifier = ["${data.aws_subnet.sn-app-1.id}","${data.aws_subnet.sn-app-2.id}"]
  min_size  = 1
  desired_capacity  = 1
  max_size  = 3
  launch_configuration = "${aws_launch_configuration.lc.name}"
  load_balancers  = ["${aws_lb.lb.name}"]
  default_cooldown= 180
  health_check_grace_period = 180
  termination_policies = ["ClosestToNextInstanceHour", "NewestInstance"]
}

I run terraform init and terraform plan -out=plan.json and everything goes well, but after run terraform apply plan.json, Terraform spend some time trying to create the AutoScaling Group and throws something like this:

aws_ecs_service.ecss: 1 error(s) occurred:

  • aws_ecs_service.ecss: InvalidParameterException: The target group with targetGroupArn arn:aws:elasticloadbalancing:us-east-1:xxxxxx:targetgroup/test/xxxxxx does not have an associated load balancer. status code: 400, request id: b2565334-da9a-11e7-ab5a-8f0bfc9ecd99 "test"

  • aws_autoscaling_group.asg: 1 error(s) occurred:

  • aws_autoscaling_group.asg: Error creating AutoScaling Group: ValidationError: Provided Load Balancers may not be valid. Please ensure they exist and try again. status code: 400, request id: cf2d4ac6-da9a-11e7-950f-050f1f0711f8

How can I associate a target group wth an LB? And why provided Load Balancer may not be valid for the AutoScaling Group?

Tales Morais
  • 33
  • 1
  • 3

1 Answers1

5

Try using the target_group_arns option on the ASG.

resource "aws_autoscaling_group" "asg" {
    name  = "test"
    vpc_zone_identifier = ["${data.aws_subnet.sn-app-1.id}","${data.aws_subnet.sn-app-2.id}"]
    min_size  = 1
    desired_capacity  = 1
    max_size  = 3
    launch_configuration = "${aws_launch_configuration.lc.name}"
    target_group_arns = ["${aws_lb_target_group.lbtg.arn}"]
    default_cooldown= 180
    health_check_grace_period = 180
    termination_policies = ["ClosestToNextInstanceHour", "NewestInstance"]
}
Matt Cooper
  • 166
  • 3
  • Thanks a lot! It solves the problem and now I am in another one. But I will try something here first. Thanks again! – Tales Morais Dec 07 '17 at 19:24