0
votes

I'm using an EFS volume on my EC2 instance (Amazon linux AMI). I can mount the volume without issue, if I shell into the server and run something like:

sudo mount -t nfs -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 fs-xxxxxxxxx.efs.ap-southeast-2.amazonaws.com:/ efs

But if I add a shell script inside the user data section of my instance and the boot it, I get nothing, how can I debug it? Is there some logs or something in the filesystem I can look at? I don't get an errors, just no mounted drive. Any help is appreciated.

I'm using the following shell script:

#!/bin/bash
# Make sure all packages are up-to-date
yum update -y

# Make sure that NFS utilities and AWS CLI utilities are available
yum install -y jq nfs-utils python27 python27-pip awscli
pip install --upgrade awscli

# Name of the EFS filesystem (match what was created in EFS)
EFS_FILE_SYSTEM_NAME="xxxx.efs.ap-southeast-2.amazonaws.com"

# Gets the EC2 availability zone for the current ECS instance

EC2_AVAIL_ZONE="ap-southeast-2b"
# Gets the EC2 region for the current ECS instance

EC2_REGION="Asia Pacific (Sydney)"

# Creates the mount-point for the EFS filesystem
DIR_TGT="efs"
mkdir "${DIR_TGT}"

# Get the EFS filesystem ID.
EFS_FILE_SYSTEM_ID="$(/usr/local/bin/aws efs describe-file-systems --region "${EC2_REGION}" | jq '.FileSystems[]' | jq "select(.Name==\"${EFS_FILE_SYSTEM_NAME}\")" | jq -r '.FileSystemId')"

if [ -z "${EFS_FILE_SYSTEM_ID}" ]; then
    echo "ERROR: variable not set" 1> /etc/efssetup.log
    exit
fi

# Create the mount source path
DIR_SRC="${EC2_AVAIL_ZONE}.${EFS_FILE_SYSTEM_ID}.efs.${EC2_REGION}.amazonaws.com"

# Actually mount the EFS filesystem
mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,soft,timeo=600,retrans=2 "${DIR_SRC}:/" "${DIR_TGT}"

# Create a backup of the existing /etc/fstab
cp -p "/etc/fstab" "/etc/fstab.back-$(date +%F)"

# Add the new mount point to /etc/fstab
echo -e "${DIR_SRC}:/ \t\t ${DIR_TGT} \t\t nfs \t\t nfsvers=4.1,rsize=1048576,wsize=1048576,soft,timeo=600,retrans=2 \t\t 0 \t\t 0" | tee -a /etc/fstab
1
DIR_TGT="efs" needs to be an absolute path to an existing directory, e.g. DIR_TGT="/srv/efs" - Michael - sqlbot

1 Answers

2
votes

you can find the UserData logs at cloud-init-output.log

/var/log/cloud-init.log and
/var/log/cloud-init-output.log

Your EC2_REGION should be ap-southeast-2 not Asia Pacific (Sydney), Since you have mentioned the Endpoint name already, you don't need to construct the EFS endpoint.

mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,soft,timeo=600,retrans=2 "${EFS_FILE_SYSTEM_NAME}:/" "${DIR_TGT}"

If you want to construct it, use the following EFS DNS Endpoint convention:

file-system-id.efs.aws-region.amazonaws.com

So, it should be

DIR_SRC="${EFS_FILE_SYSTEM_ID}.efs.${EC2_REGION}.amazonaws.com"