Create RDS Snapshot via Lambda

Today we will Learn "How to create an AWS RDS Snapshot using Lambda and Cloudwatch". So that we can automate the AWS RDS Snapshot creation 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
 3import datetime
 4from datetime import datetime
 5
 6def lambda_handler(event, context):
 7        client = boto3.client('rds')
 8        now = datetime.now()
 9        date = now.strftime("%d-%m-%Y-%H-%M-%S")
10        tagname = now.strftime("%d-%m-%Y")
11        #print("date and time =", date)
12        response = client.create_db_snapshot(
13        DBSnapshotIdentifier='RDS-NAME-snapshot-{}'.format(date),
14        DBInstanceIdentifier='RDS-NAME',
15        Tags=[
16            {
17                'Key': 'backupon',
18                'Value': tagname
19            },
20         ]
21        )
22        #return (response);

Note: change "RDS-NAME" with your RDS Instance Identifier name.

  • 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 take the RDS Snapshot backup for you daily at given Timing.

To learn who to delete AWS RDS Snapshot after retention period over. Click on this link.

I :heart: AWS! :smile: Enjoy