Amazon Relational Database Service (Amazon RDS) is a web service that makes it easier to set up, operate, and scale a relational database in the cloud. It provides cost-efficient, resizable capacity and can manage common database administration tasks. If your site/application stores its sensitive data in the database, you will definitely want to back up that information so that it can be restored in case of any disaster.
Manual backups for RDS can be configured and scheduled. Thought of doing it the lambda way and below is a working script for the same. Code is originally developed by another author and shared at https://gist.github.com/mzupan/41d01bfb3b4c292fdac0
Below is the python code. The Lambda function is set to run every day using CloudWatch Event Scheduler. All we did was to create a trigger by connecting a CloudWatch event, where it is set up as a cron command, to the Lambda function.
The retention period is set as 7 days and also change the DBInstance identifier as the one in your RDS instance. Below code worked at the time of writing, but if it is not, please comment below.
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'] )
This is my solution to take RDS Snapshots using Lambda.