Skip to content

Instantly share code, notes, and snippets.

@reedobrien
Forked from mzupan/lambda.py
Created February 6, 2018 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save reedobrien/64bcade90a974a4eb0bfe1fd21052eef to your computer and use it in GitHub Desktop.
Save reedobrien/64bcade90a974a4eb0bfe1fd21052eef to your computer and use it in GitHub Desktop.
AWS Lambda job to backup RDS instances
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