Skip to content

Instantly share code, notes, and snippets.

@noqcks
Last active September 9, 2022 20:54
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 noqcks/6244864a1802d9959b8682f512fcb1e4 to your computer and use it in GitHub Desktop.
Save noqcks/6244864a1802d9959b8682f512fcb1e4 to your computer and use it in GitHub Desktop.
#!/bin/sh
sudo apt-get -y install awscli
export AWS_DEFAULT_REGION=$( curl -s http://169.254.169.254/latest/meta-data/placement/region )
EFS_ID=$( aws ssm get-parameter --name dev_efs_id --output text --query 'Parameter.Value' )
ACCESS_POINT_DATA=$( aws ssm get-parameter --name dev_efs_data_ap --output text --query 'Parameter.Value' )
EFS_MOUNT_AZ=$( aws ssm get-parameter --name dev_efs_az --output text --query 'Parameter.Value' )
IP_ALLOC_ID=$( aws ssm get-parameter --name dev_ip_allocation_id --output text --query 'Parameter.Value' )
SSH_PUBLIC_KEY=$( aws ssm get-parameter --name dev_ssh_key --output text --query 'Parameter.Value' )
INSTANCE_ID=$( curl -s http://169.254.169.254/latest/meta-data/instance-id )
HOME_DIR=/home/ubuntu
echo "Installing Amazon EFS file system utilities..."
sudo apt-get update -y
sudo apt-get -y install git binutils python3-pip awscli
git clone https://github.com/aws/efs-utils /home/ubuntu/efs-utils
cd efs-utils
sudo ./build-deb.sh
sudo apt-get -y install ./build/amazon-efs-utils*deb
sudo rm -rf /home/ubuntu/efs-utils
pip3 -q install botocore
echo "Mount EFS file system into home directory"
mount -t efs -o az=$EFS_MOUNT_AZ,tls,accesspoint=$ACCESS_POINT_DATA $EFS_ID:/ $HOME_DIR
echo "Preparing Bash profile..."
[ ! -f $HOME_DIR/.bashrc ] && {
sudo -u ubuntu cp -r /etc/skel/. $HOME_DIR/
}
echo "Inserting public SSH key into EFS home directory..."
sudo -u ubuntu mkdir $HOME_DIR/.ssh
sudo -u ubuntu chmod 0700 $HOME_DIR/.ssh
sudo -u ubuntu touch $HOME_DIR/.ssh/authorized_keys
grep -q -F "${SSH_PUBLIC_KEY}" $HOME_DIR/.ssh/authorized_keys || {
echo "${SSH_PUBLIC_KEY}" | sudo -u ubuntu tee -a "${SSH_PUBLIC_KEY}" $HOME_DIR/.ssh/authorized_keys
}
echo "Associating Elastic IP..."
aws ec2 associate-address --instance-id $INSTANCE_ID --allocation-id $IP_ALLOC_ID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment