
You can add AWS Elastic Beanstalk configuration files (.ebextensions) to your web application’s source code to configure your environment and customize the AWS resources that it contains. Configuration files are YAML- or JSON-formatted documents with a .config file extension that you place in a folder named .ebextensions and deploy in your application source bundle.
Add following efs-mount.config file into your “.ebextensions” folder for mounting your existing WordPress uploads directory with EFS you created.
efs-mount.config:
container_commands:
1chown:
command: "chown webapp:webapp /wpfiles"
02-cp-wp-content-uploads:
command: cp -r wp-content/uploads /wpfiles
03-rm-wp-content-uploads:
command: "rm -rf wp-content/uploads"
4link:
command: "sudo -u webapp ln -s /wpfiles/uploads wp-content/uploads"
option_settings:
aws:elasticbeanstalk:application:environment:
FILE_SYSTEM_ID: xx-xxxxxx
MOUNT_DIRECTORY: '/wpfiles'
##############################################
#### Do not modify values below this line ####
##############################################
packages:
yum:
amazon-efs-utils: []
commands:
01_mount:
command: "/tmp/mount-efs.sh"
files:
"/tmp/mount-efs.sh":
mode: "000755"
content : |
#!/bin/bash
EFS_MOUNT_DIR=$(/opt/elasticbeanstalk/bin/get-config environment -k MOUNT_DIRECTORY)
EFS_FILE_SYSTEM_ID=$(/opt/elasticbeanstalk/bin/get-config environment -k FILE_SYSTEM_ID)
echo "Mounting EFS filesystem ${EFS_FILE_SYSTEM_ID} to directory ${EFS_MOUNT_DIR} ..."
echo 'Stopping NFS ID Mapper...'
service rpcidmapd status &> /dev/null
if [ $? -ne 0 ] ; then
echo 'rpc.idmapd is already stopped!'
else
service rpcidmapd stop
if [ $? -ne 0 ] ; then
echo 'ERROR: Failed to stop NFS ID Mapper!'
exit 1
fi
fi
echo 'Checking if EFS mount directory exists...'
if [ ! -d ${EFS_MOUNT_DIR} ]; then
echo "Creating directory ${EFS_MOUNT_DIR} ..."
mkdir -p ${EFS_MOUNT_DIR}
if [ $? -ne 0 ]; then
echo 'ERROR: Directory creation failed'
exit 1
fi
else
echo "Directory ${EFS_MOUNT_DIR} already exists"
fi
mountpoint -q ${EFS_MOUNT_DIR}
if [ $? -ne 0 ]; then
echo "mount -t efs -o tls ${EFS_FILE_SYSTEM_ID}:/ ${EFS_MOUNT_DIR}"
mount -t efs -o tls ${EFS_FILE_SYSTEM_ID}:/ ${EFS_MOUNT_DIR}
if [ $? -ne 0 ] ; then
echo 'ERROR: Mount command failed!'
exit 1
fi
chmod 777 ${EFS_MOUNT_DIR}
runuser -l ec2-user -c "touch ${EFS_MOUNT_DIR}/it_works"
if [[ $? -ne 0 ]]; then
echo 'ERROR: Permission Error'
exit 1
else
runuser -l ec2-user -c "rm -f ${EFS_MOUNT_DIR}/it_works"
fi
else
echo "Directory ${EFS_MOUNT_DIR} is already a valid mountpoint"
fi
echo 'EFS mount complete.'
Note: Add EFS file system ID for “FILE_SYSTEM_ID” parameter. Or if you have configured this parameter into Software settings of the Elastic Beanstalk or through EB cli, you can simple remove this hard coded parameter from here.
Once you upload the source code on EB environment, you will see the symlink created for your WP uploads folder. You can check that by accessing your application EC2 server via SSH and using “ln -l” command at appropriate path of your uploads directory.