3

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

Jsp
  • 166
  • 1
  • 1
  • 13
  • 1
    fstab is not a programming environment, and you can't embed dynamic content into it. you could GENERATE the file dynamically, but it's read as static text, no matter what you try to do internally. – Marc B Jul 07 '16 at 16:25
  • Thanks... any alternatives to get this mounted on boot? – Jsp Jul 07 '16 at 16:28
  • 2
    external script in cron or some other auto-trigger environment that does the mount calls for you. then you can figure out IPs and call mount directly. – Marc B Jul 07 '16 at 16:29

3 Answers3

1

For me, using fstab (as amazon recommends) did not work when i manually stopped and started my EC2 instance. Which for me is the reason for automation - I need a solution for a disaster recovery scenarios, cold-boot and when going off-line to scale vertically (for example when manually adding RAM to an EC2 instance, you need to 'stop' it, 'add the RAM', then 'start it'). For me it worked to do

 crontab -e

and then add the line

@reboot sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 fs-123ddddd.efs.us-east-1.amazonaws.com:/ /data
user1709076
  • 2,066
  • 7
  • 31
  • 50
  • Thanks a ton ! It worked for me as well. I hope there is no downside of doing it this way. – Naxi Oct 26 '18 at 15:55
0

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!

Jsp
  • 166
  • 1
  • 1
  • 13
0

Update: Currently, EFS already allows you to use a generic URL that doesn't depends on the availability zone (it always point to the correct availability point).

You only need to use a fstab entry like this one:

'file-system-id'.efs.'aws-region'.amazonaws.com:/  /path/to/dir  nfs4  nfsvers=4.1  0  0

Old response:

I had the same problem to mount EFS volumes in my /etc/fstab independent of the availability zone.

From experoinc.com - Amazon’s Elastic File System: Kicking the Tires:

Finally, it’s no fun having the EFS disappear when a machine reboots, so consider adding the EFS mount to the /etc/fstab of your machine image. Unfortunately, you have different mount points per availability zone, so it’s a little harder to bake into a per-region AMI as one usually does. It may be wise to configure in a provisioning script to be run on boot.

For now, the solution I'm using is to create a mount helper (/sbin/mount.efs-nfs4) and use it to mount my EFS volumes.

The code and the instructions for this helper are in this Github Gist: https://gist.github.com/rarylson/9095c56137a60a7fdb7bb2b420f0ad04.

In short, using it, you can put a line like this in /etc/fstab, without specifying the availability zone in the URL and using the efs-nfs4 "fake" filesystem type:

'file-system-id'.efs.'aws-region'.amazonaws.com:/  /path/to/dir  efs-nfs4  nfsvers=4.1  0  0

And then running:

mount /path/to/dir

The EFS filesystem will be mounted at boot time after a reboot also.