Last active
May 14, 2025 16:26
-
-
Save MichMich/2a661db6fff4b615a745750d2d44271a to your computer and use it in GitHub Desktop.
This script allows you to interactively connect to a running ECS task (in AWS Elastic Container Service) using aws ecs execute-command. It lists running tasks in a specified ECS cluster, shows their details (including uptime), and lets you select one to connect to via a shell.
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
#!/bin/bash | |
# ANSI Colors (Fix: Use \033 instead of \e) | |
RED="\033[31m" | |
GREEN="\033[32m" | |
YELLOW="\033[33m" | |
BLUE="\033[34m" | |
CYAN="\033[36m" | |
WHITE="\033[97m" | |
RESET="\033[0m" | |
# Function to check dependencies | |
check_dependencies() { | |
for cmd in aws jq date; do | |
if ! command -v "$cmd" &> /dev/null; then | |
echo -e "${RED}Error: $cmd is not installed.${RESET}" | |
exit 1 | |
fi | |
done | |
} | |
# Function to normalize timestamp and get uptime | |
get_uptime() { | |
local start_time="$1" | |
if [ -z "$start_time" ] || [ "$start_time" == "null" ]; then | |
echo "" | |
return | |
fi | |
local now=$(date +%s) | |
# Remove milliseconds and timezone offset | |
start_time=$(echo "$start_time" | sed -E 's/\.[0-9]+//; s/(\+|-)[0-9]{2}:[0-9]{2}//') | |
# Convert to seconds since epoch | |
if [[ "$OSTYPE" == "darwin"* ]]; then | |
start_seconds=$(date -j -f "%Y-%m-%dT%H:%M:%S" "$start_time" +%s) | |
else | |
start_seconds=$(date -d "$start_time" +%s) | |
fi | |
local diff=$(( now - start_seconds )) | |
if [ "$diff" -lt 60 ]; then | |
echo "${diff}s" | |
elif [ "$diff" -lt 3600 ]; then | |
echo "$(( diff / 60 ))m" | |
elif [ "$diff" -lt 86400 ]; then | |
echo "$(( diff / 3600 ))h" | |
else | |
echo "$(( diff / 86400 ))d" | |
fi | |
} | |
# Function to list tasks and allow selection | |
select_task() { | |
local cluster_name="$1" | |
# Fetch running task ARNs | |
task_arns=$(aws ecs list-tasks --cluster "$cluster_name" --desired-status RUNNING --query "taskArns[]" --output json | jq -r '.[]') | |
if [ -z "$task_arns" ]; then | |
echo -e "${RED}No running tasks found in cluster '$cluster_name'.${RESET}" | |
exit 1 | |
fi | |
echo -e "${BLUE}Fetching task details...${RESET}" | |
task_details=$(aws ecs describe-tasks --cluster "$cluster_name" --tasks $(echo "$task_arns" | tr '\n' ' ') --query "tasks[*].{taskArn:taskArn, taskId:taskArn, status:lastStatus, startedAt:startedAt, service:group, taskDef:taskDefinitionArn}" --output json) | |
# Table Header | |
echo -e "\n${CYAN}Select a task to connect to:${RESET}" | |
printf "%-3s %-36s %-28s %-28s %-10s %-15s\n" "#" "Task ID" "Service" "Task Definition" "Status" "Uptime" | |
printf "%-3s %-36s %-28s %-28s %-10s %-15s\n" "---" "------------------------------------" "----------------------------" "----------------------------" "--------" "--------" | |
tasks=() | |
while IFS= read -r line; do | |
taskArn=$(echo "$line" | jq -r '.taskArn') | |
taskId=$(echo "$taskArn" | awk -F'/' '{print $NF}') # Extract last part of ARN as task ID | |
status=$(echo "$line" | jq -r '.status') | |
service=$(echo "$line" | jq -r '.service' | sed 's/^service://') # Remove 'service/' prefix | |
startedAt=$(echo "$line" | jq -r '.startedAt') | |
taskDefArn=$(echo "$line" | jq -r '.taskDef') | |
# Extract task definition name and revision | |
taskDef=$(echo "$taskDefArn" | awk -F'/' '{print $NF}') | |
# Compute uptime | |
uptime=$(get_uptime "$startedAt") | |
# Use colors for status | |
case "$status" in | |
"RUNNING") status_color=$GREEN ;; | |
"STOPPED") status_color=$RED ;; | |
"PENDING") status_color=$YELLOW ;; | |
*) status_color=$WHITE ;; | |
esac | |
# Store task ARN | |
tasks+=("$taskArn") | |
# Print row with colors and alignment | |
printf "%-3d ${YELLOW}%-36s${RESET} %-28s %-28s ${status_color}%-10s${RESET} %-15s\n" \ | |
"${#tasks[@]}" "$taskId" "$service" "$taskDef" "$status" "$uptime" | |
done <<< "$(echo "$task_details" | jq -c '.[]')" | |
# User selects task | |
echo -e "\n${CYAN}Enter task number to connect:${RESET} " | |
read -p "> " selection | |
if [[ "$selection" =~ ^[0-9]+$ ]] && [ "$selection" -ge 1 ] && [ "$selection" -le "${#tasks[@]}" ]; then | |
selected_task="${tasks[$((selection-1))]}" | |
echo -e "\n\n${GREEN}Connecting to task: $selected_task...${RESET}\n" | |
aws ecs execute-command --cluster "$cluster_name" --task "$selected_task" --interactive --command "/bin/bash" | |
else | |
echo -e "${RED}Invalid selection.${RESET}" | |
exit 1 | |
fi | |
} | |
# Main script execution | |
if [ $# -ne 1 ]; then | |
echo -e "${RED}Usage: ecs-connect <cluster-name>${RESET}" | |
exit 1 | |
fi | |
check_dependencies | |
select_task "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment