Terraform-provider-aws: aws_alb_target_group_attachment does not support list for target_id

Created on 13 Jun 2017  路  3Comments  路  Source: hashicorp/terraform-provider-aws

_This issue was originally opened by @plotniku as hashicorp/terraform#15247. It was migrated here as part of the provider split. The original body of the issue is below._


Hi there,

I have added two VMs with aws_instance and used the "count" attribute. Also i would like to add future two new VMs to Target Group, so i need to specify a list of VMs for "target_id", however resources done with count do not exist at the plan execution. terraform show error

Terraform Version

[root@ip-100-79-13-128 2_vpc]# terraform version
Terraform v0.9.8

Affected Resource(s)

  • aws_alb_target_group_attachment

Terraform Configuration Files

resource "aws_instance" "web" {
  count         = "2"
  ami           = "${var.ec2["ami"]}"
  instance_type = "${var.ec2["instance_type"]}"
  key_name      = "${var.ec2["key"]}"
  subnet_id     = "${element(aws_subnet.private-subnets.*.id, count.index)}"
  vpc_security_group_ids = ["${aws_security_group.private.id}"]
  user_data     = "${file("script.sh")}"
  tags  {
        Name          = "${var.general["owner"]}-web"
        configurator  = "${var.vpc["tag"]}"
  }
  depends_on    = ["aws_nat_gateway.main"]
}

resource "aws_alb" "app" {
  name            = "${var.general["owner"]}-elb-app"
  internal        = false
  security_groups = ["${aws_security_group.elb.id}"]
  subnets         = ["${aws_subnet.public-subnets.*.id}"]

  enable_deletion_protection = false

  tags {
    Name           = "${var.general["owner"]}-elb-app"
    configurator   = "terraform"
  }
}

resource "aws_alb_target_group" "app_tg_0" {
  name     = "${var.general["owner"]}-app-tg-0"
  port     = 80
  protocol = "HTTP"
  vpc_id   = "${aws_vpc.main.id}"

  health_check {
    healthy_threshold   = 2
    unhealthy_threshold = 3
    timeout             = 5
    path                = "${var.app["elb_hc_uri"]}"
    interval            = 10
    port                = "${var.app["listen_port_http"]}"
  }

  tags {
    Name           = "${var.general["owner"]}-app-tg-0"
    configurator   = "terraform"
  }
}

resource "aws_alb_target_group" "app_tg_1" {
  name     = "${var.general["owner"]}-app-tg-1"
  port     = 80
  protocol = "HTTP"
  vpc_id   = "${aws_vpc.main.id}"

  health_check {
    healthy_threshold   = 2
    unhealthy_threshold = 3
    timeout             = 5
    path                = "${var.app["elb_hc_uri"]}"
    interval            = 10
    port                = "${var.app["listen_port_http"]}"
  }

  tags {
    Name           = "${var.general["owner"]}-app-tg-1"
    configurator   = "terraform"
  }

}

resource "aws_alb_listener" "app_listener_0" {
  load_balancer_arn = "${aws_alb.app.arn}"
  port              = "80"
  protocol          = "HTTP"

  default_action {
    target_group_arn = "${aws_alb_target_group.app_tg_0.arn}"
    type             = "forward"
  }
}

resource "aws_alb_target_group_attachment" "app_tg_att_0" {
  target_group_arn = "${aws_alb_target_group.app_tg_0.arn}"
  target_id        = "${aws_instance.web.0.id}"
  port             = 80
}

resource "aws_alb_target_group_attachment" "app_tg_att_1" {
  target_group_arn = "${aws_alb_target_group.app_tg_1.arn}"
  target_id        = "${aws_instance.web.1.id}"
  port             = 80
}

resource "aws_alb_listener_rule" "static" {
  listener_arn = "${aws_alb_listener.app_listener_0.arn}"
  priority     = 100

  action {
    type             = "forward"
    target_group_arn = "${aws_alb_target_group.app_tg_1.arn}"
  }

  condition {
    field  = "path-pattern"
    values = ["/static/*"]
  }

}

Debug Output

[root@ip-100-79-13-128 2_vpc]# terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

Error running plan: 1 error(s) occurred:

  • aws_alb_target_group_attachment.app_tg_att_1: 1 error(s) occurred:

  • aws_alb_target_group_attachment.app_tg_att_1: Resource 'aws_instance.web' not found for variable 'aws_instance.web.1.id'

Expected Behavior

aws_alb_target_group_attachment should accept

Actual Behavior

Terraform does not want to apply

Steps to Reproduce

use the above piece of config (i have not added the configuration of the VPC as that part works fine)

Important Factoids

Nope, AWS account.

enhancement servicelbv2 terraform-0.12

Most helpful comment

Experiencing this same issue. Any update on this?

All 3 comments

Hello,

I too have run into this problem and just wondered if there has been any planned movement on this issue?

I could be wrong, but is it not a simple fix of changing the accepted input type for "target_id" from string to a list?

I have checked lots of other issues and discussions in this area, but see no workaround for this other than to use WET code and defy the DRY policy:

resource "aws_lb_target_group_attachment" "attach_http_tg_target1" {
  count = "${var.lb_http_listener ? "${length(split(",", var.target_ids)) >= 1 ? "${length(var.http_target_group_names) == "${length(var.http_target_group_ports)}" ? "${length(var.http_target_group_names)}" : 0}" :0}" :0}"
  target_group_arn = "${element(aws_lb_target_group.tg_http.*.arn, count.index)}"
  target_id        = "${element(split(",", var.target_ids), 0)}"
  port             = "${element(var.http_target_group_ports, count.index)}"
}
resource "aws_lb_target_group_attachment" "attach_http_tg_target2" {
  count = "${var.lb_http_listener ? "${length(split(",", var.target_ids)) >= 2 ? "${length(var.http_target_group_names) == "${length(var.http_target_group_ports)}" ? "${length(var.http_target_group_names)}" : 0}" :0}" :0}"
  target_group_arn = "${element(aws_lb_target_group.tg_http.*.arn, count.index)}"
  target_id        = "${element(split(",", var.target_ids), 1)}"
  port             = "${element(var.http_target_group_ports, count.index)}"
}
resource "aws_lb_target_group_attachment" "attach_http_tg_target3" {
  count = "${var.lb_http_listener ? "${length(split(",", var.target_ids)) >= 3 ? "${length(var.http_target_group_names) == "${length(var.http_target_group_ports)}" ? "${length(var.http_target_group_names)}" : 0}" :0}" :0}"
  target_group_arn = "${element(aws_lb_target_group.tg_http.*.arn, count.index)}"
  target_id        = "${element(split(",", var.target_ids), 2)}"
  port             = "${element(var.http_target_group_ports, count.index)}"
}

not such an elegant solution 馃榾

Experiencing this same issue. Any update on this?

Was this page helpful?
0 / 5 - 0 ratings