Skip to content

Instantly share code, notes, and snippets.

@robertdown
Forked from brienw/ec2_instance_user_data.sh
Last active July 30, 2022 17:03
Show Gist options
  • Save robertdown/3ea6cf4c3b6c70765227e0cb5cb7080e to your computer and use it in GitHub Desktop.
Save robertdown/3ea6cf4c3b6c70765227e0cb5cb7080e to your computer and use it in GitHub Desktop.
#!/bin/bash
echo ECS_CLUSTER=${CLUSTER_NAME} > /etc/ecs/ecs.config
INSTANCE=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
hostname $INSTANCE
sed -i.bak "s/\\(HOSTNAME=\\).*/\\1$INSTANCE/" /etc/sysconfig/network
# https://aws.amazon.com/blogs/compute/using-amazon-efs-to-persist-data-from-amazon-ecs-containers/
yum -y install jq
if ! rpm -qa | grep -qw nfs-utils; then
yum -y install nfs-utils
fi
if ! rpm -qa | grep -qw python2; then
yum -y install python2
fi
yum -y install python2-pip
pip install awscli
EC2_AVAIL_ZONE=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone`
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"
EFS_NAME=${EFS_NAME}
EFS_FILE_SYSTEM_ID=`/usr/local/bin/aws efs describe-file-systems --region $EC2_REGION | jq '.FileSystems[]' | jq "select(.Name==\"${EFS_NAME}\")" | jq -r '.FileSystemId'`
DIR_SRC=$EC2_AVAIL_ZONE.$EFS_FILE_SYSTEM_ID.efs.$EC2_REGION.amazonaws.com
DIR_TGT=/mnt/efs
mkdir -p ${DIR_TGT}
chown 9999:9999 ${DIR_TGT}
mount -t nfs4 $DIR_SRC:/ $DIR_TGT
cp -p /etc/fstab /etc/fstab.back-$(date +%F)
echo -e "$DIR_SRC:/ \t\t $DIR_TGT \t\t nfs \t\t defaults \t\t 0 \t\t 0" | tee -a /etc/fstab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment