Creating a Lambda based Automated Backup Job for AWS RDS
This article helps creating an automated RDS backup function in Lambda that uses AWS SDK java.
Why Auto Backups feature of RDS is 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 RDS auto backup feature is 35 days at max which means you can keep 35 days 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 organization prefer one backup per week and some prefer more than one backups per day. This kind of customizable and sophisticated routine is not achievable using the auto backup feature of RDS.
We assume you already have Java, eclipse, AWS eclipse toolkit with SDK all configured and running. Here are some useful links
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
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 private static final String accesskey = "xxxx"; private static final String secretkey = "xxxxx"; //RDS instance identifier private 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 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 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
Bluestack IT Solutions