Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 745
Anar Sultanov Avatar asked Aug 22 '17 09:08

Anar Sultanov


1 Answers

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']
like image 163
hjpotter92 Avatar answered Oct 27 '22 21:10

hjpotter92