Delete RDS Snapshot via Lambda

Today we will Learn "How to delete an AWS RDS Snapshot using Lambda and Cloudwatch". So that we can automate the AWS RDS Snapshot deletion process as per our requirement.

Please follow the below steps to achieve this.

Step 1: Create IAM policy for an IAM Role.

  • Copy the below policy and paste it in your IAM policy.
 1{
 2  "Version": "2012-10-17",
 3  "Statement": [
 4    {
 5      "Action": "RDS:*",
 6      "Effect": "Allow",
 7      "Resource": "*"
 8    }
 9  ]
10}

Note: To know how to create a custom IAM policy , Please follow this link

Step 2: Create IAM Role and Attached the above IAM policy.

Note: To know how to create IAM Role, Please follow the this link

Step 3: Create Lambda Function.

To create an Lambda Function, Go to Lambda service from AWS console and create a new Function.

Lambda Console

  • Add Lambda Function name. (You can write any name).
  • In Runtime info, Choose "Python 3.7".
  • In permission, Choose "Use an existing Role" in Execution Role
  • In Existing Role, choose the IAM role which you have create above for this Lambda Function.
  • Click on "Create Function".
  • Go to "Function Code" and Paste the below Python code in it.
 1import json
 2import boto3
 3from datetime import datetime
 4from datetime import timedelta
 5
 6def lambda_handler(event, context):
 7        client = boto3.client('rds')
 8        now = datetime.now() - timedelta(days=2)   #change the retention days as per your requirement.
 9        date = now.strftime("%d-%m-%Y")
10        snapshots = client.describe_db_snapshots()
11        for i in snapshots['DBSnapshots']:
12            S_type = i['SnapshotType']
13            S_date= i['SnapshotCreateTime']
14            S_date = S_date.strftime("%d-%m-%Y")
15            if S_type == "manual":
16                if S_date == date:
17                    ID = i['DBSnapshotIdentifier']
18                    client.delete_db_snapshot( DBSnapshotIdentifier=ID )
19                    print("We have deleted the {} RDS Snapshot".format(ID))

Note: It will delete all 2 days RDS snapshots backup. You can change retention period as per your requirement.

  • Save the Lambda Function and Test the same. So that there should be no error.

Step 4: Create Cloudwatch Scheduler and attached the Lambda Function.

  • To configure the scheduler, Open CloudWatch Service and Click on the Rules menu, present in the Left side of your AWS console.

CloudWatch scheduler

  • Click in Create Rule and Select schedule from the Event Source.

CloudWatch Rule image

  • Select the Cron expression and Enter the Timing:

Here I want to take the AWS RDS Snapshot daily at 11:30 GMT Monday to Friday. (5:00 PM IST)

CloudWatch scheduler image

Note: Time in CloudWatch is in GMT. You have to check only to convert IST into GMT. You can use this link to convert the time from IST to GMT.

Also, Use this link to learn more about the CloudWatch cron format.

  • Select the Lambda function in Targets and select your function from the drop-down menu.

CloudWatch target

  • Last, enter the Rule name , the description and check the Enabled state check-box.

CloudWatch Last Step

  • Now Click on Create rule to complete the step.

CloudWatch Create Rule

Note: Please note all service should be in the same region.

Step 5: Shit relaxed, Your Scheduler will delete the RDS Snapshot backup for you daily at given Timing.

I :heart: AWS! :smile: Enjoy