Created
December 7, 2025 22:04
-
-
Save entrptaher/6812194efdf076cb785640d41b6f3fac to your computer and use it in GitHub Desktop.
Interactive ecs shell selection bash script
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 | |
| set -e | |
| # Colors for better readability | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| BLUE='\033[0;34m' | |
| NC='\033[0m' # No Color | |
| # Default region | |
| REGION="us-west-2" | |
| print_header() { | |
| echo -e "\n${BLUE}═══════════════════════════════════════════════════════════${NC}" | |
| echo -e "${BLUE} $1${NC}" | |
| echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}\n" | |
| } | |
| print_step() { | |
| echo -e "${GREEN}▶ $1${NC}" | |
| } | |
| print_error() { | |
| echo -e "${RED}✖ $1${NC}" | |
| } | |
| # Global variable to store selection result | |
| SELECTED_INDEX=0 | |
| select_option() { | |
| local prompt="$1" | |
| shift | |
| local options=("$@") | |
| echo -e "${YELLOW}$prompt${NC}" | |
| echo "" | |
| local i=1 | |
| for opt in "${options[@]}"; do | |
| echo " $i) $opt" | |
| ((i++)) | |
| done | |
| echo "" | |
| while true; do | |
| read -p "Enter selection [1-${#options[@]}]: " selection | |
| if [[ "$selection" =~ ^[0-9]+$ ]] && [ "$selection" -ge 1 ] && [ "$selection" -le "${#options[@]}" ]; then | |
| SELECTED_INDEX=$((selection - 1)) | |
| return 0 | |
| fi | |
| print_error "Invalid selection. Please try again." | |
| done | |
| } | |
| # Step 1: Select AWS Profile | |
| print_header "ECS Interactive Shell" | |
| print_step "Fetching AWS profiles..." | |
| profiles=($(aws configure list-profiles 2>/dev/null)) | |
| if [ ${#profiles[@]} -eq 0 ]; then | |
| print_error "No AWS profiles found. Please configure AWS CLI first." | |
| exit 1 | |
| fi | |
| select_option "Select AWS Profile:" "${profiles[@]}" | |
| PROFILE="${profiles[$SELECTED_INDEX]}" | |
| echo -e "\n${GREEN}✔ Selected profile: ${PROFILE}${NC}" | |
| # Step 2: Select Region (with option to change) | |
| print_header "Region Selection" | |
| regions=("us-west-2" "us-east-1" "us-east-2" "eu-west-1" "ap-southeast-1" "Custom") | |
| select_option "Select AWS Region:" "${regions[@]}" | |
| if [ "${regions[$SELECTED_INDEX]}" == "Custom" ]; then | |
| read -p "Enter custom region: " REGION | |
| else | |
| REGION="${regions[$SELECTED_INDEX]}" | |
| fi | |
| echo -e "\n${GREEN}✔ Selected region: ${REGION}${NC}" | |
| # Step 3: Select ECS Cluster | |
| print_header "ECS Cluster Selection" | |
| print_step "Fetching ECS clusters..." | |
| clusters_json=$(aws ecs list-clusters --profile "$PROFILE" --region "$REGION" --output json) | |
| cluster_arns=($(echo "$clusters_json" | grep -o '"arn:aws:ecs:[^"]*"' | tr -d '"')) | |
| if [ ${#cluster_arns[@]} -eq 0 ]; then | |
| print_error "No ECS clusters found in region $REGION" | |
| exit 1 | |
| fi | |
| # Extract cluster names from ARNs | |
| cluster_names=() | |
| for arn in "${cluster_arns[@]}"; do | |
| name=$(echo "$arn" | sed 's/.*cluster\///') | |
| cluster_names+=("$name") | |
| done | |
| select_option "Select ECS Cluster:" "${cluster_names[@]}" | |
| CLUSTER="${cluster_names[$SELECTED_INDEX]}" | |
| echo -e "\n${GREEN}✔ Selected cluster: ${CLUSTER}${NC}" | |
| # Step 4: Select Service | |
| print_header "Service Selection" | |
| print_step "Fetching services in cluster..." | |
| services_json=$(aws ecs list-services --cluster "$CLUSTER" --profile "$PROFILE" --region "$REGION" --output json) | |
| service_arns=($(echo "$services_json" | grep -o '"arn:aws:ecs:[^"]*"' | tr -d '"')) | |
| if [ ${#service_arns[@]} -eq 0 ]; then | |
| print_error "No services found in cluster $CLUSTER" | |
| exit 1 | |
| fi | |
| # Extract service names from ARNs | |
| service_names=() | |
| for arn in "${service_arns[@]}"; do | |
| name=$(echo "$arn" | sed 's/.*service\/[^/]*\///') | |
| service_names+=("$name") | |
| done | |
| select_option "Select Service:" "${service_names[@]}" | |
| SERVICE="${service_names[$SELECTED_INDEX]}" | |
| echo -e "\n${GREEN}✔ Selected service: ${SERVICE}${NC}" | |
| # Step 5: Select Task | |
| print_header "Task Selection" | |
| print_step "Fetching running tasks..." | |
| tasks_json=$(aws ecs list-tasks \ | |
| --cluster "$CLUSTER" \ | |
| --service-name "$SERVICE" \ | |
| --desired-status RUNNING \ | |
| --profile "$PROFILE" \ | |
| --region "$REGION" \ | |
| --output json) | |
| task_arns=($(echo "$tasks_json" | grep -o '"arn:aws:ecs:[^"]*"' | tr -d '"')) | |
| if [ ${#task_arns[@]} -eq 0 ]; then | |
| print_error "No running tasks found for service $SERVICE" | |
| exit 1 | |
| fi | |
| # Get task IDs for display | |
| task_ids=() | |
| for arn in "${task_arns[@]}"; do | |
| id=$(echo "$arn" | sed 's/.*task\/[^/]*\///') | |
| task_ids+=("$id") | |
| done | |
| if [ ${#task_ids[@]} -eq 1 ]; then | |
| echo -e "${GREEN}✔ Found 1 running task: ${task_ids[0]}${NC}" | |
| TASK_ARN="${task_arns[0]}" | |
| else | |
| select_option "Select Task:" "${task_ids[@]}" | |
| TASK_ARN="${task_arns[$SELECTED_INDEX]}" | |
| echo -e "\n${GREEN}✔ Selected task${NC}" | |
| fi | |
| # Step 6: Select Container | |
| print_header "Container Selection" | |
| print_step "Fetching containers in task..." | |
| task_details=$(aws ecs describe-tasks \ | |
| --cluster "$CLUSTER" \ | |
| --tasks "$TASK_ARN" \ | |
| --profile "$PROFILE" \ | |
| --region "$REGION" \ | |
| --output json) | |
| container_names=($(echo "$task_details" | grep -o '"name": "[^"]*"' | sed 's/"name": "//;s/"$//' | sort -u)) | |
| if [ ${#container_names[@]} -eq 0 ]; then | |
| print_error "No containers found in task" | |
| exit 1 | |
| fi | |
| if [ ${#container_names[@]} -eq 1 ]; then | |
| CONTAINER="${container_names[0]}" | |
| echo -e "${GREEN}✔ Found 1 container: ${CONTAINER}${NC}" | |
| else | |
| select_option "Select Container:" "${container_names[@]}" | |
| CONTAINER="${container_names[$SELECTED_INDEX]}" | |
| echo -e "\n${GREEN}✔ Selected container: ${CONTAINER}${NC}" | |
| fi | |
| # Step 7: Select Shell | |
| print_header "Shell Selection" | |
| shells=("/bin/bash" "/bin/sh" "Custom command") | |
| select_option "Select shell/command:" "${shells[@]}" | |
| if [ "${shells[$SELECTED_INDEX]}" == "Custom command" ]; then | |
| read -p "Enter custom command: " COMMAND | |
| else | |
| COMMAND="${shells[$SELECTED_INDEX]}" | |
| fi | |
| # Summary and Confirmation | |
| print_header "Connection Summary" | |
| echo -e " Profile: ${YELLOW}${PROFILE}${NC}" | |
| echo -e " Region: ${YELLOW}${REGION}${NC}" | |
| echo -e " Cluster: ${YELLOW}${CLUSTER}${NC}" | |
| echo -e " Service: ${YELLOW}${SERVICE}${NC}" | |
| echo -e " Task: ${YELLOW}$(echo $TASK_ARN | sed 's/.*task\/[^/]*\///')${NC}" | |
| echo -e " Container: ${YELLOW}${CONTAINER}${NC}" | |
| echo -e " Command: ${YELLOW}${COMMAND}${NC}" | |
| echo "" | |
| read -p "Connect to container? [Y/n]: " confirm | |
| if [[ "$confirm" =~ ^[Nn]$ ]]; then | |
| echo "Aborted." | |
| exit 0 | |
| fi | |
| # Execute command | |
| print_header "Connecting..." | |
| aws ecs execute-command \ | |
| --cluster "$CLUSTER" \ | |
| --task "$TASK_ARN" \ | |
| --container "$CONTAINER" \ | |
| --interactive \ | |
| --command "$COMMAND" \ | |
| --profile "$PROFILE" \ | |
| --region "$REGION" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment