Skip to content

Instantly share code, notes, and snippets.

@lucapericlp
Last active July 27, 2025 17:57
Show Gist options
  • Save lucapericlp/6101d62a3c47f0415b3d051eda9480aa to your computer and use it in GitHub Desktop.
Save lucapericlp/6101d62a3c47f0415b3d051eda9480aa to your computer and use it in GitHub Desktop.
#!/bin/bash
# A script to terminate an EC2 instance and remove its local SSH config entry.
# --- Configuration ---
SSH_CONFIG_FILE="$HOME/.ssh/config"
# Exit script if any command fails.
set -e
# --- 1. Get SSH Alias from Command Line ---
if [ -z "$1" ]; then
echo "❌ Error: No SSH alias provided."
echo "Usage: $0 <ssh-alias>"
exit 1
fi
SSH_ALIAS="$1"
# --- 2. Find Instance ID from SSH Config ---
echo "πŸ”Ž Searching for '$SSH_ALIAS' in $SSH_CONFIG_FILE..."
# Find the line number of the "Host" directive for the given alias.
# We use `head -n 1` to ensure we only act on the first match if there are duplicates.
HOST_LINE_NUM=$(grep -n "Host $SSH_ALIAS" "$SSH_CONFIG_FILE" | head -n 1 | cut -d: -f1)
if [ -z "$HOST_LINE_NUM" ]; then
echo "❌ Error: Could not find an SSH entry for alias '$SSH_ALIAS' in $SSH_CONFIG_FILE."
exit 1
fi
# The launch script places a comment with the Instance ID on the line directly above the "Host" line.
COMMENT_LINE_NUM=$((HOST_LINE_NUM - 1))
# Extract the Instance ID from that comment line using sed and awk.
INSTANCE_ID=$(sed -n "${COMMENT_LINE_NUM}p" "$SSH_CONFIG_FILE" | awk '{print $6}')
# Validate that we found a plausible instance ID.
if [[ -z "$INSTANCE_ID" || ! "$INSTANCE_ID" =~ ^i- ]]; then
echo "❌ Error: Could not parse a valid Instance ID for alias '$SSH_ALIAS'."
echo "Please check the formatting in $SSH_CONFIG_FILE around line $HOST_LINE_NUM."
exit 1
fi
echo "βœ… Found Instance ID: $INSTANCE_ID"
# --- 3. Confirm and Terminate Instance ---
read -p "Are you sure you want to terminate instance $INSTANCE_ID? [y/N] " confirm
# This regex checks for 'y' or 'Y'.
if [[ ! "$confirm" =~ ^[yY] ]]; then
echo "Termination cancelled."
exit 0
fi
echo "πŸ—‘οΈ Terminating instance $INSTANCE_ID..."
# Redirect stdout to /dev/null to keep the output clean.
aws ec2 terminate-instances --instance-ids "$INSTANCE_ID" --output text > /dev/null
echo "⏳ Waiting for instance to fully terminate..."
aws ec2 wait instance-terminated --instance-ids "$INSTANCE_ID"
echo "βœ… Instance terminated."
# --- 4. Clean up SSH Config ---
echo "🧹 Cleaning up SSH config entry..."
# The block created by the launch script is 10 lines total, plus a preceding blank line.
# This makes it hard to remove robustly without line numbers.
# The block starts with a blank line, then a comment, then the "Host" line.
# So, the block to delete starts 2 lines *before* our HOST_LINE_NUM.
START_DELETE=$((HOST_LINE_NUM - 2))
# The block ends 7 lines *after* our HOST_LINE_NUM.
END_DELETE=$((HOST_LINE_NUM + 7))
# Use sed to delete the line range. The `-i.bak` option creates a backup and
# is compatible with both Linux (GNU) and macOS (BSD) versions of sed.
sed -i.bak "${START_DELETE},${END_DELETE}d" "$SSH_CONFIG_FILE"
echo "βœ… SSH config entry removed. Original config saved to ${SSH_CONFIG_FILE}.bak"
# --- 5. Final Output ---
echo "--------------------------------------------------"
echo "βœ… Success! Instance '$INSTANCE_ID' has been terminated and config cleaned up."
echo "--------------------------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment