无法使用terraform

时间:2017-06-12 05:32:10

标签: amazon-web-services amazon-ec2 amazon-elb terraform

我正在尝试使用terraform脚本创建目标组并将多台计算机连接到目标组。

我无法附加多个target_id,请帮助我实现此目的。

4 个答案:

答案 0 :(得分:3)

感谢您的快速回复。

实际上为aws_alb_target_group_attachment提供了像test1和test2这样的单独标签帮助我在一个taget组中添加了多个目标实例。

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}"
}

答案 1 :(得分:2)

尝试创建实例ID列表,然后使用count索引进行迭代。

例如:

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
}

答案 2 :(得分:0)

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

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)}"
}

参考:

  

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

答案 3 :(得分:0)

从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
}

假设aws_instance.test返回一个list

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