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.
- 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
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.”
- 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.
Wait for the File system’s state to be Available. Your file systems page looks like in the picture below.
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.
- 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.
Once added the Inbound rule, scroll down to the bottom of the page and click the Create security group link.
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.
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.
Remove all of them and add the new security group which you created earlier to all the mount targets.
Finally you can see their security group is attached with them.
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.
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 >
Mount via IP Address
Another type is Mount via IP Address.
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.
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.
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.
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.