Terraform EC2实例的Route53记录延迟
问题描述:
我正在创建EC2实例,但是当我添加Route53记录时,DNS需要60秒才能进行复制并得到识别。Terraform EC2实例的Route53记录延迟
resource "aws_instance" "zk-host" {
ami = "ami-30041c53" # Amazon Linux Image
count = "${var.count}"
associate_public_ip_address = false
connection {
user = "ec2-user"
}
provisioner "remote-exec" {
inline = [
... install things here...
#"sudo service zookeeper-server start ... after some delay ?"
]
}
}
// Make a DNS entry for each host while we're here.
resource "aws_route53_record" "route53-record-zk" {
zone_id = "${var.route53-zone-id}"
count = "${var.count}"
name = "${var.host-name-prefix}${count.index}.dwh.local"
type = "A"
ttl = "30"
records = [
"${element(aws_instance.zk-host.*.private_ip, count.index)}"
]
}
问题是,我正在使用remote-exec provisioner在我的EC2实例中启动依赖于DNS查找其对等方的服务。
我似乎无法在创建EC2实例之前配置DNS条目。
有没有办法可以后期处理我的EC2实例并在以后启动服务?还是有一种技术可以延迟服务启动,直到DNS条目存在?
答
您可以使用null_resource
来解决此问题,以便将供应商与实例资源分开。
在你的情况下,它看起来是这样的:
resource "aws_instance" "zk-host" {
ami = "ami-30041c53" # Amazon Linux Image
count = "${var.count}"
associate_public_ip_address = false
}
// Make a DNS entry for each host while we're here.
resource "aws_route53_record" "route53-record-zk" {
zone_id = "${var.route53-zone-id}"
count = "${var.count}"
name = "${var.host-name-prefix}${count.index}.dwh.local"
type = "A"
ttl = "30"
records = [
"${element(aws_instance.zk-host.*.private_ip, count.index)}"
]
}
resource "null_resource" "zk-host_provisioning" {
count = "${var.count}"
connection {
user = "ec2-user"
host = "${element(aws_instance.zk-host.*.public_ip, count.index)}"
}
provisioner "remote-exec" {
inline = [
... install things here...
#"sudo service zookeeper-server start ... after some delay ?"
]
}
}
我会稍微谨慎依靠DNS发现这里虽然因为当它涉及到的东西像自动缩放组中运行限制你的选择的(因为实例没有制定出自己的DNS记录并在启动时进行配置),因此您可能需要考虑Zookeeper集群内部的其他服务发现选项。 This blog post建议您可以使用S3提供所有节点的列表,但您可能想要提出另一个有关该问题的问题。
+0
谢谢你。不用担心,当我们移出POC并将其封装到生产环境和其他环境中时,我们将整理好这一切。这虽然有帮助,并有助于我们继续前进。干杯。 – Exie
您需要在remote-exec中添加包装脚本,例如'sleep 120'。 – BMW
感谢您的建议,但没有奏效。这个问题似乎是* aws_route53_record *的相互依赖关系。它取决于aws_instance获取它的IP。添加睡眠只会延迟aws_instance的完成。我想我需要以某种方式在背景中分叉它。 – Exie