aws:script: switch_server_behind_elastic_ip.sh

#!/bin/bash

INSTANCE_ID_1=i-050f5b01xxxxxxxxx
INSTANCE_ID_2=i-080f6d36xxxxxxxxx

ALLOCATION_ID=eipalloc-033b9381xxxxxxx

ASSOCIATION_ID=$(aws ec2 describe-addresses --query "Addresses[*].AssociationId" --filters "Name=allocation-id,Values=$ALLOCATION_ID" --output text)

aws ec2 disassociate-address --association-id $ASSOCIATION_ID

if [ $1 -eq 1 ]
then
aws ec2 associate-address --instance-id $INSTANCE_ID_1 --allocation-id $ALLOCATION_ID
fi

if [ $1 -eq 2 ]
then
aws ec2 associate-address --instance-id $INSTANCE_ID_2 --allocation-id $ALLOCATION_ID
fi