Terraform: Mount volume

Mounting the volume needs to be done from the guest OS itself using mount, fstab, etc.

The digital ocean docs cover this here.

Using Chef you could use resource_mount to mount it in an automated fashion.

The device name will be /dev/disk/by-id/scsi-0DO_Volume_YOUR_VOLUME_NAME. So, using the example from the Terraform docs, it would be /dev/disk/by-id/scsi-0DO_Volume_baz.


To mount the volume automatically, you can use user_data via cloud init to run a script as follow:

This is how your digitalocean_droplet resources should reflect:

resource "digitalocean_droplet" "foobar" {
  name       = "baz"
  size       = "1gb"
  image      = "coreos-stable"
  region     = "nyc1"
  volume_ids = ["${digitalocean_volume.foobar.id}"]
   # user data
  user_data = "${data.template_cloudinit_config.cloudinit-example.rendered}"
}

Then your cloud.init file that contains the cloudinit_config should be as bellow. It will reference the shell script in ${TERRAFORM_HOME}/script/disk.sh that would mount your volume automatically:

provider "cloudinit" {}


data "template_file" "shell-script" {
  template = "${file("scripts/disk.sh")}"

}
data "template_cloudinit_config" "cloudinit-example" {

  gzip = false
  base64_encode = false

  part {
    content_type = "text/x-shellscript"
    content      = "${data.template_file.shell-script.rendered}"
  }

}

The shell script to mount the volume automatically on startup is in ${TERRAFORM_HOME}/script/disk.sh

It will first check if a file system exist. If true it wouldn't format the disk if not it will

#!/bin/bash


DEVICE_FS=`blkid -o value -s TYPE ${DEVICE}`
if [ "`echo -n $DEVICE_FS`" == "" ] ; then
        mkfs.ext4 ${DEVICE}
fi
mkdir -p /data
echo '${DEVICE} /data ext4 defaults 0 0' >> /etc/fstab
mount /data

I hope this helps