Skip to content

Instantly share code, notes, and snippets.

@francescor
Last active January 29, 2020 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francescor/a4da4f1e65700aa3a56d993efbf8f9d7 to your computer and use it in GitHub Desktop.
Save francescor/a4da4f1e65700aa3a56d993efbf8f9d7 to your computer and use it in GitHub Desktop.
# Mount volume EBS che ospita, in modo permanente, /etc/letsencrypt
# vedi https://www.karelbemelmans.com/2016/11/ec2-userdata-script-that-waits-for-volumes-to-be-properly-attached-before-proceeding/
VOLUME_DEVICE=$1
MOUNT_DIR=$2
VOLUME_ID=$3
AWS_REGION=$4
EC2_INSTANCE_ID=$(curl -s http://instance-data/latest/meta-data/instance-id)
# https://docs.aws.amazon.com/cli/latest/reference/ec2/attach-volume.html
aws ec2 attach-volume --device $VOLUME_DEVICE --instance-id $EC2_INSTANCE_ID --volume-id $VOLUME_ID --region $AWS_REGION
######################################################################
# Volume /dev/sdh (which will get created as /dev/xvdh on Amazon Linux and Centos, too)
# ATTENZIONE: con server T3 /dev/sdh viene montata in /dev/nvme1n1 e non /dev/xvdh
DATA_STATE="unknown"
until [ "$DATA_STATE" == "attached" ]; do
DATA_STATE=$(aws ec2 describe-volumes \
--region $AWS_REGION \
--filters Name=attachment.instance-id,Values=$EC2_INSTANCE_ID Name=attachment.device,Values=$VOLUME_DEVICE --query Volumes[].Attachments[].State \
--output text)
sleep 5
done
# Format /dev/xvdh if it does not contain a partition yet
if [ "$(file -b -s /dev/nvme1n1)" == "data" ]; then
mkfs -t ext4 /dev/nvme1n1
fi
# mounting
mkdir -p $MOUNT_DIR
mount /dev/nvme1n1 $MOUNT_DIR
# Persist the volume in /etc/fstab so it gets mounted again
echo "/dev/nvme1n1 $MOUNT_DIR ext4 defaults,nofail 0 2" >> /etc/fstab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment