Before we logon to our EC2 instance there are a couple things that we need to do in the AWS console. The first is to ensure that the DNS hostnames setting is enabled on the VPC where the EFS file system and EC2 instance are in.
The next (optional) step is to create a new security group to assign to our EFS file system. This step can be skipped by assigning the default VPC security group to the instance. This is the security group that we assigned to the EFS file system in the previous guide.
To create the new security group go in to the EC2 console, select Security Groups from the left menu and hit the big blue Create Security Group button at the top. We will just add a single NFS rule and set the source to be the security group that is already assigned to the instance that will mount the EFS file system. Selecting NFS as the Type will pre-populate the Protocol and Port Range fields.
Next, we need to assign the new security group to the EFS file system. To do this, select the file system from the EFS console and then select Manage file system access from the Actions drop down.
Now you can remove the default security group previous added and assign the new group, as shown below.
Mount the File System
That’s all of the prep work done and now we need to logon to our EC2 instance via SSH. If you are using Windows then you will need to download the Putty client. If you are using a linux distribution then you are already good to go.
Before mounting the EFS file system we need to install amazon-efs-utils package mount helper. This can be installed on various Linux distributions and you can find more information on how to do that here. I will just shown you how to install the helper on Amazon Linux 2.
Logon on to your EC2 instance and run the following command to install the helper:
sudo yum install amazon-efs-utils
Now, create a directory under which the file system will be mounted. We’ll create a new directory in root called efs and to do that run the follwowing command:
sudo mkdir /efs
We are almost there. Now we just need to run one more command to mount the file system:
sudo mount -t efs fs-12345678:/ /efs
Replace fs-12345678 with the ID of your file system. Once you have run that, you can run the command df to confirm it was successful. As you can see in the image below we have successfully mounted the EFS filesystem to /efs.
Automatically Mount File System
The file system is now available from your EC2 instance, but this will not persist if the instance reboots. This means we need to get our instance to automatically mount the file system on boot.
To do this, open the file /etc/fstab in an editor and add the following line (replaing fs-12345678 with the ID of your file system):
fs-12345678/ /efs efs defaults,_netdev 0 0
If you want to know what this additional line does you can see more information in the here in the AWS documentation.
Once you have saved your changes your instance will now mount the file system after a reboot.