Introduction
Amazon Elastic File System (Amazon EFS) provides a simple, serverless elastic file system that lets you share file data without provisioning or managing storage. In this lab, we modify 3 existing EC2 instances to use a shared EFS storage volume instead of duplicated Elastic Block Store volumes. This reduces costs significantly, as we only need to store data in 1 location instead of 3. By the end of this lab, you will understand how to create EFS volumes and attach them to an EC2 instance.
Create EFS File System
Create an EFS Volume
- Navigate to EC2 > Instances (running).
- Click the checkbox next to webserver-01.
- Click the Storage tab and note the 10 GiB volume attached.
- In a new browser tab, navigate to EFS.
- Click Create file system, and set the following values:
- Name: SharedWeb
- Availability and durability: One Zone
- Click Create.
- Once it’s created, click View file system in the top right corner.
- Click the Network tab and wait for the created network to become available.
- Once it’s created, click Manage.
- Under Security groups, remove the currently attached default security group, and open the dropdown menu to select the provided EC2 security group (not the default).
- Click Save.
- Return to the EC2 browser tab.
- Click Security Groups in the left-hand menu.
- Click the checkbox next to that same security group (the one that is not default).
- Click the Inbound rules tab.
- Click Edit inbound rules.
- Click Add rule, and set the following values:
- Type: NFS
- Source: Custom, 0.0.0.0/0
- Click Save rules.
- Click EC2 Dashboard in the left-hand menu.
- Click Instances (running).
- With
webserver-01
selected, click Connect in the top right corner. - Click Connect. This should take you to a new terminal showing your EC2 instance in a new browser tab or window.
Mount the EFS File System and Test It
- In the terminal, list your block devices:
lsblk
- View the data inside the 10 GiB disk mounted to
/data
:ls /data
- Create a mount point or directory to attach our EFS volume:
sudo mkdir /efs
- Return to the AWS EFS console showing the
SharedWeb
file system. - Click Attach.
- Select Mount via IP.
- Copy the command under Using the NFS client: to your clipboard.
- Return to the terminal, and paste in the command.
- Add a slash right before
efs
and press Enter. - View the newly mounted EFS volume:
ls /efs
Nothing will be returned, but that shows us it’s mounted. - List the block devices again:
lsblk
- View the mounts:
mount
- View file system mounts:
df -h
- Move all files from
/data
to the/efs
file system:sudo rsync -rav /data/* /efs
- View the files now in the
/efs
file system:ls /efs
This time, a list should be returned.
Remove Old Data
Remove Data from webserver-01
- Unmount the partition:
sudo umount /data
- Edit the
/etc/fstab
file:sudo nano /etc/fstab
- Remove the line starting with
"UUID="
by placing the cursor at the beginning of the line and pressing Ctrl+K. - In the AWS console, navigate to the EFS tab.
- In the Using the NFS client: section, copy the IP in the command.
- Back in the terminal, paste in the IP you just copied:
<NFS MOUNT IP>:/
- Press the Tab key twice.
- Add the mount point and file system type (
nfs4
), so that the line now looks like this (with a tab after/data
):<NFS MOUNT IP>:/ /data nfs4
- Back on the EFS page of the AWS EFS console, copy the options (the part of the command starting with
nfsvers
and ending withnoresvport
). - In the terminal, press Tab after
nfs4
and add the copied options to the line with two zeroes at the end, so that it now looks like this:<NFS MOUNT IP>:/ /data nfs4 <OPTIONS> 0 0
- Save and exit by pressing Ctrl+X, followed by
Y
and Enter. - Unmount the
/efs
to test if this worked:sudo umount /efs
- View the file systems:
df -h
- Try and mount everything that is not already mounted:
sudo mount -a
- View the file systems again and check if
10.0.0.180:/
is mounted:df -h
You should see the NFS share is now mounted on/data
. - View the contents of
/data
:ls /data
- Navigate back to the AWS console with the Connect to instance EC2 page open.
- Click EC2 in the top left corner.
- Click Volumes.
- Scroll to the right and expand the Attached Instances column to find out which 10 GiB volume is attached to
webserver-01
. - Click the checkbox next to the 10 GiB volume attached to
webserver-01
. - Click Actions > Detach volume.
- Click Detach.
- Once it’s detached, click the checkbox next to the same volume again.
- Click Actions > Delete volume.
- Click Delete.
Remove Data from webserver-02
and webserver-03
- Click Instances in the left-hand menu.
- Click the checkbox next to webserver-02.
- Click Connect.
- Click Connect. This should launch a terminal in a new browser window or tab.
- In the tab with the terminal for
webserver-01
, view the contents of/etc/fstab
:cat /etc/fstab
- Copy the second line (starting with an IP) to your clipboard.
- Return to the terminal you launched for
webserver-02
. - Unmount the
/data
partition:sudo umount /data
- Edit the
/etc/fstab
file:sudo nano /etc/fstab
- Delete the second line using Ctrl+K.
- Paste in the line from your clipboard.
- Align the pasted line with the line above as seen in
webserver-01
. - Save and exit by pressing Ctrl+X, followed by
Y
and Enter. - Mount it:
sudo mount -a
- Check the disk status:
df -h
- Check the contents of
/data
:ls /data
- Return to the window with the Connect to instance EC2 page open.
- Click Instances in the top left.
- Click the checkbox next to webserver-03.
- Click Connect.
- Click Connect. This should launch a terminal in a new browser window or tab.
- Unmount the
/data
partition:sudo umount /data
- Edit the
/etc/fstab
file:sudo nano /etc/fstab
- Delete the second line using Ctrl+K.
- Paste in the line from your clipboard.
- Align the pasted line with the line above as seen in
webserver-01
. - Save and exit by pressing Ctrl+X, followed by
Y
and Enter. - Mount everything that is not already mounted:
sudo mount -a
- Check the disk status:
df -h
- Check the contents of
/data
:ls /data
- Return to the window with the Connect to instance EC2 page open.
- Navigate to EC2 > Volumes.
- Check the boxes for both of the 10 GiB volumes.
- Click Actions > Detach volume.
- Type detach into the box, and then click Detach.
- Once they’re detached, select them again and click Actions > Delete volume.
- Type delete into the box, and then click Delete.