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”
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
}
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!
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
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"
]
}
}
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.