Learn how to – How To Mount AWS EFS File System on EC2 Instance.
This article takes the reader through mounting an EFS (Elastic File System) on to an EC2 instance. The Elastic File System is a managed file system that can be shared across multiple instances in different availability zones. This can come in handy especially for Web Applications and Container Storage.
In our previous article we discussed on creating an EFS filesystem using cloudformation and the article is available in the link below.
Step 1: Check Requirements/Prerequisites
Before proceeding to mount the EFS file system, the user needs to have:
- An AWS Account.
- Created a user with permissions to create resources on the AWS Account.
- Created an EFS file system on the AWS account.
- Ensure that the security group of the EFS allows the security group of the EC2 instance on port 2049.
Step 2: Mount the EFS
We can mount an EFS on an existing EC2 instance. It can also be mounted when launching a new EC2 Instance. I have explained this below.
Mount EFS on an existing EC2 Instance
On the Elastic File system console, select the EFS you created.
Click attach. This opens a page with mount instructions for the EFS.
Select Mount via DNS or Mount via IP. Should you have a VPC with DNS hostnames disabled, select the mount via IP option. Click User guide to get the guide on how to Install the mount helper. The amazon-efs-utils package. For example, for a Linux instance, run the below command to install the mount helper.
sudo yum install -y amazon-efs-utils
Your installation should look similar to below:
on your EC2 instance create a directory called efs.
sudo mkdir efs
The EFS will be mounted on your EC2 instance as per above image. You can confirm by running the below command.
df -h
To Mount the EFS on an Ubuntu instance, follow the below instructions.
Create a directory called efs
Install NFS common (mount helper) using the below command.
sudo apt update && sudo apt -y install nfs-common
Run the below command to mount the EFS. Make sure to replace the command with your EFS specific mount instruction command.
sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport fs-d3e98219.efs.eu-west-1.amazonaws.com:/ efs
Confirm that the file system has been mounted using the below command.
df -h
Mount on an EC2 Instance at Launch
When Launching an EC2 instance, on the configure instance page, under file systems select the file system you created. See below.
The EC2 will be launched with the file system mounted.
Mount EFS on EC2 Instance Automatically on Reboot
Using System Manager or SSH connect to your EC2 instance. edit the fstab file and add the below command. To edit the file use the command below.
$ sudo vi /etc/fstab
fs-d3e98219.efs.eu-west-1.amazonaws.com:/ usr/bin/efs nfs defaults,_netdev 0 0
See screenshot below:
N/B: Ensure that on the fstab file you use the exact path where your efs directory is. For this case, my efs is in /usr/bin/efs. Also, ensure your replace the file system identity with your specific file system.
To test use:
sudo mount -fav
If the fstab file is configured correctly, you should see the below:
You can now reboot your instance and the EFS will be mounted automatically. To unmount the EFS run the below command:
sudo umount -f /usr/bin/efs
Again, ensure you replace the path to the efs directory with your own specific path.
Important Links
More AWS guides:
- Setup AWS RDS MySQL DB Instance with CloudFormation
- Create Amazon DocumentDB (MongoDB) Database on AWS With CloudFormation
- Create and Configure AWS Application Load Balancer with CloudFormation
Happy Building!!!
Leave a Reply
You must be logged in to post a comment.