Thursday, May 11, 2017

How to get all your AWS snapshots using one command and AWS CLI




In case there is a need to view all the available snapshots from all of your account, for all of the availability zone using one command, this is how to do it.

create the owner-list.txt file which contain all of your AWS accounts number, each account should be in a line, for example

# cat owner-list.txt
483426017123 
487214417321


then create az-list.txt file, with the following availability zones

# cat az-list.txt
us-east-1 
us-east-2 
us-west-1 
us-west-2 
ca-central-1 
eu-west-1
eu-central-1
eu-west-2 
ap-southeast-1 
ap-southeast-2 
ap-northeast-2 
ap-northeast-1 
ap-south-1 
sa-east-1


now create the script itself


#cat get-snapshot.sh
while read az; do  
echo $az 
while read owner; do 
echo $owner 
aws ec2 describe-snapshots --region $az --owner-ids $owner --output json > $owner.$az.json 
done <owner-list.txt 
done <az-list.txt


the output will be list of json files, for each AZ and account.