Skip to content

Instantly share code, notes, and snippets.

@jwineinger
Created July 26, 2018 20:37
Show Gist options
  • Save jwineinger/f5bf91d26c8c41305b107b3ec4a88e98 to your computer and use it in GitHub Desktop.
Save jwineinger/f5bf91d26c8c41305b107b3ec4a88e98 to your computer and use it in GitHub Desktop.
Count ECS tasks per AZ for a service on a cluster
#!/bin/bash
#
# Ouptuts the number of tasks running per AZ for a given service on a given ECS cluster
#
#
# Boilerplate to setup a temp dir since we save some files for intermediate processing
#
set -e -o pipefail
# the directory of the script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# the temp directory used, within $DIR
# omit the -p parameter to create a temporal directory in the default location
WORK_DIR=$(mktemp -d)
# check if tmp dir was created
if [[ ! "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then
echo "Could not create temp dir"
exit 1
fi
# deletes the temp directory
function cleanup {
rm -rf "$WORK_DIR"
#echo "Deleted temp working directory $WORK_DIR"
}
# register the cleanup function to be called on the EXIT signal
trap cleanup EXIT
#
# End setup
#
#
# Begin script body
#
CLUSTER=ecs-cluster-name
SERVICE=ecs-service-name
aws ecs list-tasks --cluster $CLUSTER --service-name $SERVICE > tasks.json
aws ecs describe-tasks \
--cluster $CLUSTER \
--tasks $(cat tasks.json | jq -r '.taskArns | join(" ")') \
--query 'tasks[].containerInstanceArn' \
> container-instances.json
aws ecs describe-container-instances \
--cluster $CLUSTER \
--container-instances $(cat container-instances.json | jq -r '. | join(" ")') \
--query 'containerInstances[].attributes[?name==`ecs.availability-zone`]' \
> container-instance-azs.json
cat container-instance-azs.json | jq -r '.[][]| .value ' | sort | uniq -c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment