This article shows how to Mount Amazon Elastic File System (EFS) to EC2 on Amazon AWS. For those of you who don’t know what EFS and EC2 are, here’s a one line introduction.
One line introduction to Amazon Elastic File System (EFS) and Amazon EC2
EFS – Elastic File System
– Amazon EFS is a file system then can store petabytes of data and that can be mounted to multiple amazon EC2 machines. The File system is independent of the server.
– Amazon EC2 instances are amazon virtual private machines that you can use as a computer on the cloud.
Steps to mount EFS on EC2
Creating an Elastic File System
Login to Amazon AWS management console and click on EFS. If this is the first time you are using EFS then you should see the welcome screen
Hit on create file system. The next page showw “configure file system access”. chose the default and click “Next Stemp”
Hit Next and Step 2 says Configure optional settings. You can add key-value tags on this page. The tags can be used later for reports or billing breakdown.
The last step is to review and create the File System.
Once you hit create the file system will be created. It will also create mount targets for
Creating The Security Groups For Elastic File System (EFS) and EC2
The next step after creating the EFS is creating the security groups for EFS and EC2. We want to be able to mount an EFS on EC2, which means we are looking at opening up two kinds of firewalls (look at security groups as opening up of firewall ports)
- Opening up the port on EFS so that EC2 can connect to it.
- Opening up the SSH port on EC2 so that we can connect to it.
Lets first look at how to create the security group to allow connection to EFS. Open up the EC2 dashboard and look at security groups section on the left menu.
On the security groups page click on ‘Create Security Group’
You shoud see a create Security group popup. Enter a security group name. We have used ‘EFSSecurityGroup’; Add a description; we choose the default VPC; Add an InBound rule with type as NFS, it should auto populate protocol and port range; We select a source of ‘Anywhere’ from the dropdown but you can limit it to a particular IP; click on create.
Create another security group for the EC2 instances. We use ‘EC2SecurityGroup’ for the name; Add an new inbound rule for SSH and source ‘Anywhere’ or a specific IP if you so choose.
Assigning appropriate groups to the Elastic File System (EFS) and EC2
We have created the groups and now the next step is to add the groups to EFS and EC2. Go to the EFS group and click on ‘Mange File System Access’. Remove the default Security Groups and add the ‘EFSSecurityGroup’ and click on save.
Similarly, assign the ‘EC2SecurityGroup’ to the EC2 instance.
Mounting the Elastic File System (EFS) to EC2
The last step now is to mount the file system to EC2. If you go to the file system dashboard, you should see a link that has instructions to mount the file system. Click on the link to bring up up this window
If you have created the EC2 instance using the Amazon linux then you should have the required tools to mount the file system. If not, follow the instructions on the popup; create a folder called ‘efs’ on the EC2 instance and fire the command specified on step 3 of the popup to mount it. If you copy paste the command directly it should work fine. To permanently mount the EFS add the entry to /etc/fstab file
Once you mount the file system, you can start creating files to it. Also, If you unmount the file system and mount it to a different machine the files are all still there. To unmount the file system type in ‘sudo umount efs/’
That finishes the tutorial on mounting the EFS to EC2.