Last active
August 6, 2024 01:54
-
-
Save tnm/56397060defe658618efc6f6a0affc9d to your computer and use it in GitHub Desktop.
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 | |
| # Function to prompt for input with default value | |
| prompt_input() { | |
| local prompt="$1" | |
| local variable="$2" | |
| local default="$3" | |
| if [ -n "$default" ]; then | |
| read -p "$prompt [$default]: " $variable | |
| eval $variable="\${$variable:-$default}" | |
| else | |
| read -p "$prompt: " $variable | |
| fi | |
| } | |
| # Set default values | |
| DEFAULT_REGION="us-west-2" | |
| DEFAULT_CLUSTER="your-default-cluster-name" | |
| # Prompt for region and cluster | |
| prompt_input "Enter the AWS region" region "$DEFAULT_REGION" | |
| prompt_input "Enter the ECS cluster name" cluster_name "$DEFAULT_CLUSTER" | |
| # List available task names | |
| echo "Fetching available tasks..." | |
| task_names=$(aws ecs list-task-definitions --region "$region" --query 'taskDefinitionArns[*]' --output text | xargs -n 1 basename | sort | uniq) | |
| if [ -z "$task_names" ]; then | |
| echo "No task definitions found in the region." | |
| exit 1 | |
| fi | |
| echo "Available tasks:" | |
| echo "$task_names" | nl | |
| # Prompt for task name | |
| read -p "Enter the task number (or leave blank to use the first running task): " task_number | |
| if [ -n "$task_number" ]; then | |
| full_task_name=$(echo "$task_names" | sed -n "${task_number}p") | |
| echo "Selected task: $full_task_name" | |
| # Extract the family name (remove the version number) | |
| task_family=$(echo "$full_task_name" | cut -d':' -f1) | |
| echo "Fetching the specified task..." | |
| task_arn=$(aws ecs list-tasks --cluster "$cluster_name" --family "$task_family" --desired-status RUNNING --region "$region" --query 'taskArns[0]' --output text) | |
| else | |
| echo "Fetching the first running task..." | |
| task_arn=$(aws ecs list-tasks --cluster "$cluster_name" --desired-status RUNNING --region "$region" --query 'taskArns[0]' --output text) | |
| fi | |
| if [ -z "$task_arn" ] || [ "$task_arn" == "None" ]; then | |
| echo "No running tasks found in the cluster." | |
| exit 1 | |
| fi | |
| # Get the first container name from the task | |
| echo "Fetching container information..." | |
| container=$(aws ecs describe-tasks --cluster "$cluster_name" --tasks "$task_arn" --region "$region" --query 'tasks[0].containers[0].name' --output text) | |
| if [ -z "$container" ] || [ "$container" == "None" ]; then | |
| echo "No containers found in the task." | |
| exit 1 | |
| fi | |
| # Display the collected information | |
| echo | |
| echo "Will connect to the following:" | |
| echo | |
| echo "Region: $region" | |
| echo "Cluster Name: $cluster_name" | |
| echo "Task ARN: $task_arn" | |
| echo "Container: $container" | |
| echo | |
| # Confirm execution | |
| read -p "Do you want to connect? (y/n): " confirm | |
| if [[ $confirm == [Yy]* ]]; then | |
| # Execute the AWS ECS command | |
| aws ecs execute-command \ | |
| --region "$region" \ | |
| --cluster "$cluster_name" \ | |
| --task "$task_arn" \ | |
| --container "$container" \ | |
| --command "sh" \ | |
| --interactive | |
| else | |
| echo "Command execution cancelled." | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment