Created
July 22, 2019 18:18
-
-
Save sportebois/e87e337602584b72495de294ae6f00e0 to your computer and use it in GitHub Desktop.
List EC2 instances and create CSV files with the desired fields (AZ, Tag, ...)
This file contains 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
#!/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