Closed1

LambdaとBoto3でとにかく全Redshiftクラスタ・RDSインスタンスを停止させる

とにかくAWSアカウント内の全クラスタ、全インスタンスを停止させるLambda関数。
RDS、君はなぜ7日後に再び立ち上がるのだ...?

import boto3

redshift = boto3.client('redshift')
rds = boto3.client('rds')

def lambda_handler(event, context):
    rs_clusters = redshift.describe_clusters()
    rs_clusters = [r['ClusterIdentifier'] for r in rs_clusters['Clusters']]
    print(rs_clusters)
    for c in rs_clusters:
        print(f'pause {c}')
        try:
            response = redshift.pause_cluster(
                ClusterIdentifier=c
            )
            print(response)
        except Exception as e:
            print(e)

    db_clusters = rds.describe_db_clusters()
    db_clusters = [r['DBClusterIdentifier'] for r in db_clusters['DBClusters']]
    print(db_clusters)
    for c in db_clusters:
        print(f'pause {c}')
        try:
            response = rds.stop_db_cluster(
                DBClusterIdentifier=c
            )
        except Exception as e:
            print(e)
        
    db_instances = rds.describe_db_instances()
    db_instances = [r['DBInstanceIdentifier'] for r in db_instances['DBInstances']]
    print(db_instances)
    for i in db_instances:
        print(f'pause {i}')
        try:
            response = rds.stop_db_instance(
                DBInstanceIdentifier=i
            )
        except Exception as e:
            print(e)

    return 'ok'
このスクラップは2022/05/18にクローズされました
ログインするとコメントできます