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.
Q: What if I want to take a snapshot without any retention period?
A: Just take a manual snapshot from the AWS Console 🙂.
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.
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.
Next create a rule in CloudWatch Events to trigger every 15 days (any frequency of your choice).
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
Create a SNS Topic and create a subscription for the email you want to notify.
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.