DEV Community

Cover image for Mount an AWS EFS to Multiple EC2 Instances from a Local Linux Machine
Md Abu Musa
Md Abu Musa

Posted on

Mount an AWS EFS to Multiple EC2 Instances from a Local Linux Machine

Amazon Elastic File System (EFS) is a scalable, elastic, and shared file storage service for use with AWS compute services, including EC2 instances. This guide explains how to create an EFS, mount it on multiple EC2 Ubuntu instances using the DNS method, and access it from your local Linux machine. We'll also break down the commands used step by step.


Prerequisites

  • AWS EC2 Instances: Multiple Ubuntu instances configured in the same VPC as the EFS.
  • EFS Created: An Elastic File System set up in the AWS Management Console.
  • Local Linux Machine: Used to manage and mount the EFS to EC2 instances.

Steps to Mount EFS on AWS EC2 Instances

Step 1: Switch to Root User

sudo su
Enter fullscreen mode Exit fullscreen mode
  • This command switches to the root user to ensure you have the necessary privileges to install packages and modify the filesystem.

Step 2: Navigate to the Root Directory

cd /
Enter fullscreen mode Exit fullscreen mode
  • Moves you to the root directory of the system, where you will create a new directory to mount the EFS.

Step 3: Create a Directory for Mounting the EFS

mkdir efs
Enter fullscreen mode Exit fullscreen mode
  • Creates a new directory named efs at the root level. This directory will serve as the mount point for your EFS.

Step 4: Install Amazon EFS Utilities

sudo yum install -y amazon-efs-utils
Enter fullscreen mode Exit fullscreen mode
  • Installs the EFS utilities required to mount and manage EFS volumes.
  • yum is the package manager used in Amazon Linux; for Ubuntu, use apt:
  sudo apt update && sudo apt install -y nfs-common
Enter fullscreen mode Exit fullscreen mode

Step 5: Attach the EFS

In the AWS Console:

  1. Navigate to the EFS dashboard.
  2. Select your EFS and click the Attach button.
  3. Choose Mount via DNS (recommended for flexibility and ease of use).

Copy the "Using the NFS Client" Command

The console provides a command similar to the one below. It mounts the EFS using its DNS endpoint.


Step 6: Mount the EFS Using the NFS Client

sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport fs-0f69aa5e27936dc87.efs.ap-northeast-2.amazonaws.com:/ efs
Enter fullscreen mode Exit fullscreen mode

Command Breakdown:

  • mount: Mounts a filesystem.
  • -t nfs4: Specifies the NFS version (NFS v4.1) for compatibility and performance.
  • -o: Specifies mount options:
    • nfsvers=4.1: Uses NFS version 4.1.
    • rsize=1048576, wsize=1048576: Set the read/write buffer sizes to 1 MB for optimal performance.
    • hard: Ensures retries persist if the server becomes unavailable.
    • timeo=600: Sets a timeout of 600 deciseconds (60 seconds) for retries.
    • retrans=2: Limits the number of retries to 2.
    • noresvport: Disables the use of a reserved port.
  • fs-0f69aa5e27936dc87.efs.ap-northeast-2.amazonaws.com:/: The DNS name of the EFS file system.
  • efs: The local directory where the EFS is mounted.

Verification

Check the Mounted Filesystem

df -h
Enter fullscreen mode Exit fullscreen mode
  • Displays the mounted filesystems, showing the EFS under the /efs directory.

Test Write and Read Operations

  1. Create a test file:
   echo "Hello, EFS!" > /efs/testfile.txt
Enter fullscreen mode Exit fullscreen mode
  1. Read the file:
   cat /efs/testfile.txt
Enter fullscreen mode Exit fullscreen mode

Advantages of Mounting via DNS

  • Flexibility: The DNS endpoint adjusts to changes in the EFS configuration.
  • High Availability: Ensures reliable access to the EFS from multiple availability zones.
  • Ease of Use: Simplifies the mounting process without needing static IPs.

Conclusion

By following these steps, you can efficiently mount an AWS EFS to multiple EC2 instances using the DNS method. The shared storage provided by EFS is perfect for distributed systems and applications requiring synchronized access to files across multiple servers.

Top comments (0)