Question

DigitalOcean Terraform can't work with Count and droplet_ids

When trying to run the following script I get an error and I don’t understand why. All the example show it should be done this way:

resource "digitalocean_loadbalancer" "web" {
  name   = "loadbalancer-1"
  region = "ams3"

  forwarding_rule {
    entry_port     = 80
    entry_protocol = "http"

    target_port     = 80
    target_protocol = "http"
  }

  healthcheck {
    port     = 80
    protocol = "tcp"
  }

  droplet_ids = ["${digitalocean_droplet.web.*.id}"]
}

resource "digitalocean_droplet" "web" {
  count              = 2
  image              = "ubuntu-18-04-x64"
  name               = "web-${count.index + 1}"
  region             = "ams3"
  size               = "s-1vcpu-1gb"
  private_networking = true
  user_data          = "${file("config/webuserdata.sh")}"
  ssh_keys = [
    "${var.ssh_fingerprint}"
  ]
}

The error:

Error: Incorrect attribute value type

  on web.tf line 18, in resource "digitalocean_loadbalancer" "web":
  18:   droplet_ids = ["${digitalocean_droplet.web.*.id}"]

Inappropriate value for attribute "droplet_ids": element 0: number required.

I’m new to terraform and I don’t know how to solve this issue.


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
June 25, 2019
Accepted Answer

Terraform recently released a major new version, 0.12.x. This included some fairly important changes in syntax. One of these changes is “first-class list support.” Check out that link for all the details. But to solve your immediate problem, just change:

droplet_ids = ["${digitalocean_droplet.web.*.id}"]

to:

droplet_ids = digitalocean_droplet.web.*.id

Since some of the changes to the syntax were disruptive, Terraform 0.12 comes with an upgrade command that can make these changes for you automatically. Running the following should resolve the issue:

terraform 0.12upgrade

I’d recommend running after first checking your existing work into a git branch so that you can review the changes it makes by looking at the diff.

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