Skip to content

Instantly share code, notes, and snippets.

@andromedarabbit
Last active June 3, 2021 01:54
Show Gist options
  • Save andromedarabbit/375f82afa0ca08180e7d5f95109d87aa to your computer and use it in GitHub Desktop.
Save andromedarabbit/375f82afa0ca08180e7d5f95109d87aa to your computer and use it in GitHub Desktop.
Get a report on How EC2 instances are deployed across multiple AZs
#!/bin/bash
type csvsql || brew instal csvkit
type jq || brew install jq
type aws || brew intall awscli
TABLE_NAME=tmp
TMP_FILE=${TABLE_NAME}.csv
OUTPUT_FILE="output.csv"
truncate -s 0 "${OUTPUT_FILE}"
echo "Name AZ" > "${TMP_FILE}"
aws ec2 describe-instances | jq -r '.Reservations[].Instances[] | select(.State .Name == "running") | [ (.Tags[]|select(.Key=="Name")|.Value), .Placement .AvailabilityZone] | @tsv' >> "${TMP_FILE}"
csvsql --query "select name, az, count(*) as cnt from '${TABLE_NAME}' group by name, az" "${TMP_FILE}" > "${OUTPUT_FILE}"
echo -e "The followings are the first 3 x 3 records of the result:"
echo -e "\n"
head -n 3 "${OUTPUT_FILE}" | cut -d$'\t' -f1-3
echo -e "\n"
echo -e "Open ${OUTPUT_FILE} to read all the records you wanted"
echo -e "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment