AWS Lambda function for Automated Manual RDS Snapshots

RDS Automated snapshots can be set to a max retention of 35 days, which means AWS deletes your snapshot when it is 35 days old.

Max period is only 35 days 👿.

Q: What if I want to take a snapshot without any retention period?

A: Just take a manual snapshot from the AWS Console 🙂.

Select the instance and click Take snapshot from the instance actions.

Q: Alright, is there an option in the AWS Console to automate this manual snapshot?

A: No 😒.

Q: So every time I need to login to the console and do this task if I want snapshots with no retention period?

A: Yes 😒, but wait there is another way 😏!

Automated Manual RDS Snapshots

With AWS SDK we can take a manual snapshot programatically, which means we can write a script to do this and run it in a AWS Lambda function periodically!

Here is the script below.

Lambda function for creating manual RDS snapshots.

Create an IAM User of type Programmatic access with a Policy having RDS CreateDBSnapshot Permission. Generate Access key ID and Secret access key for this user and enter it in the Environment variables section of the Lambda Function.

Add the keys in the Environment variables section of the Lambda.

Next create a rule in CloudWatch Events to trigger every 15 days (any frequency of your choice).

Select the created Lambda in the target section of the Events rule.

Now we have CloudWatch Events triggering the Lambda every 15 days to create our RDS Snapshots with no retention period 😈.

Setup Email Notification 😌

You can also have success/failure email notification on every trigger of the Lambda function.

To setup notifications first update the IAM Policy you created before with SNS Publish Permission.

Add the SNS Publish permission to the policy you created for CreateDBSnapshot.

Create a SNS Topic and create a subscription for the email you want to notify.

Creating a subscription to a SNS Topic.

Copy the SNS topic’s ARN and add it as a variable SNS_TOPIC_ARN to the Environment variables section of the Lamdba.

Finally update your Lambda script to the one below.

Now you will start receiving email notifications every 15 days when the snapshot procedure starts and/or if it fails 😃.

Remember RDS has a limit of 100 manual snapshots per region. If you feel that’s not enough, you can request AWS to increase the limit.