地形循环依赖 [英] Terraform cyclic dependency
问题描述
我正在尝试实例化3个通过Terraform知道彼此ip地址的aws_instances.当然,这导致循环依赖性.我想知道什么是克服此问题的最佳方法.我已经尝试了几种解决方案:
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个实例,然后实例化一个依赖于这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}"
]
}
}
推荐答案
基于评论.
您可以使用远程执行器在您的代码上运行代码三个实例,只有在全部配置好后..您将必须开发这样的脚本才能将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.
这篇关于地形循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!