Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform: Mount volume

According to documentation, using terraform, I'm able to create a droplet on digital ocean:

resource "digitalocean_volume" "foobar" {
  region      = "nyc1"
  name        = "baz"
  size        = 100
  description = "an example volume"
}

So, I'm also able to add a volume to it:

resource "digitalocean_droplet" "foobar" {
  name       = "baz"
  size       = "1gb"
  image      = "coreos-stable"
  region     = "nyc1"
  volume_ids = ["${digitalocean_volume.foobar.id}"]
}

I'd like to know how to mount this on a desired location. I need to mount it automatically. I mean, when droplet is up I need to the volume is mounted. I was thinking about using chef...

Any ideas?

like image 388
Jordi Avatar asked May 19 '17 13:05

Jordi


People also ask

How do you detach EBS volume from ec2 instance using terraform?

Just run the plan without the aws_volume_attachment and Terraform will remove the volume. Terraform makes all the necessary changes to have the same infrastructure in code as in AWS. Anything missing in AWS will be created and any thing missing in code will be removed from AWS.


2 Answers

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}/scripts/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}/scripts/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

like image 117
Innocent Anigbo Avatar answered Nov 13 '22 01:11

Innocent Anigbo


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.

like image 41
c3st7n Avatar answered Nov 13 '22 01:11

c3st7n