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.
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.”
![](https://i0.wp.com/linuxquery.com/wp-content/uploads/2024/05/1.png?resize=640%2C155&ssl=1)
- 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.
![](https://i0.wp.com/linuxquery.com/wp-content/uploads/2024/05/image.png?resize=640%2C549&ssl=1)
Wait for the File system’s state to be Available. Your file systems page looks like in the picture below.
![](https://i0.wp.com/linuxquery.com/wp-content/uploads/2024/05/image-1.png?resize=640%2C156&ssl=1)
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.
![](https://i0.wp.com/linuxquery.com/wp-content/uploads/2024/05/image-2.png?resize=640%2C203&ssl=1)
- 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.
![](https://i0.wp.com/linuxquery.com/wp-content/uploads/2024/05/image-3.png?resize=640%2C490&ssl=1)
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.
![](https://i0.wp.com/linuxquery.com/wp-content/uploads/2024/05/image-4.png?resize=640%2C339&ssl=1)
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
![](https://i0.wp.com/linuxquery.com/wp-content/uploads/2024/05/image-5.png?resize=640%2C397&ssl=1)
Remove all of them and add the new security group which you created earlier to all the mount targets.
![](https://i0.wp.com/linuxquery.com/wp-content/uploads/2024/05/image-6.png?resize=640%2C339&ssl=1)
Finally you can see their security group is attached with them.
![](https://i0.wp.com/linuxquery.com/wp-content/uploads/2024/05/image-7.png?resize=640%2C247&ssl=1)
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.
![](https://i0.wp.com/linuxquery.com/wp-content/uploads/2024/05/image-8.png?resize=640%2C245&ssl=1)
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.
mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport <efs domain name>:/ <your mount point >
![](https://i0.wp.com/linuxquery.com/wp-content/uploads/2024/05/image-9.png?resize=640%2C239&ssl=1)
Mount via IP Address
Another type is Mount via IP Address.
![](https://i0.wp.com/linuxquery.com/wp-content/uploads/2024/05/image-10.png?resize=640%2C251&ssl=1)
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.
![](https://i0.wp.com/linuxquery.com/wp-content/uploads/2024/05/image-11.png?resize=640%2C212&ssl=1)
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.
![](https://i0.wp.com/linuxquery.com/wp-content/uploads/2024/05/image-12.png?resize=640%2C128&ssl=1)
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.