AWS cli cheat sheet

List of code tidbits

This page has a random list of code tidbits or bash command line tricks to do stuff with AWS.

List all regions

aws ec2 describe-regions | jq '.Regions|to_entries[]|.value.RegionName' -r

Or, using the built in –query, which is going to transfer less over the wire:

for r in $(aws ec2 describe-regions --query "Regions[*].RegionName" --output text); do echo $r; done

ap-south-1
eu-west-3
eu-west-2
eu-west-1
ap-northeast-2
ap-northeast-1
sa-east-1
ca-central-1
ap-southeast-1
ap-southeast-2
eu-central-1
us-east-1
us-east-2
us-west-1
us-west-2

And you can also filter, like this:

for r in $(aws ec2 describe-regions --query "Regions[*].RegionName" --filter "Name=region-name,Values=us-east-*" --output text); do 
  echo $r; 
done

us-east-1
us-east-2

This is useful if you want to then look for stuff in all regions like this:

for region in $(aws ec2 describe-regions | jq '.Regions|to_entries[]|.value.RegionName' -r); do 
    echo $region; aws ec2 describe-vpcs --region $region
done

Another Example: describe-instances in all US based regions:

for r in $(aws ec2 describe-regions --filter "Name=region-name,Values=us-*" | jq '.Regions|to_entries[]|.value.RegionName' -r); do 
  echo $r; 
  echo "-----"; 
  aws ec2 describe-instances --region $r --query "Reservations[*].Instances[*].[InstanceId,InstanceType]" --output table;
done

us-east-1
-----
-------------------------------------
|         DescribeInstances         |
+----------------------+------------+
|  i-0XXXXa9ae9a62XXXX |  t3.small  |
|  i-0XXXX410773e7XXXX |  t3.small  |
+----------------------+------------+
us-east-2
-----
us-west-1
-----
us-west-2
-----

Recursively Delete Buckets

This isn’t really needed because the AWS Console has a delete bucket button that does all of this for you in a click. But here it is anyway.

delete_buckets.sh