So, you have heard about EFS and wondering what are their use cases are and how to get started with it. No worries, I have got your back, let's get started with it right now!
Amazon Elastic File System (EFS) is a fully managed Network File System (NFS) Service. NFS is a protocol, network file sharing protocol, that defines the way how we can access storage volumes that are across the network.
Assume that you have to serve the same static content via multiple EC2 instances. You would create a separate EBS (Elastic Block Storage) for all of the EC2 instances, mount it on them and then store the same content across them.
This is a perfect way to do this but you are not utilizing other services and your billing is also getting bigger.
Instead what you can do is, create a single NFS file system, mount it across all of the EC2 fleets, and store content in it which would be accessible to all of them.
In this way, you will have consistent content, will be easier to manage, and have a lower bill on the cost side.
How to do it? We will get on that now.
You need to have an AWS account with the permission of EFS as well as of EC2 as we will be testing it on that.
You would need to provide the name for the file system (which is an option, but it's good to assign it a name) and the VPC to which it should be associated as we know it operates over the network. We also need to choose whether it should be regional or One Zone. One Zone will have less availability and durability than regional and it will be mounted within a single subnet only whereas with regional we can have one mount target in each subnet. Of course, it will have high durability and availability percentage than OZ.
A mount target provides an IP to which we can connect to access it.
To keep stuff simple, I have chosen the OZ option and have set the AZ to
But the section we want to have a look at is the Network. It contains the mount target information such as the subnet, AZ, IP address, etc.
Congratulations, You have just created an EFS. Next, we will launch an EC2 instance and mount EFS on it.
I named it
G's EC2 and have chosen
Amazon Linux and all others default to keep things simple. However, we need to make sure that it gets provisioned inside the
us-east-1 AZ as our EFS is provisioned there. we need to allow port 2049 access as NFS operates over it as. For that, we need to move over to the Network section and click on the Edit button
Click on the Subnet dropdown and choose the us-east-1a option
It will look like something like this afterward.
Then, we can rename the security group name to something like EC2-EFS-Access but for the sake of simplicity, I will leave it as default. Now, click on the Add Security group rule button. We can set the new rule type to NFS and source to
Next, we can just click on the Launch Instance button which is at the bottom after which we will be prompted with whether or not to choose a key pair so that we can access it. We can create one, use an existing one if you have, or proceed without it. If you clicked on proceed without it, you still will be able to access it via browser. But I like to operate it over a terminal hence, I will create one
After that, we again need to click on the launch instance and one EC2 instance will be provisioned for us.
Let's wait for a few minutes to complete its status checks. Once that's done, we can move over to our terminal.
ssh -i <PEM_FILE> ec2-user@<IP_ADDRESS>
We need to mount EFS inside a folder so let's create that
sudo mkdir /efs
we just need to change
/efs and press enter.
Woah! It's in a stuck phase, nothing is working. No worries, it's expected because we have forgotten to configure security groups for the mount target itself. We also need to open the
2049 i.e. the NFS port.
Now, we need to clear the default SG and select the launch-wizard-2 which we configured for EC2 just to make it simple. But in real life, you would create a new security group and assign that.
We will now click on save and give try the command again through the terminal.
And, it WORKED this time!!!!!. CONGRATS, You just mounted an EFS to an EC2 instance!!!
We can get more information about it through the below command
mount -t nfs4
Okay, so you have mounted an EFS into a running EC2 instance, but what about its persistence, will it be there once you reboot it? The answer is no, but we can configure it to be there in every boot.
Now, to ensure that the mount persists we will configure the
/etc/fstab file. For more information regarding that, please refer to this page.
For now, we will open the file and enter the following in a new line. The file is again at
<EFS_IP>:/ <MOUNT_PATH> nfs4 <EFS_OPTIONS> 0 0
The content can be replaced from the command that we used to mount it previously. Below is the diagram which breaks the command into the thing that we want.
The final output should look something like this if you are following along, expect the difference in IP Address.
172.31.13.244:/ /efs nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport 0 0
Connect with me @Linkedin