Skip to content

Instantly share code, notes, and snippets.

@sators
Last active October 13, 2018 23:12
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 sators/111faf88ba7bdb080f804eea701c4ed6 to your computer and use it in GitHub Desktop.
Save sators/111faf88ba7bdb080f804eea701c4ed6 to your computer and use it in GitHub Desktop.
Bash script to join a rabbitmq cluster of the same AWS autoscaling group
#!/bin/bash
wget --quiet http://s3.amazonaws.com/ec2metadata/ec2-metadata
sudo chmod u+x ec2-metadata
INSTANCE_ID=$(./ec2-metadata | grep instance-id | awk 'NR==1{print $2}')
AG_NAME=$(aws autoscaling describe-auto-scaling-instances --instance-ids ${INSTANCE_ID} --query AutoScalingInstances[].AutoScalingGroupName --output text)
echo ===================================
echo Searching for cluster in ${AG_NAME}
echo ===================================
echo Local Instance ID: ${INSTANCE_ID}
for ID in $(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names ${AG_NAME} --query AutoScalingGroups[].Instances[].InstanceId --output text);
do
echo Found cluster node ${ID}
if [ "${ID}" == ${INSTANCE_ID} ] ; then
continue;
fi
DNS=$(aws ec2 describe-instances --instance-ids $ID --query Reservations[].Instances[].PrivateDnsName --output text)
DNS=(${DNS//./ })
echo Joining cluster ${ID} at rabbit@${DNS[0]}
rabbitmqctl stop_app
rabbitmqctl join_cluster rabbit@${DNS[0]}
rabbitmqctl start_app
exit 1
done
@sators
Copy link
Author

sators commented Oct 13, 2018

Note this is expecting your default region for the aws cli to already be set:

REGION=`/usr/bin/ec2metadata | grep "^availability-zone:" | awk '{print substr($2, 1, length($2)-1)}'`
aws configure set default.region $REGION

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment