Question

Terraform - mounting a volume

I’m creating a volume from a snapshot and attaching it to a droplet. I’m attempting to mount the volume when it is created. According to the answer to the following question, all I need to do is specify “initial_filesystem_type”

https://www.digitalocean.com/community/questions/what-s-the-best-way-to-create-a-droplet-with-block-storage-using-terraform-and-ansible

However, although the volume is attached to the droplet, it is not being mounted.


resource "digitalocean_droplet" "droplet-instance" {

  for_each = var.droplets

  image = each.value.image
  name = each.key
  region = each.value.region
  size = each.value.size
  ssh_keys = each.value.ssh_keys
}


data "digitalocean_volume_snapshot" "droplet-snapshot" {
  name = "${var.snapshot_name}"
}


resource "digitalocean_volume" "droplet-volume" {
  for_each = var.droplets

  region      = each.value.region
  name        = "${each.key}-volume"
  size        = data.digitalocean_volume_snapshot.droplet-snapshot.min_disk_size
  snapshot_id = data.digitalocean_volume_snapshot.droplet-snapshot.id
  initial_filesystem_type = "ext4"
  description             = "an example volume"
}

resource "digitalocean_volume_attachment" "droplet-volume-attachment" {
  for_each = var.droplets

  droplet_id = digitalocean_droplet.droplet-instance[each.key].id
  volume_id  = digitalocean_volume.droplet-volume[each.key].id
}

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.

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.

For anyone looking for an answer, I got it to work using a null_resource with a provisioner. The scripts to actually mount the volume are somewhat configurable.

resource "null_resource" "node" {
  count     = var.volume_size == 0 ? 0 : var.node_count

  depends_on = [digitalocean_volume_attachment.node]
  provisioner "remote-exec" {
    connection {
      host = digitalocean_droplet.node[count.index].ipv4_address
      port = 22
      user = "root"
      type = "ssh"
      private_key = var.private_key
    }
    inline = [
      "sudo mkfs.ext4 /dev/disk/by-id/scsi-0DO_Volume_${digitalocean_droplet.node[count.index].name}",
      "mkdir -p /mnt/${digitalocean_droplet.node[count.index].name}",
      "mount -o discard,defaults,noatime /dev/disk/by-id/scsi-0DO_Volume_${digitalocean_droplet.node[count.index].name} /mnt/${digitalocean_droplet.node[count.index].name}",
      "echo '/dev/disk/by-id/scsi-0DO_Volume_${digitalocean_droplet.node[count.index].name} /mnt/${digitalocean_droplet.node[count.index].name} ext4 defaults,nofail,discard 0 0' | sudo tee -a /etc/fstab"
    ]
  }
}

Using the volume_ids = [“${digitalocean_volume.VOLUME_NAME_HERE.id}”] approach had the same undesired results for me. The volume is successfully attached to the droplet, however it is not mounted on the file system.

I assume it was mounted under the “/mnt” directory for you?

Hey there, @julianbroudou,

So, I recreated the answer to the other question and it seems it did not work for me either. However, I tried another method (from Terraform docs) and it did the job just fine!

Instead of doing a whole new block of digitalocean_volume_attachment, you can simply add the following line in your digitalocean_droplet block. It goes like this:

volume_ids = [“${digitalocean_volume.VOLUME_NAME_HERE.id}”]

We have an array of volume IDs (in my case only 1 element) which is doing the same thing as digitalocean_volume_attachment.

In short, I would recommend deleting the digitalocean_volume_attachment block, and instead simply include the volume_ids argument in your digitalocean_droplet block.

I hope this helps, and please let me know if that solved your issue!

Best, Dennis