Not able to add multiple target_id inside targer group using terraform

 ̄綄美尐妖づ 提交于 2020-12-06 02:19:01

问题


I'm trying to create target groups and attach multiple machines to the target groups using terraform script.

I'm not able to attach multiple target_id please help me to achieve this.


回答1:


As of Terraform 0.12, this could simply be

resource "aws_alb_target_group_attachment" "test" {
  count = length(aws_instance.test)
  target_group_arn = aws_alb_target_group.test.arn
  target_id = aws_instance.test[count.index].id
}

assuming aws_instance.test returns a list.

https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9 is an excellent reference.




回答2:


Thanks for your quick reply.

Actually giving seperate tag like test1 and test2 for aws_alb_target_group_attachment helped me to add multiple target instances inside one taget group.

resource "aws_alb_target_group_attachment" "test1" {
  target_group_arn = "${aws_alb_target_group.test.arn}"
  port             = 8080
  target_id        = "${aws_instance.inst1.id}"
}
resource "aws_alb_target_group_attachment" "test2" {
  target_group_arn = "${aws_alb_target_group.test.arn}"
  port             = 8080
  target_id        = "${aws_instance.inst2.id}"
}



回答3:


Below code actually works for me.

resource "aws_alb_target_group_attachment" "test" {
  count = 3 #This can be passed as variable.
  target_group_arn = "${aws_alb_target_group.test.arn}"
  target_id         = "${element(split(",", join(",", aws_instance.web.*.id)), count.index)}"
}

Ref:

https://github.com/terraform-providers/terraform-provider-aws/issues/357 https://groups.google.com/forum/#!msg/terraform-tool/Mr7F3W8WZdk/ouVR3YsrAQAJ




回答4:


Try creating a list of instance ID's and then iterate over using the count index.

For example:

variable "instance_list" {
  description = "Push these instances to ALB"
  type = "list"
  default = ["i00001", "i00002", "i00003"]
}

resource "aws_alb_target_group_attachment" "test" {
  count            = "${var.instance_list}"
  target_group_arn = "${aws_alb_target_group.test.arn}"
  target_id        = "${element(var.instance_list, count.index)}"
  port             = 80
}


来源:https://stackoverflow.com/questions/44491994/not-able-to-add-multiple-target-id-inside-targer-group-using-terraform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!