使用 AWS LAMBDA 删除旧的手动集群 Snaphots RDS

在我的 AWS Lambda 的 python 代码下方,我想使用它删除旧的手动 RDS 快照,但它仍然无法正常工作,我需要帮助来调试和更正此 lambda 脚本。非常感谢。


import boto3

from os import getenv

import datetime

from datetime import date

client = boto3.client('rds')

ClientName = getenv('CLIENT_NAME')

today = date.today()


def lambda_handler(event, context):

    delete_db_cluster_snapshot():

    snapshots_marker = ""

    while snapshots_marker != None:

        snapshots = client.describe_db_cluster_snapshots(Marker=snapshots_marker)

        

        if 'Marker' in snapshots:

            snapshots_marker = snapshots['Marker']

        else:

            snapshots_marker = None

            

        for snapshot in snapshots['DBClusterSnapshots']:

            if snapshot["SnapshotType"] == "manual" and ClientName in snapshot["DBClusterIdentifier"] and snapshot ["SnapshotCreateTime"].date() < today:

                client.delete_db_cluster_snapshot(DBClusterSnapshotIdentifier=snapshot["DBClusterSnapshotIdentifier"])

                

delete_db_cluster_snapshot()


Cats萌萌
浏览 122回答 1
1回答

隔江千里

您的代码看起来不错,但您应该删除子函数:delete_db_cluster_snapshot()import boto3from os import getenvimport datetimefrom datetime import dateclient = boto3.client('rds')ClientName = getenv('CLIENT_NAME')today = date.today()def lambda_handler(event, context):&nbsp; &nbsp; snapshots_marker = ""&nbsp; &nbsp; while snapshots_marker != None:&nbsp; &nbsp; &nbsp; &nbsp; snapshots = client.describe_db_cluster_snapshots(Marker=snapshots_marker)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if 'Marker' in snapshots:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; snapshots_marker = snapshots['Marker']&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; snapshots_marker = None&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for snapshot in snapshots['DBClusterSnapshots']:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if snapshot["SnapshotType"] == "manual" and ClientName in snapshot["DBClusterIdentifier"] and snapshot ["SnapshotCreateTime"].date() < today:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; client.delete_db_cluster_snapshot(DBClusterSnapshotIdentifier=snapshot["DBClusterSnapshotIdentifier"])
打开App,查看更多内容
随时随地看视频慕课网APP