AWS Lambda Python Script to create Database Snapshots

The below script is used to create a database manual snapshots on all regions in an AWS account.  RDS support automatic snapshots which are very useful for point in time recovery but it does not protect you from accidental deletion of RDS instances.

Here are the reasons why we need manual snapshots

  1. Accidental RDS Instance Deletion
  2. Cross region backup
  3. Long term retention

import boto3
import time
import sys

def lambda_handler(event, context):
ec2Regions = boto3.client('ec2')
awsRegions = ec2Regions.describe_regions()['Regions']
for region in awsRegions:
rds = boto3.client('rds',region_name=region['RegionName'])
try:
current_date = time.strftime("%Y-%m-%d-%H-%M-%S")
print (current_date)
print (region)
response = rds.describe_db_instances()
try:
for rdsinstance in response['DBInstances']:
if (rdsinstance['DBInstanceStatus']=='available'): 
try:
shotIdentifier = rdsinstance['DBInstanceIdentifier'].replace('_','-') + current_date
rds.create_db_snapshot(DBInstanceIdentifier = rdsinstance['DBInstanceIdentifier'],DBSnapshotIdentifier = shotIdentifier)
except Exception as e:
print ('Error::%s'%e)
else:
print ('Instance NOT available Instance State is %s and Engine is %s'%(rdsinstance['DBInstanceStatus'],rdsinstance['Engine']))
except Exception as e:
print ('Error::%s'%e)
except Exception as e:
print (e)
#lambda_handler(None,None)

Hope you enjoyed the post.

Cheers

Ramasankar Molleti

LinkedIn

 

Published by Ramasankar

Hi. I’m Ramasankar Molleti. I’m a passionate IT professional with over 14 years of experience on providing solutions for customers who are looking on cloud computing, Database Migration, Development, and Big Data. I love learning new technologies and share my knowledge to community. I am currently working as Sr Cloud Architect with focus on Cloud Infrastructure, Big Data. I work with developers to architect, build, and manage cloud infrastructure, and services. I have deeep knowledge and experience on working with various database platforms such as MS SQL Server, PostgeSQL, Oracle, MongoDB, Redshift, Dyanamodb, Amazon Aurora. I worked as Database Engineer, Database Administrator, BI Developer and successfully transit myself into Cloud Architect with focus on Cloud infranstructure and Big Data. I live in USA and put my thoughts down on this blog. If you want to get in touch with me, contact me on my Linkedin here: https://www.linkedin.com/in/ramasankar-molleti-23b13218/ My Certifications: Amazon: AWS Certified Solutions Architect – Professional AWS Certified DevOps Engineer – Professional certificate AWS Certified Big Data – Specialty AWS Certified Security – Specialty certificate AWS Certified Advanced Networking – Specialty certificate AWS Certified Solutions Architect – Associate Microsoft: Microsoft® Certified Solutions Associate: SQL Server 2012/2014 Microsoft Certified Professional Microsoft® Certified IT Professional: Database Administrator 2008 Microsoft® Certified Technology Specialist: SQL Server 2008, Implementation and Maintenance

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: