2

Could anyone advise on how I can auto-mount an EBS volume created using Terraform and make it available on /custom?

resource "aws_instance" "ec201" {
...
  ebs_block_device {
    device_name = "/dev/sdd"
    volume_type = "gp2"
    volume_size = 10
    delete_on_termination = true
    encrypted = true
  }
...

Is it possible to auto-mount it?

I've read these pages:

When I do a:

> lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
...
nvme1n1     259:0    0   10G  0 disk 
nvme0n1     259:1    0  250G  0 disk 
└─nvme0n1p1 259:2    0  250G  0 part /

I have a 10GB partition that is not mounted. Would it be possible to auto-mount it using terraform?

dur
  • 15,689
  • 25
  • 79
  • 125
amir
  • 331
  • 2
  • 9
  • 20
  • 2
    You will need to run a [`remote-exec` provisioner](https://www.terraform.io/docs/language/resources/provisioners/remote-exec.html) that connects to the instance and runs a command to mount it or have userdata automatically mount it on first boot. – ydaetskcoR May 27 '21 at 09:52
  • 1
    Yes. Ok but do you have any clue how? Because, I don't see /dev/sdd in the instance once created. So 'im confused :( – amir May 27 '21 at 11:25

1 Answers1

7

As you can to see, your SO reads nvme1n1 as name of device (not /dev/sdd).

So, you could apply an user_data with the cloud-init instructions for your EC2 instance:

resource "aws_instance" "your-instance" {
  ..
  user_data              = file("user_data/ebs-mount.sh")
  ..
}

where user_data/ebs-mount.sh has the next content (considering that EBS disk have xfs format):

#cloud-config
hostname: your-instance

runcmd:

- sudo mkdir /custom -p
- sudo echo '/dev/nvme1n1 /custom xfs defaults 0 0' >> /etc/fstab
- sudo mount -a

output : { all : '| tee -a /var/log/cloud-init-output.log' }
dur
  • 15,689
  • 25
  • 79
  • 125
mrexojo
  • 156
  • 3
  • I had to format my disk, since it was an ext4. And used the mount command differently. `sudo mkfs -t ext4 /dev/nvme1n1` `sudo mount /dev/nvme1n1 /custom` These commands would replace the command #2, #3 above in the answer. – swateek Jul 08 '23 at 18:12