Automate the RDS backup function using Lambda

Kubernetes Advocate
AVM Consulting Blog
3 min readAug 24, 2022

Lambda-based Automated Backup Job for AWS RDS

Check out this tutorial that will create an automated backup system to overcome the limitations of the built-in auto backups feature.

Why Is the Auto Backups Feature of RDS Not Enough?

Before we go to the step-by-step guide we should know why the default automated backup feature of RDS provided by AWS is not enough.

There are two reasons for that:

1. The retention period of backups supported by the RDS auto-backup feature is 35 days at max which means you can keep a 35-day-old backup only. What if you want to keep backups older than 35 days?

2. Many organizations require a sophisticated backup routine where they prefer to take backups on selected days of the month. Some organizations prefer one backup per week and some prefer more than one backup per day. This kind of customizable and sophisticated routine is not achievable using the auto backup feature of RDS.

Prerequisites

We assume you already have Java, Eclipse, and AWS eclipse toolkit with SDK all configured and running. Here are some useful links if you don’t:

Setting Up AWS Eclipse Toolkit

Create, upload and invoke Lambda function

Try creating a HelloWorld Lambda function to make sure everything is working fine. Once you have your handleRequest method ready and working follow the steps below. handleRequest is the method that we need to override and within it, provide the functionality of our Lambda function

Steps

1. Get the access key and secret key from IAM console if you are using an IAM user to create an and run the Lambda function. If you are using root user download the keys from ‘My Security Credentials”.

2. Get the unique identifier for the target RDS instance from RDS console.

3. Know the region identifier for example us-west-2

4. Now create following String constants in your Request Handler class

//alphanumeric accesskey and secretkey privatestatic final String accesskey = "xxxx";private static final String secretkey = "xxxxx";//RDS instance identifierprivate static final String db_instance_identifier = "xyz";//region private static final String region = "xx-xxxx-x";

5. Create a unique identifier to be assigned later to the backup snapshot

String date = LocalDateTime.now().toString();//generated data has : character that is not supported to be used in the unique identifier, so//replacing it with - sign.String db_snapshot_id = "RDS-snapshot-"+date.replace(':','-').replace('.', '-');

6. Use BasicAWSCredentials to provide Lambda code the authentication to connect to RDS and take the backup snapshot. Create AWSStaticCredentialsProvider object out of BasicAWSCredentials .

BasicAWSCredentials basic_aws_credentials = new BasicAWSCredentials(accesskey,secretkey);AWSCredentials aws_credentials = (AWSCredentials)basic_aws_credentials;AWSStaticCredentialsProvider aws_static_credentials_provider = AWSStaticCredentialsProvider(aws_credentials)

7. We need to create an object that stores all information about the request we are going to make to the RDS instance for the creation of a backup. So for this, we create a CreateDBSnapshotRequest object.

CreateDBSnapshotRequest create_snapshot_request = new CreateDBSnapshotRequest(db_snapshot_id, db_instance_id);

8. Now create the AmazonRDSClient an object that is actually used to create the backup snapshot and also allows you to perform many other possible RDS-related tasks

AmazonRDSClient rdsclient = (AmazonRDSClient) AmazonRDSClientBuilder.standard().withCredentials(aws_static_credentials_provider).withRegion(region).build();DBSnapshot snapshot = rdsclient.createDBSnapshot(create_snapshot_request);

9. Build and Deploy your Lambda project.

👋 Join us today !!

️Follow us on LinkedIn, Twitter, Facebook, and Instagram

https://avmconsulting.net/

If this post was helpful, please click the clap 👏 button below a few times to show your support! ⬇

--

--

Kubernetes Advocate
AVM Consulting Blog

Vineet Sharma-Founder and CEO of Kubernetes Advocate Tech author, cloud-native architect, and startup advisor.https://in.linkedin.com/in/vineet-sharma-0164