Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform and DigitalOcean: assign volume to specific droplet created with count parameter

just started exploring terraform to spin up droplets and volumes on digital ocean.

My question is to know the right way to do the following:

  • create a certain number of droplet instances using count within digitalocean_dropletresource named ubuntu16
  • assign a digitalocean_volume only to one or a subset of previously created droplets.

How to do it?I was assuming to use droplets_id property on digitalocean_volume resource. Something like:

resource "digitalocean_volume" "foovolume" {
  ...
  droplet_ids = ["${digitalocean_droplet.ubuntu16.0.id}"]
}

Validating it with terraform validate I got:

Error: digitalocean_volume.foovolume: "droplet_ids": this field cannot be set

Any advice? Thanks to any inputs on it.

Regards

like image 892
mvito Avatar asked Oct 22 '25 04:10

mvito


1 Answers

The way the Terraform provider for DigtialOcean is currently implemented requires that you take the opposite approach. You can specify which volumes are attached to which Droplets by defining the volume_ids of the Droplet resource. For example:

resource "digitalocean_volume" "volume" {
    region      = "nyc3"
    count       = 3
    name        = "volume-${count.index + 1}"
    size        = 100
    description = "an example volume"
}

resource "digitalocean_droplet" "web" {
    count      = 3
    image      = "ubuntu-17-10-x64"
    name       = "web-${count.index + 1}"
    region     = "nyc3"
    size       = "1gb"
    volume_ids = ["${element(digitalocean_volume.volume.*.id, count.index)}"]
}

If you look at the docs for the volume resource, you'll see that droplet_ids is a "computed" field. This means that you are unable to set the field, and that its value is computed by Terraform via the provider's API.

like image 81
andrewsomething Avatar answered Oct 23 '25 21:10

andrewsomething



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!