Terraform Route53记录EC2实例的延迟

时间:2017-09-26 22:50:16

标签: amazon-ec2 amazon-route53 terraform

我正在创建EC2实例但是当我添加Route53记录时,DNS最多需要60秒才能复制并获得识别。

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配置程序在我的EC2实例中启动服务,该服务依赖于DNS来查找其对等体。

在创建EC2实例之前,我似乎无法配置DNS条目。

有没有办法可以后处理我的EC2实例并在以后启动服务?或者是否存在延迟服务启动直到存在DNS条目的技术?

1 个答案:

答案 0 :(得分:0)

您可以使用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提供所有节点的列表,但您可能想要提出另一个问题。