首页 > 解决方案 > 有没有办法向自动化 AWS Redshift 快照添加标签?

问题描述

我的问题非常简单——您能否将标签列表附加到 AWS 将为集群拍摄的自动集群快照中?

一些背景知识,当我们手动创建快照时,我们上传了一系列标签以及 CreateClusterSnapshot 请求,允许我们将快照与用户关联(因此使他们能够在以后从“终止”实例恢复红移集群。不幸的是,自动快照(通过 withAutomatedSnapshotRetentionPeriod 设置启用)到达 aws 时未加标签,因此除了手动添加标签外,我们无法将它们与最终用户相关联。

谢谢!

标签: amazon-web-servicesconfigurationamazon-redshift

解决方案


目前在 Terraform 中无法做到这一点。我向 AWS 提出请求,要求他们将此功能添加到 redshift。如果您想将集群标签添加到自动快照,您可能会认为只需选中一个框即可。

  1. 自动快照不会生成 cloudtrail 日志,因此您不能执行此事件驱动(据我所知),它必须执行一个经常运行以查找快照的 cron 作业。

     import json
     import boto3
    
     def lambda_handler(event, context):
         client = boto3.client('redshift')
         response = client.describe_cluster_snapshots()
    
     for i in range(len(response["Snapshots"])):
    
         #Count number of Tags assoicated with Snapshot
         NumofTags = len(response["Snapshots"][i]["Tags"])
    
         #Get the name of the cluster the snapshot is for
         clustername = response["Snapshots"][i]["ClusterIdentifier"]
    
         #Create the ARN for the snapshot
         arn = "arn:aws:redshift:us-east-1:631284890255:snapshot:" + clustername + "/" + str(response["Snapshots"][i]["SnapshotIdentifier"])
    
    
         #if snapshot has no tags get tags from cluster and append tags to snapshot
         if NumofTags == 0:
    
             try:
    
                 #Get Cluster information
                 cl_response = client.describe_clusters(ClusterIdentifier=clustername)
    
                 #Get tags from cluster
                 clustertags = cl_response["Clusters"][0]['Tags']
    
                 #Append cluster tags to snapshot
                 client.create_tags(ResourceName=arn,Tags=clustertags)
    
             #If cluster no longer exists print Exception and continue function    
             except:
                 print("Cluster already destroyed: no tags to be assigned")
                 continue
    
    
    
     return "Function Complete"
    

这是我为检查每个快照的标签而编写的 lambda,如果没有标签,则从集群中提取标签并添加它们。如果集群已经被销毁但快照仍然存在,则例外。


推荐阅读