如何对多个实例使用 Terraform provisioner

How to use Terraform provisioner with multiple instances

我想创建 x 个实例和 运行 同一个供应商。

resource "aws_instance" "workers" {
  ami = "ami-08d658f84a6d84a80"
  count = 3
  ...
provisioner "remote-exec" {
    scripts = ["setup-base.sh", "./setup-docker.sh"]
    connection {
      type = "ssh"
      host = "${element(aws_instance.workers.*.public_ip, count.index)}"
      user = "ubuntu"
      private_key = file("${var.provisionKeyPath}")
      agent = false
    }
  }

我认为 host 行混淆了 Terraform。获取 Error: Cycle: aws_instance.workers[2], aws_instance.workers[1], aws_instance.workers[0]

自从我升级了terraform版本(0.12)后,我遇到了和你一样的问题。

您需要对 connection 对象中的 host 属性 使用 ${self.private_ip}connection 对象应该位于 provisioner "remote-exec"

之外

详情如下。

resource "aws_instance" "workers" {
  ami = "ami-08d658f84a6d84a80"
  count = 3
  ...
  connection {
    host = "${self.private_ip}"
    type = "ssh"
    user = "YOUR_USER_NAME"
    private_key = "${file("~/YOUR_PEM_FILE.pem")}"
  }

  provisioner "remote-exec" {
    scripts = ["setup-base.sh", "./setup-docker.sh"]
  }
...
}

如果您需要获取更多信息,下面的 link 将为您提供帮助。 https://github.com/hashicorp/terraform/issues/20286