Created
January 9, 2025 20:51
-
-
Save magickatt/15adda57bf52d8bdee1749829f4a060a to your computer and use it in GitHub Desktop.
Assign a spare Elastic IP by tag to a new EKS node
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
instance_id=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) | |
# Find any unassociated Elastic IPs for this EKS cluster, and associate 1 to this node | |
allocate_eip () { | |
allocation_ids_string=$(aws ec2 describe-addresses --filters Name=tag:cluster,Values=${cluster} --query "Addresses[?NetworkInterfaceId == null ].AllocationId" --output text) | |
allocation_ids=($allocation_ids_string) | |
# Check if there is at least 1 Elastic IP free | |
if [ ${#allocation_ids[@]} -eq 0 ]; then | |
return 1 | |
else | |
allocation_id=${allocation_ids[0]} | |
echo "Try to associate $allocation_id" | |
aws ec2 associate-address --instance-id $instance_id --allocation-id $allocation_id --allow-reassociation} | |
fi | |
} | |
echo "Checking for free Elastic IPs" | |
end=$((SECONDS+300)) | |
while [ $SECONDS -lt $end ]; do | |
if allocate_eip; then | |
echo "Associated Elastic IP $allocation_id" | |
exit 0 | |
else | |
echo "Could not associate Elastic IP, waiting 15 seconds..." | |
sleep 15 | |
fi | |
done | |
echo "Waited 5 minutes to associate Elastic IP, giving up" | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment