Skip to content

Instantly share code, notes, and snippets.

@thebirk
Created February 25, 2026 13:23
Show Gist options
  • Select an option

  • Save thebirk/101ad7b4f33d3a5fa4ac001b7985e201 to your computer and use it in GitHub Desktop.

Select an option

Save thebirk/101ad7b4f33d3a5fa4ac001b7985e201 to your computer and use it in GitHub Desktop.
aws-exec
#!/usr/bin/env bash
# Ensure required dependencies are installed
for cmd in aws jq fzf column; do
if ! command -v "$cmd" &> /dev/null; then
echo "Error: '$cmd' is not installed or not in your PATH."
exit 1
fi
done
echo "Fetching ECS clusters and tasks (this may take a moment)..."
# Create a temporary file to store our list
TASK_LIST=$(mktemp)
# 1. Get all clusters
CLUSTERS=$(aws ecs list-clusters --query 'clusterArns' --output text)
# 2. Iterate through clusters to find tasks
for CLUSTER in $CLUSTERS; do
# Get all task ARNs for the current cluster
TASKS=$(aws ecs list-tasks --cluster "$CLUSTER" --query 'taskArns' --output text)
if [ -n "$TASKS" ]; then
# The describe-tasks API accepts a maximum of 100 tasks per call.
# We use xargs to chunk the tasks into groups of 100.
echo "$TASKS" | tr '\t' '\n' | xargs -n 100 | while read -r TASK_CHUNK; do
if [ -n "$TASK_CHUNK" ]; then
# Query the task details and format them as: ClusterName TaskARN ExecEnabled
aws ecs describe-tasks --cluster "$CLUSTER" --tasks $TASK_CHUNK --output json | \
jq -r '.tasks[] | "\(.clusterArn | split("/")[-1]) \(.taskArn) \(.enableExecuteCommand)"' >> "$TASK_LIST"
fi
done
fi
done
# Check if we found any tasks
if [ ! -s "$TASK_LIST" ]; then
echo "No tasks found across any clusters."
rm -f "$TASK_LIST"
exit 1
fi
# 3. Present the list to the user via fzf
# We use `column -t` to align the fields neatly.
SELECTED=$(cat "$TASK_LIST" | column -t | fzf \
--prompt="Select ECS Task > " \
--header=$'CLUSTER_NAME TASK_ARN EXEC_ENABLED\n------------------------------------------------------------------------------------------------------------')
# Clean up the temp file
rm -f "$TASK_LIST"
# Exit if the user pressed ESC or Ctrl+C
if [ -z "$SELECTED" ]; then
echo "No task selected. Exiting."
exit 0
fi
# 4. Extract the relevant fields from the user's selection
SELECTED_CLUSTER=$(echo "$SELECTED" | awk '{print $1}')
SELECTED_TASK_ARN=$(echo "$SELECTED" | awk '{print $2}')
EXEC_ENABLED=$(echo "$SELECTED" | awk '{print $3}')
# Warn the user if execute-command is not enabled on the container
if [ "$EXEC_ENABLED" != "true" ]; then
echo "⚠️ Warning: execute-command is NOT enabled for this task."
echo "The connection will likely fail unless it was enabled recently."
read -p "Do you want to proceed anyway? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
# 5. Execute the command
echo "Connecting to task: $SELECTED_TASK_ARN in cluster: $SELECTED_CLUSTER..."
aws ecs execute-command \
--cluster "$SELECTED_CLUSTER" \
--task "$SELECTED_TASK_ARN" \
--command "/bin/bash" \
--interactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment