Skip to content

Instantly share code, notes, and snippets.

@sportebois
Created December 11, 2019 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sportebois/6ffbbe58fb7a9808e630ae5393df3a59 to your computer and use it in GitHub Desktop.
Save sportebois/6ffbbe58fb7a9808e630ae5393df3a59 to your computer and use it in GitHub Desktop.
Dumps a CSV of EC2 instances (no pagination, up to 200 per region)
#!/usr/bin/env bash
set -eu
dumpInstances() {
local readonly region="$1"
local readonly date_ts="$(date +%Y%m%d-%H%M)"
aws ec2 describe-instances \
--max-items 200 \
--region "$region" \
--page-size 200 \
--query 'Reservations[*].Instances[*].[InstanceId,InstanceType,Placement.AvailabilityZone,Tags[?Key==`Name`].Value,Tags[?Key==`Environment`].Value]' \
| jq -r '.[] | .[] | {"id": .[0], "type": .[1], "az": .[2] ,"name": .[3][0], "environment": .[4][0]} | [.id, .type, .az, .name, .environment] | @csv' \
| tee /dev/tty > "ec2-inventory-${region}-${date_ts}.csv"
}
dumpInstances "us-east-1"
dumpInstances "eu-central-1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment