A Cloud Architect Company
mount efs file system on ec2
Amazon Web Services

How to mount Elastic File System (EFS) to Amazon EC2 instance

Introduction

Elastic File System is known as EFS which is a storage type product of aws. EFS  is a scalable storage solution that can be utilized for a variety of applications. We can connect EC2 instances or on-premise servers with EFS.

If you’re using Amazon Linux 2, you can use the Amazon Elastic File System (EFS) to store data in the cloud.

The advantage of using EFS over EBS is we can connect multiple EC2 instances like 1000 servers in a single EFS storage.  But with EBS we can connect to only a single instance.

  • EFS has two types Standard and One zone.
  • Standard storage classes: This type will spawn across multiple availability zones in a region. So this type of storage gives us a high availability and durability.
  • One Zone storage classes: As the name says It will store your data in a single availability zone that you choose. This is a cost saving choice if you want to save additional costs.

Advantages:

  • It is a fully managed service by AWS.
  • Highly available and durable.
  • Low cost. You pay only what you use, like S3.
  • Automatically scale up or down for the storage needs.
  • Can connect multiple EC2 servers with a single File System.

Also Know: AWS Storage Types

Procedure

Setup EC2 Instance

  • Create an EC2 instance in AWS Linux 2 image in AWS console. The Instance must have an SSH connection allowed in its security group.
  • Then connect your Instance via SSH from the local terminal using the IP address of the instance. 
  • Run the following commands to login as a root user and update the system.
sudo su -

yum update

Creating Amazon Elastic File System

Open your AWS console and go to All Services→ Storage→  select EFS under storage (direct link). Then Click “Create file system.”

Elastic File System - EFS

  • Provide a Name for your file system.
  • Select a VPC for EFS. For this tutorial we can select the default VPC.
  • Select the storage class that you want to. As I mentioned in Introduction, the Standard type stores the data across multiple AZs, and the One Zone type stores them in a single AZ.
  • Then click the Create button.

EFS mount with ec2 Create File System

Wait for the File system’s state to be Available. Your file systems page looks like in the picture below.

EFS mount with ec2 File System State Available

Creating Security Group for EFS

Once you complete creating EFS, create the security group of EFS as shown in the below steps.

Navigate to EC2, find Security Groups under the Network & Security. Click it and then click the Create Security Group button.

EFS mount with ec2 Go to Security Group Page

  • Provide a name for your Security group for EFS.
  • Select VPC which one you previously created for your EFS file system. EFS and its Security group must be in the same VPC.
  • Scroll down and under the inbound rules section click the Add rule button, and select the Type NFS. Select source to Anywhere.

EFS mount with ec2 Create Security Group with Inbound Rule

Once added the Inbound rule, scroll down to the bottom of the page and click the Create security group link.

EFS mount with ec2 Create Security Group

Attach the Security group to NFS

Now navigate to your created NFS, Under the General section you will see the Network section.

Choose it and then click the Manage button.

EFS mount with ec2 Manage Networks

You can see our file system is created in all AZs in the region and a default security group attached with all them. These are called mount targets.

EFS mount with ec2 Remove Default Security Group

Remove all of them and add the new security group which you created earlier to all the mount targets.

EFS mount with ec2 Add Security Group

Finally you can see their security group is attached with them.

EFS mount with ec2 Target State Available

Attaching Elastic File System (EFS) to EC2 instance

Now your EFS is ready to be mounted in an EC2 instance. 

Use one of the following method to mount the EFS in your desired folder. For example, let us mount our EFS under /efs-mount-path.

EFS mount with ec2 Mount via DNS Name

In your File system’s General page you can see the Attach button in the top right corner.

Click the button and you can see two options. Either one of them you can use.

Mount via DNS

For the first type we select the Mount via DNS type and under the Using NFS client there is a command for mounting the EFS file system to EC2 instance.

Go to your terminal and type ‘mkdir /efs-mount-path’ command.

Then copy the mount command from that efs page and paste it in your ec2 terminal.

That command is like the one below.

mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport <efs domain name>:/ <your mount point >

EFS mount with ec2 Mount via DNS

Mount via IP Address

Another type is Mount via IP Address.

EFS mount with ec2 Mount via IP

Select the second option and under the NFS Client you can see the command for mount EFS with IP address.

Before that we have to select an availability zone like the picture below.

EFS mount with ec2 Mount via IP Select AZ

Then you are able to see the command with an IP Address. So copy the command and paste it in your instance terminal.

 

mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport <efs Ip address>:/ <your mount point >

Permanent Mounting of EFS

You can permanently mount EFS by adding entries in /etc/fstab

Now add the entries to your /etc/fstab  as shown below.

Open the file in EC2 instance using the following command.

vi /etc/fstab

Now add the following line into the file and save it.

FOR IP ADDRESS

<efs Ip address>:/       <your mount point > nfs4   defaults,_netdev 0  0

FOR DNS NAME

<efs domain name>:/ <your mount point > nfs4   defaults,_netdev 0  0

You have successfully mounted the EFS to your EC2 instance. you can check your output using the ‘df -h’  command from the console.

EFS mount with ec2 Confirm mount point

You have successfully mounted your EFS file system to your EC2 instance. You can mount many EC2 instances with the same single EFS file system using this process. 

Thank you for reading this one. I hope you find something useful. See you in the next article.

Article written by:

Jerin Rathnam is a proficient DevOps engineer who is dedicated to streamlining software development and deployment processes. He has extensive knowledge of cloud infrastructure, containerization, and CI/CD pipelines, which enables him to effectively connect development and operations. Jerin specializes in creating numerous Terraform modules for multi-cloud infrastructure and possesses immense expertise in configuring and managing cloud infrastructure. His profound understanding of containerization, along with his experience in orchestration tools like Docker and Kubernetes, further supports his skills as a valuable DevOps engineer.

Leave a Reply

Your email address will not be published. Required fields are marked *

back to top
advanced-floating-content-close-btn

Contact Us to save your AWS bill by 40%

X