Terraform: aws_alb_target_group_attachment doesn't register more than one target_id

Created on 8 Nov 2016  ยท  9Comments  ยท  Source: hashicorp/terraform

Hi there,

It seems that the aws_alb_target_group_attachment resource can only provision a single target, whereas one would expect to be able to define multiple targets.

Terraform Version

terraform -v
Terraform v0.7.9

Affected Resource(s)

Please list the resources as a list, for example:

  • aws_alb_target_group_attachment
  • aws_alb_target_group

Terraform Configuration Files

Below is the complete definition for an AWS Application Load Balancer, although the specific issue relates to the aws_alb_target_group_attachment resource

resource "aws_alb" "XXXX-alb-private-YYYY" {
  name            = "XXXX-alb-private-YYYY"
  internal        = true
  security_groups = ["${aws_security_group.XXXX-sg-alb-private.id}"]
  subnets         = ["${aws_subnet.XXXX-subnet-private-app-1a.id}", "${aws_subnet.XXXX-subnet-private-app-1b.id}"]
  enable_deletion_protection = false
}

resource "aws_alb_listener" "XXXX-alb-listener-private-YYYY" {
  load_balancer_arn = "${aws_alb.XXXX-alb-private-YYYY.arn}"
  port = "80"
  protocol = "HTTP"

  default_action {
    target_group_arn = "${aws_alb_target_group.XXXX-targetgroup-app-YYYY-ZZZZ.arn}"
    type = "forward"
  }
}

resource "aws_alb_target_group" "XXXX-targetgroup-app-YYYY-ZZZZ" {
  name     = "XXXX-targetgroup-app-YYYY-ZZZZ"
  port     = 80
  protocol = "HTTP"
  vpc_id   = "${var.XXXX-dev-vpc}"
}

resource "aws_alb_target_group_attachment" "XXXX-targetgroupattach-app-YYYY-ZZZZ" {
  target_group_arn = "${aws_alb_target_group.XXXX-targetgroup-app-YYYY-ZZZZ.arn}"
  # define multiple targets like this?
  target_id = "${var.XXXX-appserver1}"
  target_id = "${var.XXXX-appserver2}"
  # or as a list?
  # target_id = ["${var.XXXX-appserver1}", "${var.XXXX-appserver2}"]

  port = ZZZZ
}

Expected Behavior

I would expect to be able to define more than one target_id, so that the target group is a group, and not just a single instance. I would guess that target_id should be a list, or allow more than one target_id entry, eg target_id = ["${var.appserver1}", "${var.appserver2}"]

Actual Behavior

Only a single target instance is provisioned.

Steps to Reproduce

Note different results depending on whether multiple target_id entries are used, or whether a list is given. Using a list gives the error below. Using multiple entries causes no errors, but only the first entry is registered, while the 2nd is ignored.

  1. terraform plan
    ```There are warnings and/or errors related to your configuration. Please
    fix these before continuing.

Errors:

  • aws_alb_target_group_attachment.XXXX-targetgroupattach-app-YYYY-ZZZZ: target_id must be a single value, not a list
    ```

Important Factoids

None

References

None

bug provideaws

Most helpful comment

@kwilczynski thanks for the response! If I can add my 2c, intuitively it feels that a list parameters make most sense (this is also what the underlying API expects.

So something like target_id = ["${var.XXXX-appserver1}", "${var.XXXX-appserver2}"]

All 9 comments

@cornevandyk hi there! I am sorry you are having issues and thank you for the time you took to let us know.

The underlying API definitely allows for more than one target to be associated, thus we need to most likely add a support for multiple target_ids (with optional port).

@kwilczynski thanks for the response! If I can add my 2c, intuitively it feels that a list parameters make most sense (this is also what the underlying API expects.

So something like target_id = ["${var.XXXX-appserver1}", "${var.XXXX-appserver2}"]

Hi there, i just ran into the same issue and hope you'll find a solution for this.

@cornevandyk @awuetz hi there!

I believe that @optimisticanshul took a stab at this in #9986.

@kwilczynski @optimisticanshul is there anything I need to do on #9986?

I have the same issue, there is the workaround, https://groups.google.com/forum/#!msg/terraform-tool/Mr7F3W8WZdk/ouVR3YsrAQAJ

What worked for me:

data "aws_instances" "instances" {
  instance_tags {
    Role = "Backend"
  }
}
resource "aws_lb_target_group_attachment" "main" {
  count            = "${aws_instance.my_instance.count}"
  target_group_arn = "${aws_lb_target_group.main.arn}"
  target_id        = "${data.aws_instances.instances.ids[count.index]}"
  port             = 80
}

Thanks djuretic for the solution, however it doesn't work for me until I change
target_id = "${data.aws_instances.instances.ids[count.index]}"
to
target_id = "${element(aws_instance.jt-api-aws.*.id, count.index)}"

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rjinski picture rjinski  ยท  3Comments

larstobi picture larstobi  ยท  3Comments

franklinwise picture franklinwise  ยท  3Comments

rjinski picture rjinski  ยท  3Comments

ronnix picture ronnix  ยท  3Comments