Skip to content

Instantly share code, notes, and snippets.

@Ashex
Last active August 12, 2021 13:45
Show Gist options
  • Save Ashex/7e70b106a33901544de3 to your computer and use it in GitHub Desktop.
Save Ashex/7e70b106a33901544de3 to your computer and use it in GitHub Desktop.
EIP failover
#!/bin/sh
# This script will monitor another HA node and take over an Elastic IP (EIP)
# if communication with the other node fails
# Based off the failover scenario and script available here:
# https://aws.amazon.com/articles/2127188135977316
# High Availability IP variables
# Other node's IP to ping and EIP to swap if other node goes down
HA_Node_IP=
EIP=
#Specify the Secondary Private IP for this node
PRIV_IP=
# Specify the EC2 region that this will be running in
REGION=
# Determine the instance, Allocation, and ENI IDs so we can reassign the EIP to the
# correct ENI. Requires EC2 describe-instances, associate-address, and describe-addresses
# permissions. The following example EC2 Roles policy will authorize these
# commands:
# {
# "Statement": [
# {
# "Action": [
# "ec2:DescribeAddresses",
# "ec2:AssociateAddress",
# "ec2:DescribeInstances"
# ],
# "Effect": "Allow",
# "Resource": "*"
# }
# ]
# }
Instance_ID=`/usr/bin/curl --silent http://169.254.169.254/latest/meta-data/instance-id`
ENI_ID=`/bin/aws ec2 describe-instances --instance-ids $Instance_ID --region eu-central-1|python -c 'import sys, json; print json.load(sys.stdin)["Reservations"][0]["Instances"][0]["NetworkInterfaces"][0]["NetworkInterfaceId"]'`
ALLOC_ID=`/bin/aws ec2 describe-addresses --public-ips $EIP --region eu-central-1| python -c 'import sys, json; print json.load(sys.stdin)["Addresses"][0]["AllocationId"]'`
echo `date` "-- Starting HA monitor"
while [ . ]; do
pingresult=`ping -c 3 -W 1 $HA_Node_IP | grep time= | wc -l`
if [ "$pingresult" == "0" ]; then
echo `date` "-- HA heartbeat failed, taking over EIP"
/bin/aws ec2 associate-address --network-interface-id $ENI_ID --allocation-id $ALLOC_ID --private-ip-address $PRIV_IP --allow-reassociation --region $REGION
pingresult=`ping -c 1 -W 1 $EIP | grep time= | wc -l`
if [ "$pingresult" == "0" ]; then
echo `date` "-- Restarting network"
/sbin/service network restart > /dev/null 2>&1
fi
sleep 60
echo `date` "-- Waiting for $HA_Node_IP to come online before resuming heartbeat"
until ping -c 3 -W 1 $HA_Node_IP > /dev/null 2>&1; do
sleep 60
done
echo `date` "-- $HA_Node_IP now online, resuming heartbeat"
fi
sleep 2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment