How can I retrieve the list of all databases in an AWS RDS DB instance via API?

Viewed 4858

Is there any way to retrieve the list of all databases (or at least database names) in an AWS RDS DB instance via API or SDK (lang doesn't matter)?

The 'describe-db-instances' action doesn't serve my needs, as it contains only the "name of the initial database of this instance that was provided at create time"...
couldn't find anything in the docs / web
‍♂️

I would like to avoid triggering an SQL query and maximize API usage.
I saw this SO-question already, however it is specific for Boto3 usage, whereas I'm not limited to any specific AWS SDK.

Thank you all!

2 Answers

While you're not limited to boto3, you're limited to what the AWS API offers (and to my knowledge boto3 covers pretty much all of it).

The DescribeDBInstances API-Call returns a list of DBInstance objects.

Each object has the Attribute DBName, which is pretty much the only information exposed about the layout of the database Schema through the AWS API.

DBName

The meaning of this parameter differs according to the database engine you use.

MySQL, MariaDB, SQL Server, PostgreSQL

Contains the name of the initial database of this instance that was provided at create time, if one was specified when the DB instance was created. This same name is returned for the life of the DB instance.

Type: String

Oracle

Contains the Oracle System ID (SID) of the created DB instance. Not shown when the returned parameters do not apply to an Oracle DB instance.

Type: String

That's essentially all you're going to get using the AWS API. If you want more, you need to connect to the instance and use the RDBMS to query that information. RDS as a service doesn't actually manage the schemas on your instance (beyond creating an initial one if you want it to) and doesn't expose them.

While Mark B and Maurice are 100% correct, there's a workaround that might come in handy for others, as the user Optimus replied my question in AWS forum:

It's not possible because each database engine has a different implementation of the database concept, so it's a subject that varies from engine to engine.

What you can do is to use the ExecuteStatement RDS API command https://docs.aws.amazon.com/rdsdataservice/latest/APIReference/API_ExecuteStatement.html to run a select that queries the DB data dictionary for that specific engine for that RDS instance, and then you can get the database names.

It does not avoid to run the SQL, but it is a standardized way to get this info no matters the instance DB engine, as the result output has always the same structure.

For me this solution is not good enough as I don't have the DB instance's Secret-ARN which is required for ExecuteStatement action. However, I thought others may use this workaround.

Thank you all!

Related