Skip to content

Instantly share code, notes, and snippets.

@scarolan
Created January 29, 2018 16:04
Show Gist options
  • Save scarolan/f226c22c9f38a75ef8f1dd3327e5328f to your computer and use it in GitHub Desktop.
Save scarolan/f226c22c9f38a75ef8f1dd3327e5328f to your computer and use it in GitHub Desktop.
List all AWS instances in all regions
#!/bin/sh
getRegions() {
aws ec2 describe-regions --output text --query 'Regions[*].RegionName'
}
getInstances() {
REGION=$1
if [ "$#" -ne 1 ]; then
echo "USAGE: getInstances us-west-2"
exit 1
fi
aws ec2 describe-instances --region $REGION --output text --query 'Reservations[*].Instances[*].[InstanceId, InstanceType, ImageId, State.Name, LaunchTime, Placement.AvailabilityZone, Placement.Tenancy, PrivateIpAddress, PrivateDnsName, PublicDnsName, [Tags[?Key==`Name`].Value] [0][0], [Tags[?Key==`sleep-at-night`].Value] [0][0], [Tags[?Key==`TTL`].Value] [0][0], [Tags[?Key==`owner`].Value] [0][0] ]'
}
for region in $(getRegions); do
getInstances $region
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment