Skip to content
Nicholas Duffy

Get All RDS Instances (or Any Resource) By Region and Profile

aws, aws-cli, rds, aws-vault1 min read

I needed to get a list of all RDS instances, but not just in a single region or profile, but across all AWS regions and across all of my AWS profiles.

You can do this with RDS (and any resource really), using the CLI and a couple of loops in bash.

See the comments below for additional information and happy scripting!

1echo "Getting all RDS instances in all regions for all accounts."
2echo "This could take awhile..."
4# This assumes you have multiple profiles setup in your AWS credentials for the CLI.
6PROFILES=(myprofile1 myprofile2 myprofile3)
8# echo the fields that we're extracting from the returned JSON so we have a header in our CSV file.
9echo '"Profile", "Region", "DBInstanceIdentifier", "DBName", "DBInstanceClass", "Engine", "AvailabilityZone", "DBInstanceStatus", "Endpoint".Address, "AllocatedStorage"'
10# Get all of the regions that are enabled on our account.
11for region in $(aws ec2 describe-regions --output text | cut -f4); do
12 for profile in "${PROFILES[@]}"; do
13 # Extract the fields that we want from the response and include our profile and region as arguments so we can get them in the output as well.
14 # Note that I'm using `aws-vault` here and you should too!
15 aws-vault exec "$profile" -- aws rds describe-db-instances --region "$region" | \
16 jq -r --arg PROFILE "$profile" --arg REGION "$region" '.DBInstances[] | [$PROFILE, $REGION, .DBInstanceIdentifier, .DBName, .DBInstanceClass, .Engine, .AvailabilityZone, .DBInstanceStatus, .Endpoint.Address, .AllocatedStorage] | @csv'
17 done