如何在 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 的集群状态?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Python 中使用 JDBC 驱动程序和 Boto3 API 连接到现有的 Redshift 数据库

如何在python中使用boto3查询cloudwatch日志

如何使 Pyspark 脚本在 Amazon EMR 上运行以识别 boto3 模块?它说找不到模块

有没有办法使用 boto3 中的集群名称检查 emr 集群状态?

在单个AWS Lambda中使用两个python函数boto3

我们如何使用 python boto3 获取挂载到 EC2 实例的所有文件系统(EFS)的列表?