Q:

How to use Terraform provisioner with multiple instances

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]

A:

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

