Terraform 循环依赖 [英] Terraform cyclic dependency
问题描述
我正在尝试通过 Terraform 实例化 3 个知道彼此 IP 地址的 aws_instance.这当然会导致循环依赖.我想知道克服这个问题的最佳方法是什么.我尝试了几种解决方案:
I'm trying to instantiate 3 aws_instances that are aware of each other ip address via Terraform. This of course results in a cyclic dependency. I was wondering what is the best way to overcome this problem. I've tried a couple of solutions:
- 一起实例化 2 个实例,然后实例化 1 个依赖于这 2 个实例的实例.在第三个实例中,有一个 user_data 脚本,允许实例通过 ssh 连接到其他 2 个实例以设置必要的配置.
它有效,但我不喜欢它创建 2 个不同的资源组的事实,即使这 3 个实例在初始化后的所有意图和目的都是相同的.
It works, but I don't like the fact that it creates 2 distinct groups of resources even though the 3 instances are for all intent and purpose identical after init.
- 同时实例化 3 个实例,然后实例化另一个实例,其唯一目的是 ssh 进入每个实例以设置必要的配置.初始化完成后,附加实例应自行终止.
它也可以,但是 terraform 会将第 4 个资源视为已终止的资源,并会在有更新时尝试重新创建它,因此这不是很干净.
It also works, but then terraform will see the 4th resource as a terminated resource and will try to recreate it whenever there is an update so this is not very clean.
有什么建议吗?谢谢.
这是一个不能与 remote-exec 一起工作的尝试来说明循环依赖:
Here is an attempt that doesn't work with remote-exec to illustrate the cyclic dependency:
resource "aws_instance" "etcd" {
count = 3
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
subnet_id = module.vpc.public_subnets[count.index].id
provisioner "remote-exec" {
inline = [
"echo ${aws_instance.etcd[0].private_ip}",
"echo ${aws_instance.etcd[1].private_ip}",
"echo ${aws_instance.etcd[2].private_ip}"
]
}
}
推荐答案
根据评论.
您可以使用 remote-exec 在您的三个实例,仅在全部配置完成后.你必须开发这样一个脚本来 ssh 到他们三个.
You could use remote-exec to run code on your three instance, only after all of them have been provisioned. You would have to develop such a script to ssh to the three of them.
但是,您只能在第三个实例启动后调用您的 remote-exec
脚本.这可以通过使用 null_resoruce 来实现,这取决于实例.
But, you would only invoke your remote-exec
script after the third instance is up. This can be achieved by using null_resoruce which would depend on the instances.
无资源的供应商中描述了这种使用 null_resource 的方式.
This way of using null_resource is described in Provisioners Without a Resource.
这篇关于Terraform 循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!