Created
June 8, 2025 09:48
-
-
Save ddewaele/1c0b706c485236c017ba9e8630826cd8 to your computer and use it in GitHub Desktop.
ecs-services.sh
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 | |
# Fail early if AWS credentials are missing or invalid | |
if ! aws sts get-caller-identity >/dev/null 2>&1; then | |
echo "❌ AWS credentials not found or invalid. Please configure them using 'aws configure' or your SSO/profile setup." | |
exit 1 | |
fi | |
echo "Fetching ECS clusters and services..." | |
# Arrays | |
SERVICE_LIST=() | |
CLUSTER_LIST=() | |
# Gather all services and remember which cluster they belong to | |
for CLUSTER in $(aws ecs list-clusters --query 'clusterArns' --output text); do | |
SERVICES=$(aws ecs list-services --cluster "$CLUSTER" --query 'serviceArns' --output text) | |
for SERVICE in $SERVICES; do | |
SERVICE_LIST+=("$SERVICE") | |
CLUSTER_LIST+=("$CLUSTER") | |
done | |
done | |
# Show menu | |
echo "" | |
echo "Available ECS services:" | |
for i in "${!SERVICE_LIST[@]}"; do | |
printf "%3d) %s\n" "$i" "${SERVICE_LIST[$i]}" | |
done | |
echo "" | |
read -p "Enter the number of the service you want to inspect: " SELECTION | |
SELECTED_SERVICE="${SERVICE_LIST[$SELECTION]}" | |
SELECTED_CLUSTER="${CLUSTER_LIST[$SELECTION]}" | |
if [[ -z "$SELECTED_SERVICE" || -z "$SELECTED_CLUSTER" ]]; then | |
echo "Invalid selection." | |
exit 1 | |
fi | |
echo "" | |
echo "Selected service: $SELECTED_SERVICE" | |
echo "Cluster: $SELECTED_CLUSTER" | |
# Get the first running task | |
TASK_ARN=$(aws ecs list-tasks --cluster "$SELECTED_CLUSTER" --service-name "$SELECTED_SERVICE" \ | |
--desired-status RUNNING --query 'taskArns[0]' --output text) | |
if [[ "$TASK_ARN" == "None" ]]; then | |
echo "No running tasks found for this service." | |
exit 1 | |
fi | |
# Get ENI ID | |
ENI_ID=$(aws ecs describe-tasks --cluster "$SELECTED_CLUSTER" --tasks "$TASK_ARN" \ | |
--query 'tasks[0].attachments[0].details[?name==`networkInterfaceId`].value' --output text) | |
# Get IP | |
PRIVATE_IP=$(aws ec2 describe-network-interfaces --network-interface-ids "$ENI_ID" \ | |
--query 'NetworkInterfaces[0].PrivateIpAddress' --output text) | |
echo "" | |
echo "Private IP of the first running task: $PRIVATE_IP" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment