Amazon elastic file system( EFS) can be mounted automatically using user-data while spinning up the Instance. This post aims to show both auto and manual mount of EFS volume.
Before you start mounting the EFS volume, make sure that you have the EFS ID with you and the EFS security group is open on port 2049 for the EC2 IP or subnets.
Automatic method( User data)
Add the script below into the user data section of the EC2 and then create the instance. Remember to replace the value of file_system_id_1 and efs_mount_point_1 with the value of the EFS ID and the data directory where you want to mount the EFS volume.
#cloud-config
package_update: true
package_upgrade: true
runcmd:
- yum install -y amazon-efs-utils
- apt-get -y install amazon-efs-utils
- yum install -y nfs-utils
- apt-get -y install nfs-common
- file_system_id_1=fs-b8094b72
- efs_mount_point_1=/mnt/efs/fs1
- mkdir -p "${efs_mount_point_1}"
- test -f "/sbin/mount.efs" && printf "\n${file_system_id_1}:/ ${efs_mount_point_1} efs iam,tls,_netdev\n" >> /etc/fstab || printf "\n${file_system_id_1}.efs.eu-west-1.amazonaws.com:/ ${efs_mount_point_1} nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport,_netdev 0 0\n" >> /etc/fstab
- test -f "/sbin/mount.efs" && printf "\n[client-info]\nsource=liw\n" >> /etc/amazon/efs/efs-utils.conf
- retryCnt=15; waitTime=30; while true; do mount -a -t efs,nfs4 defaults; if [ $? = 0 ] || [ $retryCnt -lt 1 ]; then echo File system mounted successfully; break; fi; echo File system not available, retrying to mount.; ((retryCnt--)); sleep $waitTime; done;
Note: if you want to add the EFS to the ECS EC2 instance, add the below line at the end of the above script. Replace the
ECS_CLUSTER with your ECS cluster name.
– echo ECS_CLUSTER=your-cluster-name >> /etc/ecs/ecs.config
Manual Method
To mount the EFS volume manually, install the amazon-efs-utils, and use the mount command to point to a directory.
sudo yum install -y amazon-efs-utils
sudo mount -t efs -o tls efs-volume-id:/ /efs
To use IAM authentication while mounting
sudo mount -t efs -o tls,iam efs-volume id:/ /efs