Python 删除 恢复 Redshift

利用最新的快照来启动Reshift实例并自动关联角色

import boto3
import os
import re

client = boto3.client('redshift', 'ap-northeast-1')


def lambda_handler():

    #INSTANCE_LIST = os.environ.get('TARGET_INSTANCE').split(";")
    INSTANCE_LIST = ['instacneid']

    for instanceid in INSTANCE_LIST:
        snapshot_list = client.describe_cluster_snapshots(
            ClusterIdentifier=instanceid
        )

        pattern = re.compile(r"%s" % instanceid)

        snapshot_list_f = list(filter 
                                   (lambda x: pattern.match(x["SnapshotIdentifier"]) and x["Status"] == "available",
                                    snapshot_list["Snapshots"]))

        if len(snapshot_list_f) == 0:
            print("No snapshot: InstanceID = {0}".format(instanceid))
            return


        snapshot_list_s = sorted(snapshot_list_f, key=lambda x: x["SnapshotCreateTime"], reverse=True)


        try:
            instance = instanceid
            snapshot = snapshot_list_s[0]["SnapshotIdentifier"]
            client.restore_from_cluster_snapshot(
                ClusterIdentifier=instance,
                SnapshotIdentifier=snapshot,
                IamRoles=["arn:aws:iam::1111111111:role/RedshiftCopyUnload"],
                VpcSecurityGroupIds=["sg-xxxxxx"],
                # NodeType="dc2.large",
                ClusterSubnetGroupName="public-a",
                PubliclyAccessible=True,
                AvailabilityZone="ap-northeast-1a"
            )
            print("Restore Instance {0} from snapshot = {1}".format(instance, snapshot))
        except Exception as e:
            print("Error: {0}".format(e))
            print("Error InstanceId = {0} Snapshot = {1}".format(instance, snapshot))
    return

if __name__ == "__main__":
    lambda_handler()

删除指定Redshift实例,并保留快照

# coding:utf-8
import boto3
import sys
from datetime import datetime, timedelta, tzinfo
import time
import re
import os

client = boto3.client('redshift', 'ap-northeast-1')



CLUSTER_ID='db-muc1602info-dwh'
class JST(tzinfo):
    def utcoffset(self, dt):
        return timedelta(hours=9)

    def dst(self, dt):
        return timedelta(0)

    def tzname(self, dt):
        return 'JST'



as_list = ['On', 'ON']



def lambda_handler():
    print("Start" + (datetime.now(tz=JST())).strftime("%Y%m%d%H%M"))
    try:
        delete_clusters(get_clusterid())
    except Exception as e:

        print("error: {0}".format(e))
        sys.exit(1)

    print("SUCCESS: task succeeded")
    return



def get_clusterid():
    response = client.describe_clusters()


    clusters = []
    for c in response["Clusters"]:
        if  c["ClusterStatus"] == "available" and c["ClusterIdentifier"]=='db-muc1602info-dwh' :
            clusters.append(c["ClusterIdentifier"])
        #print(c["ClusterIdentifier"])

    if len(clusters) == 0:
        print("SUCCESS: specified cluster is None")

    return clusters



def delete_clusters(clusterids):
    todaystr = (datetime.now(tz=JST())).strftime("%Y%m%d%H%M")
    for clusterid in clusterids:
        try:

            print("StartExec_ClusterId: {0}".format(clusterid))

            client.delete_cluster(
                ClusterIdentifier=clusterid,
                SkipFinalClusterSnapshot=False,
                FinalClusterSnapshotIdentifier=clusterid + "-" + todaystr
            )
            print("Delete_ClusterId: {0}".format(clusterid))

            print("EndExec_ClusterId: {0}".format(clusterid))

        except Exception as e:

            print("Error: {0} ".format(e)),
            print("Error_ClusterId: {0}".format(clusterid))
            # sns.publish(
            #     TopicArn=TOPIC_ARN,
            #     Message='an error occured when deleting redshift cluster = ' + clusterid,
            #     Subject='AutoShutdown_RedShift_Error'
            # )



def check_tags(cluster):
    res = False
    try:
        for tag in cluster["Tags"]:
            if tag["Key"] == "Auto-Shutdown" and tag["Value"] in as_list:
                res = True
                break
    except Exception as e:
        print("error: {0}".format(e))
        return False
    return res

if __name__ == "__main__":
    lambda_handler()
原文地址:https://www.cnblogs.com/dl-ekong/p/8205480.html