Anar Sultanov
Anar Sultanov

Reputation: 3416

How to get host for RDS instance with boto3

I need to get host for RDS instance. I tried to do it like this:

import boto3

region = 'eu-west-1'
db_instance = 'db-instance-identifier'

def lambda_handler(event, context):
    source = boto3.client('rds', region_name=region)
    try:
        instances = source.describe_db_instances(DBInstanceIdentifier=db_instance)
        rds_host = instances[0].endpoint.address
    except Exception as e:
        raise e

Perhaps you can suggest what might be the problem. Thank you in advance!

Upvotes: 9

Views: 15043

Answers (2)

Javeed Shakeel
Javeed Shakeel

Reputation: 3437

This Will Fetch list of all RDS instances with DB identifier,DB Engine,DB size, DB type

#!/usr/bin/env python
import boto3
client = boto3.client('rds')
response = client.describe_db_instances()
for db_instance in response['DBInstances']:
        db_instance_name = db_instance['DBInstanceIdentifier']
        db_type = db_instance['DBInstanceClass']
        db_storage = db_instance['AllocatedStorage']
        db_engine =  db_instance['Engine']
        print (db_instance_name,",",db_type,",",db_storage,",",db_engine)

Upvotes: 2

hjpotter92
hjpotter92

Reputation: 80657

Based on the boto3 docs for describe_db_instances, the response is a dictionary. To access your particular DB instance, access it as follows:

instances = source.describe_db_instances(DBInstanceIdentifier=db_instance)
rds_host = instances.get('DBInstances')[0].get('Endpoint').get('Address')
# or
# rds_host = instances.get('DBInstances')[0]['Endpoint']['Address']

Upvotes: 13

Related Questions