繁体   English   中英

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

[英]How to get host for RDS instance with 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

也许你可以建议可能是什么问题。 提前谢谢你!

基于对boto3文档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']

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

#!/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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM