-
-
Save reedobrien/64bcade90a974a4eb0bfe1fd21052eef to your computer and use it in GitHub Desktop.
AWS Lambda job to backup RDS instances
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import datetime | |
def lambda_handler(event, context): | |
print("Connecting to RDS") | |
client = boto3.client('rds') | |
print("RDS snapshot backups stated at %s...\n" % datetime.datetime.now()) | |
client.create_db_snapshot( | |
DBInstanceIdentifier='web-platform-slave', | |
DBSnapshotIdentifier='web-platform-%s' % datetime.datetime.now().strftime("%y-%m-%d-%H"), | |
Tags=[ | |
{ | |
'Key': 'CostCenter', | |
'Value': 'web' | |
}, | |
] | |
) | |
for snapshot in client.describe_db_snapshots(DBInstanceIdentifier='web-platform-slave', MaxRecords=50)['DBSnapshots']: | |
if create_ts < datetime.datetime.now() - datetime.timedelta(days=7): | |
print "Deleting snapshot id:", snapshot['DBSnapshotIdentifier'] | |
client.delete_db_snapshot( | |
DBSnapshotIdentifier=snapshot['DBSnapshotIdentifier'] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment