In my previous article , I have explained how to create EFS in aws using AWS console , Refer this article
In this guide , We will learn how to mount the EFS (Elastic File System) in the EC2 Instance.
Before mounting the file system , We need to perform certain actions.
- File system DNS name
- Installing NFS client on the EC2 Instance
Mounting Elastic File System
To mount the file system , We need to get the DNS name of the File systems.
Login to EFS Console , Select the File system , Under File system access , You can find the DNS name. Make a note of it.
We are going to use it and mount it in the EC2 instance.
Login to the existing EC2 Instance , if you dont have one , You can create an EC2 Instance using this article.
Login to the EC2 instance and run the below command to install nfs client packages .
sudo yum install nfs-utils
sudo apt-get install nfs-common
Once the nfs client packages are installed.lets go ahead and mount EFS in the EC2 instance.
To mount a EFS in the EC2 instance , We need to create a directory in the instance.
sudo mkdir /mnt/efs
We are creating a folder efs where we will mount the Elastic File system.
Using the below command We are going to mount EFS in the directory we have created in the EC2 instance.
sudo mount -t nfs -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport mount-target-DNS:/ ~/efs-mount-point
In the above replace command , Replace mount-target-DNS with the actual DNS name of the elastic file system.
For efs-mount-point , Replace with the folder we have created. For eg: /mnt/efs
By running that command , You have now successfully mounted the Elastic File system on the EC2 Instance.
Step 4 – Testing
For testing , Go the folder we have created in the EC2 instance
If you do ls -la in this folder it should be empty.
By default , The root directory of the file system is owned and the files can be written by root user only.
In order to upload files to the EFS , We need to change the permission of the root directory.
sudo chmod go+rw .
We are granting read and write for the group and other users.
You can go ahead a create a folder in this directory which can be shared with other applications or servers.
By this time , We have successfully created and mounted the Elastic File system in the EC2 Instances.
The mount point /mnt/efs we created in the EC2 instance is not permanent.Because If the EC2 instance is rebooted , The file system will be unmounted.
For the permanent mount , You should add an entry for the file system in the /etc/fstab file.
Now we know How to create and mount Elastic File system in the EC2 instance and How we can share files across applications and servers.
Hope you find it helpful. Thanks for reading this article.
Do check out my other publications.