How do I mount, unmount, automount, and on-premises mount my Amazon EFS file system?

6 minute read
0

I want to know how to mount, unmount, automount, and on-premises mount my Amazon Elastic File System (Amazon EFS) file system.

Resolution

Mount a file system

To mount your EFS file system, either install the amazon-efs-utils package. Or, install the nfs-utils package from the Mankier website.

Use amazon-efs-utils

  1. To install the amazon-efs-utils package, run one of the following commands based on your distribution:
    Amazon Linux or Amazon Linux 2

    $ sudo yum install -y amazon-efs-utils

    Ubuntu and Debian-based distributions

    $ sudo apt-get update
    $ sudo apt-get -y install git binutils rustc cargo pkg-config libssl-dev
    $ git clone https://github.com/aws/efs-utils
    $ cd efs-utils
    $ ./build-deb.sh
    $ sudo apt-get -y install ./build/amazon-efs-utils*deb

    If your distribution doesn't provide a rust or cargo package, or provides versions that are earlier than 1.68, then use rustup to install rust and cargo:

    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh. "$HOME/.cargo/env"

    To build and install an RPM, run one the following commands based on your distribution:
    OpenSUSE or SLES

    $ sudo zypper refresh
    $ sudo zypper install -y git rpm-build make rust cargo openssl-devel
    $ git clone https://github.com/aws/efs-utils
    $ cd efs-utils
    $ make rpm
    $ sudo zypper --no-gpg-checks install -y build/amazon-efs-utils*rpm

    All other distributions

    $ sudo yum -y install git rpm-build make rust cargo openssl-devel
    $ git clone https://github.com/aws/efs-utils
    $ cd efs-utils
    $ make rpm
    $ sudo yum -y install build/amazon-efs-utils*rpm
  2. Open the EFS console.

  3. In the navigation pane, choose File systems.

  4. Select your file system.

  5. Choose Attach.

  6. Use SSH or Session Manager, a capability of AWS Systems Manager, to connect to the instance. Then, run the following commands:

    $ sudo mkdir -p /mnt/efs
    $ sudo mount -t efs -o tls fs-12345678:/ /mnt/efs
    $ sudo mount -t efs -o tls,accesspoint=fsap-12345678 fs-01233210 /mnt/efs

    Note: Replace the example values with your values.

Use nfs-utils

  1. To install the nfs-utils package, run one of the following commands based on your distribution:
    RHEL and CentOS-based distributions

    $ sudo yum -y install nfs-utils

    Ubuntu-based distributions

    $ sudo apt install nfs-common
  2. Open the EFS console.

  3. In the navigation pane, choose File systems.

  4. Select your file system.

  5. Choose Attach.

  6. Use SSH or Session Manager to connect to the instance, and then run the following command:

    $ sudo mkdir -p /mnt/efs
    $ sudo mount -t nfs -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport mount-target-DNS:/   ~/efs-mount-point

    -or-
    To use an IP address to mount, run the following command:

    $ sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport mount-target-ip:/  ~/efs-mount-point

    Note: Replace the example values with your values.

Unmount a file system

To unmount a file system, run the following command:

$ umount /mnt/efs

If the mount point is busy, then run the unmount command with the -l parameter:

$ umount -l /mnt/efs

Use /etc/fstab to automount a file system

To make an entry in /etc/fstab so that the Amazon EFS mount persists on reboot, run the following commands:

# vim /etc/fstab

Use amazon-efs-utils

fs-########:/ /mnt/efs efs _netdev,nofail,noresvport,tls,iam 0 0

Use nfs-utils

Configure your parameters in fstab:

fs-########.efs.REGION.amazonaws.com:/ /mnt/efs nfs4 defaults,_netdev,nofail 0 0
# mount -a

For mount options that use the mount helper, see Automatically mounting EFS file systems.

Note: You can use the IP address of mount target in a different Availability Zone from the client Amazon Elastic Compute Cloud (Amazon EC2) to mount. This mount method can incur cross Availability Zone data transfer charges and cause latency.

Use the Launch Wizard to mount a file system at instance launch

When you launch EC2 instances, you can use the launch wizard to automatically add user data to mount your file system.

Complete the following steps:

  1. Open the EC2 console.
  2. Choose Launch instances.
  3. Select an Amazon Machine Image (AMI) and an instance type, and then choose Next: Configure Instance Details.
  4. Configure the parameters for your use case. Make sure that you select the required virtual private cloud (VPC) and subnet for EFS mounting.
  5. On the Configure instance page, under File systems, select your file system. The path that's next to the file system ID is the mount point that the EC2 instance uses. You can change this path as needed. To mount your file system, user data is automatically generated in the Advanced details section:
    #cloud-config
    package_update: true
    package_upgrade: true
    runcmd:
    - yum install -y amazon-efs-utils
    - apt-get -y install amazon-efs-utils
    - yum install -y nfs-utils
    - apt-get -y install nfs-common
    - file_system_id_1=fs-0cae1679a766bcf49
    - efs_mount_point_1=/mnt/efs/fs1
    - mkdir -p "${efs_mount_point_1}"
    - test -f "/sbin/mount.efs" && printf "\n${file_system_id_1}:/ ${efs_mount_point_1} efs tls,_netdev\n" >> /etc/fstab || printf "\n${file_system_id_1}.efs.us-east-1.amazonaws.com:/ ${efs_mount_point_1} nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport,_netdev 0 0\n" >> /etc/fstab
    - test -f "/sbin/mount.efs" && grep -ozP 'client-info]\nsource' '/etc/amazon/efs/efs-utils.conf'; if [[ $? == 1 ]]; then printf "\n[client-info]\nsource=liw\n" >> /etc/amazon/efs/efs-utils.conf; fi;
    - retryCnt=15; waitTime=30; while true; do mount -a -t efs,nfs4 defaults; if [ $? = 0 ] || [ $retryCnt -lt 1 ]; then echo File system mounted successfully; break; fi; echo File system not available, retrying to mount.; ((retryCnt--)); sleep $waitTime; done;>
    -or-
    To mount an Amazon EFS file system on a custom AMI or with specific options, run the following commands to add custom user data:
    RHEL and CentOS-based distributions
    #!/bin/bash
    sudo mkdir -p /mnt/efs
    sudo yum -y install nfs-utils
    Ubuntu-based distributions
    #!/bin/bash
    sudo mkdir -p /mnt/efs
    sudo apt install nfs-common
    sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport mount-target-ip:/  /mnt/efs
    For more information, see Run commands on your Amazon EC2 instance at launch.
  6. Launch the instance.

Mount a file system on-premises

To mount an Amazon EFS file system on your on-premises servers, there must be connectivity between Amazon EFS and the on-premises servers. To establish connectivity between the on-premises server and Amazon Virtual Private Cloud (Amazon VPC), use AWS Direct Connect and AWS VPN. Then, run the following commands to install the NFS client and mount your file system:

$ sudo yum -y install nfs-utils (Red Hat Linux)
$ sudo apt-get -y install nfs-common (Ubuntu)
$ mkdir ~/efs
$ sudo mount -t nfs -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport mount-target-IP:/  ~/efs

For more information, see Tutorial: Mounting with on-premises Linux clients.

AWS OFFICIAL
AWS OFFICIALUpdated a month ago
2 Comments

In a few places there should be a line feed that is missing from the list of commands:

Example 1:

$ sudo mkdir -p /mnt/efs$ sudo mount -t efs -o tls fs-12345678:/ /mnt/efs

Should be

$ sudo mkdir -p /mnt/efs
$ sudo mount -t efs -o tls fs-12345678:/ /mnt/efs

Example 2:

#cloud-configpackage_update: true

Should be:

#cloud-config
package_update: true

Example 3:

Note: This is broken the same in 2 places

#!/bin/bashsudo mkdir -p /mnt/efs

Should be

#!/bin/bash
sudo mkdir -p /mnt/efs

Example 4:

$ sudo yum -y install nfs-utils (Red Hat Linux)$ sudo apt-get -y install nfs-common(Ubuntu)

Should be:

$ sudo yum -y install nfs-utils # For (Red Hat Linux)
$ sudo apt-get -y install nfs-common # For (Ubuntu)
WLovins
replied a month ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied a month ago