'How can use Boto3 to find and delete an RDS snapshot based on it's name?
I am trying to create a Lambda function to delete old RDS snapshots. However, it appears that wildcards aren't a supported feature for Boto3 describe_db_snapshots. I have a function that creates a weekly RDS snapshot with a name that has date and time (weekly-2021-05-05-15-19). However, I want to create a Lambda function that deletes the old snapshot after a week, before a new one is created with a similar name. The code I have working prints out all the DBSnapshotIdentifier's for a specific database but I only want it to print out the DBSnapshotIdentifier if it's similar to "weekly-*". Once I get the print working, I'll edit it to delete the snapshot. Right now, the code returns nothing. I want it to return only the snapshot that starts with "weekly-".
import boto3
client = boto3.client('rds')
DB = 'mytestdatabase'
#-----Define Lambda function-----#
def lambda_handler(event, context):
snapshots = client.describe_db_snapshots(
DBInstanceIdentifier=DB,
SnapshotType='manual'
)
for i in snapshots['DBSnapshots']:
if (i['DBSnapshotIdentifier']) == 'weekly-*':
print(i['DBSnapshotIdentifier'])
Solution 1:[1]
I used the similar function and it prints out all the required snapshots, however i would now need to put a delete logic in there so that it deletes all snapshots prior to 1 week. As we only want to keep 7 days of snapshots
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | srk786 |
