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是一个很好的参考。
下面的代码实际上对我有用。
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
感谢您的快速回复。
实际上,为 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)
| 归档时间: |
|
| 查看次数: |
8153 次 |
| 最近记录: |