Skip to content
pipefail

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..."
3
4# This assumes you have multiple profiles setup in your AWS credentials for the CLI.
5# https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html
6PROFILES=(myprofile1 myprofile2 myprofile3)
7
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! https://github.com/99designs/aws-vault
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
18done