2
votes

I want to create x instances and run the same provisioner.

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
    }
  }

I think the host line confuses Terraform. Getting Error: Cycle: aws_instance.workers[2], aws_instance.workers[1], aws_instance.workers[0]

1
Is that provisioner part of the aws_instance.workers resource? It's pretty hard to tell from what you've included in the question right now so an edit to clarify that would be useful.ydaetskcoR
Happy to provide more information if you could let me know wha you're missing.soupdiver
Can you include the full Terraform config please? In general it's best to provide a minimal reproducible example so that people can reproduce your issues.ydaetskcoR

1 Answers

4
votes

Since I upgrade my terraform version(0.12), I have been encountered the same problem as yours.

You need to use ${self.private_ip} for the host property in your connection object, and the connection object should be located out of the provisioner "remote-exec"

Details are the below.

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"]
  }
...
}

If you need to get more information, the below link is gonna be helping you. https://github.com/hashicorp/terraform/issues/20286