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.

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
September 11, 2019
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"
    ]
  }
}

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!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel