Blog

How to take RDS Snapshots using Lambda?

Published on: April 22, 2019 by Smith Nevil

How to take RDS Snapshots using Lambda?

Scenario:

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.

Infrastructure operations are quite complex and require a high level of technical competency. Our team of specially trained infrastructure consultants provides expert cloud infrastructure management for all levels of operations.

 

Category : Backup management, Sever management

Smith Nevil

Smith Nevil

Smith is always ready to learn new technologies and explore new territories. His never-ending passion towards technological advancements, unyielding affinity to perfection and excitement in the exploration of new areas, help him to be on the top of everything he is involved with. He is currently working as System Engineer at SupportSages.

You may also read:

Comments

Add new commentSIGN IN

Let's Connect

Categories

Your Cart

Cart is empty.

Subtotal
₹0.00
APPLY
0
Send this to a friend