Search⌘ K

Modifying EC2 User Data to Mount EFS

Explore how to modify EC2 user data scripts to automatically mount an Amazon EFS file system on WordPress instances. This lesson guides you through updating launch templates and scripting to ensure persistent and scalable shared storage across your AWS environment.

Now that we’ve copied wp-content files to EFS, we just need to make sure that all our WordPress instances use it automatically.

The following script will do exactly that. Lets go through it:

Shell
# 1. Stop all Wordpress services
gonit stop all
# 2. Install NFS
apt-get update
apt-get -y install nfs-common
# 3. Create the folder for EFS
mkdir /efs
# 4. Add the EFS volume to the mount targets
echo "<file-system-id>.efs.us-east-2.amazonaws.com:/ /efs efs nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport 0 0" >> /etc/fstab
# 5. Mount all file systems
mount -a
# 6. Change the wp-content link to the EFS file system
rm /opt/bitnami/wordpress/wp-content
ln -s /efs/wp-content /opt/bitnami/wordpress/wp-content
# 7. Restart all WordPress services
gonit start all

We can add the instructions to the EC2 user data script so they will run on the first boot of our WordPress instances.

Let’s go through the steps one by one:

  • Line 2: We use gonit stop all to stop all WordPress serivies.
  • Lines 5–6: We install the NFS components.
  • Line 9: We create the /efs folder.
  • Line 12: We set up the mount command for the EFS file system. This command is different from what we did in the last lesson. Here, we add the mount command to the /etc/fstab file, which maintains mount targets permanently. The options are exactly the same as in the last lesson. We’ll need to replace <file-system-id> with the actual EFS file system ID
...