amazon-web-servicesautomount

Dynamic IP in /etc/fstab


I am trying to use elastic file system (EFS) in AWS... My goal is to auto-mount it using /etc/fstab

Since EC2 instances are auto scaled across available zones, EFS mount IP's change depending on the instance's zone. At the moment AWS provides this command to mount it to the correct zone...

sudo mount -t nfs4 -o nfsvers=4.1 $(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone).fs-xxxx.efs.us-east-1.amazonaws.com:/ efs

However, there is some issues with EFS DNS url's, I was only able to connect via provided EFS IP's. So I created a bash script to obtain the correct IP pertaining to zone...

nano /efsmount.sh

#!/bin/sh

CURR_ZONE=$(/usr/bin/curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)

if [ "$CURR_ZONE" == "us-east-1e" ];then
    echo "172.xx.xx.xx"
fi
... more if statements to cover all zones

I can mount using this script

sudo mount -t nfs4 -o nfsvers=4.1 $(/efsmount.sh):/ /efs

Now my question is... how do I auto mount using fstab?

Something like this does not work

$(/efsmount.sh):/  /efs   nfs      auto,noatime,nolock,bg,nfsvers=4.1,intr,tcp,actimeo=1800 0 0

Thanks


Solution

  • I was able to auto mount EFS during boot with a init.d script. Here is the instructions I followed: http://www.archisoft.ca/ami/lemp/#setting-up-efs-with-auto-mounting

    This init.d script starts at boot time and loops through a function until network becomes available and then mounts the EFS asap.

    EFS is getting mounted before web server starts, therefore site directories residing in the EFS are recognized by NGINX web server without issues.

    I thought this might help someone!