无法使用 terraform 在目标组中添加多个 target_id

Pra*_*h P 4 amazon-ec2 amazon-web-services amazon-elb terraform

我正在尝试创建目标组并使用 terraform 脚本将多台机器附加到目标组。

我无法附加多个 target_id 请帮助我实现这一目标。

小智 6

从 Terraform 开始0.12,这可能只是

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
}
Run Code Online (Sandbox Code Playgroud)

假设aws_instance.test返回一个list.

https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9是一个很好的参考。


Eth*_*hit 5

下面的代码实际上对我有用。

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)}"
}
Run Code Online (Sandbox Code Playgroud)

参考:

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


Pra*_*h P 2

感谢您的快速回复。

实际上,为 aws_alb_target_group_attachment 提供单独的标签(例如 test1 和 test2)帮助我在一个目标组内添加多个目标实例。

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}"
}
Run Code Online (Sandbox Code Playgroud)

  • 当您有固定数量的实例时,您可以使用它。任何将其与可变实例一起使用的方法。 (4认同)