Last active
September 9, 2022 20:54
-
-
Save noqcks/6244864a1802d9959b8682f512fcb1e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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