Automatically mount EBS Volumes in Amazon EC2

Wouter van der Meulen
Wouter van der Meulen
Jun 17 2022
Posted in Engineering & Technology

A short guide to simplifying EBS mounts.

Automatically mount EBS Volumes in Amazon EC2


As a DevOps engineer, I often need to mount EBS volumes. Which is easy enough to do manually; you just add it in AWS console and run the necessary commands on the server. But there are some quirks to EBS volumes that make it more difficult to do it automatically.

Anyone who has mounted a volume will know to look for device names in /dev/. Typically, mounts will be named something like sd[a-z]. Which is exactly what AWS lets us do when creating EBS volume attachments. The problem is that the chosen name in AWS does not translate to the actual device name on the server. Instead, they are exposed as /dev/nvme[0-26]n1. This makes mounting automatically a pain in the backend!


The following scripts assume that you are running them as a root user through cloud-init. If you are not, you will need to modify the scripts to use the sudo command.


With a bit of scripting magic we can pretend AWS just gave us the device name we specified when we created the attachment. All we're going to do is fish out the device name from the NVME data with nvme id-ctrl and then we'll symlink the NVME device to the name we've specified. Which looks something like:

# Find all the NVME devices
VOLUMES_NAME=$(find /dev | grep -i 'nvme[0-21]n1$')

  # Find and set the alias name we've set in AWS
  ALIAS=$(nvme id-ctrl -v $VOLUME | grep -Po '/dev/(sd[b-z]|xvd[b-z])')
  # Check if the alias exists, if not, create it
  if [ ! -z $ALIAS ]; then
    ln -s $VOLUME $ALIAS

With this, we can safely mount the specific volumes based on device names we have set ourselves.

Formatting and Labeling

Now, if this is a fresh volume, you will need to format it. This is easy enough to do with the mkfs command. Please note that this is just an example. Please make sure you format using your filesystem of choice, as well as the device name.

mkfs -t xfs /dev/sdf

Afterwards, we can add a label to this volume to make it easier to find. Because this is a xfs volume, we can use xfs_admin.

xfs_admin -L data /dev/sdf


Because we have labeled the volume, we can mount it using that label. First, we'll add the mount data to fstab (filesystem table file) to make sure it gets mounted on boot. This is just an example fstab, the configuration is up to you.

LABEL=data /data xfs rw,noatime,noexec,attr2,inode64,noquota 0 0

Now, simply mount the volume using the mount command.

mount -a


When provisioning servers and volumes at the same time, the NVME devices may not be ready yet when the init scripts are running. You could check if the NVME device name is available yet, or simply set a timer and wait for it to be ready.


That's it! You can now mount EBS volumes in Amazon EC2 automatically without having to worry about mismatching device names.

As always, we hope you liked this article and if you have anything to add, maybe you are suited for a Developer position in Notificare. We are currently looking for several positions, so please check out our careers page.

Keep up-to-date with the latest news