如何使用boto3获取RDS实例的主机

时间:2017-08-22 09:45:01

标签: python amazon-web-services amazon-rds boto3

我需要获得RDS实例的主机。我试着这样做:

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

也许你可以建议可能出现的问题。提前谢谢!

2 个答案:

答案 0 :(得分:4)

基于the boto3 docs for describe_db_instances,响应是字典。要访问您的特定数据库实例,请按如下方式访问它:

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']

答案 1 :(得分:0)

这将获取具有数据库标识符,数据库引擎,数据库大小,数据库类型的所有RDS实例的列表

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