Last active
February 23, 2017 22:40
-
-
Save aerostitch/9e9f5bfa5ba326bd19a3 to your computer and use it in GitHub Desktop.
Little script that creates volumes, tag them and attach the volumes to several instances
This file contains hidden or 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
# Little script that creates volumes, tag them and attach the volumes to several instances | |
for i in {1..4} ; do | |
for srv in db0$i db0$i ; do | |
srvid=$(aws ec2 describe-instances --filters "Name=tag:hostname,Values=${srv}" 'Name=vpc-id,Values=vpc-123456' --output text --query 'Reservations[].Instances[].InstanceId') | |
for dev in f g ; do | |
volid=$(aws ec2 create-volume --size 1024 --availability-zone us-east-1d --volume-type standard --output json | jq '.VolumeId' | tr -d '"') | |
aws ec2 create-tags --resources ${volid} --tags Key=hostname,Value=${srv} Key=vpc-id,Value=vpc-123456 Key=device,Value=/dev/sd${dev} | |
while true ; do | |
status=$(aws ec2 describe-volumes --volume-id ${volid} --query 'Volumes[].State' --output text) | |
if [ "${status}" = "available" ] ; then | |
break | |
else | |
echo "Volume ${volid} not ready yet (${status}). Waiting 10 seconds and retrying." | |
sleep 10 | |
fi | |
done | |
echo "Attaching volume ${volid} to instance ${srvid} (${srv}) as device /dev/sd${dev}" | |
aws ec2 attach-volume --volume-id ${volid} --instance-id ${srvid} --device /dev/sd${dev} | |
done | |
done | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment