Delet old Manual Cluster Snaphots RDS using AWS LAMBDA

880 Views Asked by At

below the python code of my AWS Lambda, i want to delete OLD MANUAL RDS SNAPSHOTS by using it, and it still not working with errors, i need help please for debugging and correcting this lambda script. thanks a lot of.

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()
1

There are 1 best solutions below

5
On BEST ANSWER

Your code looks fine, but you should remove the delete_db_cluster_snapshot() sub-function:

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):
    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"])