Retrieve RDS endpoint details in Python

In this blog, we are going to see how to retrieve RDS endpoint details by using a database identifier.

We will use the boto3 library to call the RDS API and fetch the database details.

Create RDS boto3 client

We need to import the boto3 library and then initialize the rds client.

import boto3

client = boto3.client("rds")

Make describe db instance call

Once the client is created we can call the describe_db_instances method by passing the database identifier.

DBInstanceIdentifier and Filters are the required fields to make this API call.

response = client.describe_db_instances(
        DBInstanceIdentifier=primary_db_identifier,
        Filters=[
            {
                'Name': 'db-instance-id',
                'Values': [
                    primary_db_identifier
                ]
            },
        ]
    )

Parse response to retrieve endpoint

Once the API call is made we need to parse the returned response to extract the database details. As we are passing a single database identifier we will get only one object in the DBInstances list from which we can extract the endpoint details as below.

response['DBInstances'][0]['Endpoint']

Here is the link to the GitHub repository where you can find the entire code.