[AWS] Mount EFS on EC2 ubuntu 18.04
When the file is not required to be stored permanently but big, S3 might not be the best choice. I recently had to work on a video-related project that requires downloading and analyzing but doesn’t need to be stored after analysis.
In our case, the key requirement was accessing files without using S3 over the different EC2 instances.
Therefore, we decided to use EFS as a Network File System (NFS).
OS: ubuntu 18.04
1. Launch EFS
From AWS Management Console, find the EFS.
Click Create file system button.
Give any name.
Done!
2. Launch an EC2 instance
Since you are here to work on EFS, I assume you know how to launch the EC2 instance, so I will skip the explanation on this part. Just launch the instance with default setting.
We will work on security group setting later.
3. Setup for EFS from EC2
Install relevant package using following command
sudo apt-get update
git clone https://github.com/aws/efs-utils
sudo apt-get -y install binutils
cd efs-utils
./build-deb.sh
sudo apt-get -y install ./build/amazon-efs-utils*deb
Make a folder that EFS will be associated
sudo mkdir /mnt/efs
sudo chmod 775 /mnt/efs
sudo chown -R $USER /mnt/efs
4. Create a Role for EC2
They don’t have EFS related policy by default, so let’s make one.
Choose EFS as a service, set all the actions; finally, choose the EFS you just made as a resource.
Name the policy however you want.
Back to the role-related page, and choose the policy that you just made.
Create!
5. Additional setting for EC2
We have to work on two things additionally.
First, we need to assign the role, and we need to add one more security group to the EC2 instance.
Now, we add one more security group which is default.
6. Mount the EFS
Okay, all the relevant setting that we can do from AWS console is done. Now we will back to our EC2 terminal.
First, mount the EFS using following command.
fs-xxxxxxxx is the id of your EFS, you can find it from your EFS page.
sudo mount -t efs fs-xxxxxxxx:/ /mnt/efs
You can see your EFS if you type df -h.
Lastly, make it automatically mount when you reboot the instance.
echo "fs-xxxxxxxx.efs.us-east-1.amazonaws.com:/ /mnt/efs efs _netdev,tls,iam 0 0" | sudo tee -a /etc/fstab
Let’s see if the EFS can be found after rebooting.
For some reasons, the address become 127.0.0.1, however, it seems working normally.
Happy coding!
Several settings are not optimal, such as a security group. You can modify it from the network tab in EFS and assign your own security group.
References
- https://docs.aws.amazon.com/efs/latest/ug/whatisefs.html