Question

How do I work with "count" and connection.host using Terraform?

Using this setup

me@mybox:~terraform -v
Terraform v0.12.8
+ provider.digitalocean v1.7.0

I try to create two droplets serving Nginx using this Terraform file

resource "digitalocean_droplet" "web" {

    count = 2
    name  = "web-${count.index}"

    image = "ubuntu-14-04-x64"
    region = "fra1"
    size = "512mb"
    private_networking = true
    ssh_keys = [
      "${var.ssh_fingerprint}"
    ]

  connection {
    user = "root"
    type = "ssh"
    host = digitalocean_droplet.web[count.index].ipv4_address
    private_key = "${file(var.pvt_key)}"
    timeout = "2m"
  }

  provisioner "remote-exec" {
    inline = [
      "export PATH=$PATH:/usr/bin",
      # install nginx
      "sudo apt-get update",
      "sudo apt-get -y install nginx",
      "curl -sSL https://repos.insights.digitalocean.com/install.sh | sudo bash"
    ]
  }
}

but get this error when running terraform plan

Error: Cycle: digitalocean_droplet.web[1], digitalocean_droplet.web[0]

If I change the specification of connection.host to foo terraform runs without errors. Of course, it becomes hard to run any commands on the host afterwards, so my question is this:

How do I specify the host address when using count variables?

Thanks in advance and have a nice day, Morten


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

Rather than using the count, you can simplify this a bit by referencing self.ipv4_address and Terraform will do the right thing. self allows you to access attributes of the resource it is called from.

Putting this all together, you get:

resource "digitalocean_droplet" "web" {

    count = 2
    name  = "web-${count.index}"

    image = "ubuntu-14-04-x64"
    region = "fra1"
    size = "512mb"
    private_networking = true
    monitoring = true
    ssh_keys = [
      "${var.ssh_fingerprint}"
    ]

  connection {
    user = "root"
    type = "ssh"
    host = self.ipv4_address
    private_key = "${file(var.pvt_key)}"
    timeout = "2m"
  }

  provisioner "remote-exec" {
    inline = [
      "export PATH=$PATH:/usr/bin",
      # install nginx
      "sudo apt-get update",
      "sudo apt-get -y install nginx",
      "curl -sSL https://repos.insights.digitalocean.com/install.sh | sudo bash"
    ]
  }
}

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Thank you a metric ton, asb! That did the trick.

As you might have guessed, I have just started my journey into Terraform and your help on this matter has been invaluable. Should your life bring you through Denmark, make sure to drop in for a serving of your favourite beverage!