This article will guide readers to install EFS (Resilient File System) on an EC2 instance. An elastic file system is a managed file system that can be shared among multiple instances in different availability zones. This is especially useful for web applications and container storage.
In the previous article, we discussed how to use cloudformation to create an EFS file system, and the article can be found in the link below.
How to create an AWS EFS file system using CloudFormation
Step 1: Check requirements/prerequisites
Before continuing to mount the EFS file system, users need to meet the following conditions:
- An AWS account.
- Created a user authorized to create resources on the AWS account.
- Created the EFS file system on the AWS account.
- Ensure that the security group of EFS allows the security group of the EC2 instance on port 2049.
Step 2: Install EFS
We can install EFS on an existing EC2 instance. You can also mount it when starting a new EC2 instance. I explain this below.
Mount EFS on an existing EC2 instance
On the Elastic File system console, select the EFS you created.EFS add-on
Click Attach. This will open a page with installation instructions for EFS.Installation Notes
Choose to mount via DNS or mount via IP. If your VPC has disabled DNS hostnames, please select the “Mount via IP” option. Click User Guide for a guide on how to install the installation assistant. amazon-efs-utils package. For example, for a Linux instance, run the following command to install the installation helper.
sudo yum install -y amazon-efs-utils
On the EC2 instance, create a directory named efs.
sudo mkdir efs
EFS will be installed on your EC2 instance according to the figure above. You can confirm by running the following command.
To mount EFS on an Ubuntu instance, follow the instructions below.
Create a directory called efs
Use the following command to install NFS common (installation helper).
sudo apt-get -y install nfs-common
Run the following command to mount EFS. Make sure to replace the commands with EFS-specific installation instructions.
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
Use the following command to confirm that the file system is mounted.
Install on the EC2 instance at startup
When launching an EC2 instance, under “File System” on the “Configure Instance” page, select the file system you created. as follows.
EC2 will start with the file system installed.Mount EFS when EC2 starts
Automatically mount EFS on EC2 instances when rebooting
Use System Manager or SSH to connect to your EC2 instance. Edit the fstab file and add the following commands. To edit the file, use the following command.
sudo vi /etc/fstab
fs-d3e98219.efs.eu-west-1.amazonaws.com:/ usr/bin/efs nfs defaults,_netdev 0 0
Edit the fstab file
N/B: Make sure to use the exact path where the efs directory is located on the fstab file. For this case, my efs is in /usr/bin/efs. Also, make sure to replace the file system identifier with a specific file system.
To test use:
sudo mount -fav
If the fstab file is configured correctly, you should see the following:Test automatic installation
Now you can restart the instance and EFS will be installed automatically.
To uninstall EFS, run the following command:
umount -f /usr/bin/efs
Again, make sure to replace the path of the efs directory with your own specific path.
More AWS guidelines:
Use CloudFormation to set up an AWS RDS MySQL database instance
Use CloudFormation to create an Amazon DocumentDB (MongoDB) database on AWS
Create and configure AWS Application Load Balancer using CloudFormation
Happy Building! ! !