如何在多个实例中使用Terraform Provisioner

时间:2019-06-25 08:53:16

标签: terraform terraform-provider-aws

我想创建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]

1 个答案:

答案 0 :(得分:2)

自从我升级了Terraform版本(0.12)以来,我遇到了与您相同的问题。

您需要为${self.private_ip}对象中的host属性使用connection, 并且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"]
  }
...
}

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