Skip to content

Instantly share code, notes, and snippets.

@filipenf
Last active June 10, 2022 13:29
Show Gist options
  • Save filipenf/02a6fba5da2659d34732f18f53a2f7fa to your computer and use it in GitHub Desktop.
Save filipenf/02a6fba5da2659d34732f18f53a2f7fa to your computer and use it in GitHub Desktop.
Auto generate haproxy "peers" based on existing ASG instances
#!/bin/bash
# just a quick-and-dirty solution to update the peers configuration of haproxy upon ASG changes
# instance-id becomes the peer name ( need to use -L<instance_id> on the haproxy command line)
# port 7777 is used for p2p communication
# call this script on crontab and redirect the output to > /etc/haproxy/peers.cfg then include that file into your haproxy config
instance_id=$(curl http://169.254.169.254/latest/meta-data/instance-id)
region=$(curl http://169.254.169.254/latest/meta-data/placement/availability-zone/)
region=${region:0:${#region}-1}
asg_name=$(aws ec2 describe-instances --instance-ids $instance_id --region $region | jq '.Reservations[].Instances[].Tags[] | select(.Key=="aws:autoscaling:groupName") | .Value' | xargs)
instances=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name $asg_name --region $region --query "AutoScalingGroups[].Instances[].InstanceId" --output text | xargs )
private_ips=$(aws ec2 describe-instances --instance-ids $instances --region $region --query 'Reservations[].Instances[][InstanceId,NetworkInterfaces[].PrivateIpAddresses[0].PrivateIpAddress]' --output text | xargs -I{} echo -n "{};")
IFS=";" read -a results <<< "$private_ips"
echo "peers lb_repl"
for (( i=0; i<$(( ${#results[@]} / 2 )); i++ )); do
id=${results[$((i*2))]}
ip=${results[$((1+i*2))]}
echo " peer $id $ip:7777"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment