如何在 Python 中使用 boto3 模块检查 Redshift 的集群状态?

Posted

技术标签:

【中文标题】如何在 Python 中使用 boto3 模块检查 Redshift 的集群状态?【英文标题】:How to check the cluster status of Redshift using boto3 module in Python? 【发布时间】:2021-03-26 13:39:48 【问题描述】:

我需要使用 Python 检查 Redshift 集群是否已暂停或可用。我知道模块 boto3 进一步提供了一个方法 describe_clusters()。我不确定如何继续为此编写 Python 脚本。

【问题讨论】:

【参考方案1】:

你可以试试

    import boto3
    import pandas as pd

    DWH_CLUSTER_IDENTIFIER = 'Your clusterId'
    KEY='Your AWS Key'
    SECRET='Your AWS Secret'
    
    # Get client to connect redshift
    redshift = boto3.client('redshift',
                           region_name="us-east-1",
                           aws_access_key_id=KEY,
                           aws_secret_access_key=SECRET
                           )
   
    # Get cluster list as it is in AWS console
    myClusters = redshift.describe_clusters()['Clusters']
    
    if len(myClusters) > 0:
        df = pd.DataFrame(myClusters)
        myCluster=df[df.ClusterIdentifier==DWH_CLUSTER_IDENTIFIER.lower()]
        
        print("My cluster status is: ".format(myCluster['ClusterAvailabilityStatus'].item()))
    else:
        print('No clusters available')

【讨论】:

请在您的答案中添加一些解释,以便其他人可以从中学习

以上是关于如何在 Python 中使用 boto3 模块检查 Redshift 的集群状态?的主要内容,如果未能解决你的问题,请参考以下文章