Introduction

If RDS Instances is AWS keep running without 24×7 usage, it can cost you very high in AWS billing.

Take following example how idle RDS instances can cost you:-

Lets take a medium sized RDS instance db.m4.xlarge (4CPU and 16GB RAM) in Sydney region for MySQL engine costs $0.492 per hour.

Now if we use the calculator, usage running 24×7, costs on average $360.15 per month.

Now if we only had that running business hours (9 hrs a day MON-FRI) , talking on average $92.99.

Thats a cost saving of $267.16. That is just for one instance per month/ OR $3,205.92 per annum.

What if you were working in a large enterprise with 10s or 100s of RDS instances.

Hence it is very important to stop the RDS instance when Idle to save significant cost in AWS Billing.

Let’s use aws-sdk, nodeJs Lambda function and CloudWatch to start/stop RDS instances in schedule interval.

Following are step by step guide with screenshot.

Download

Download the source code in zip format from following link and keep the zip file in local machine for further upload in Lambda function.

https://s3.ap-south-1.amazonaws.com/rdsstopstartcode/rds-lambda-stop-start.zip

Step 1: Create IAM Policy RDSManagement

First, let’s create policy, which is in AWS IAM console.

Go to Services -> Policies -> Create Policy -> Create Your Own Policy.
Let’s call it “RDSManagement”. Put the code given below to the ‘Policy Document’ field:

{
    "Version": "2012-10-17",
    "Statement": [        
        {
            "Action": [
                "rds:StopDBInstance",
                "rds:StartDBInstance"
            ],
            "Effect": "Allow",
            "Resource": "*"
        }
    ]
}

Press ‘Create policy’, Create Your Own Policy put name, description and JSON to the appropriate fields and press save. Eventually, you have to get something like on the screenshot below:

Screen1:-

1

Screen2:-

2

Screen3:-

3

Screen4:-

4

Step 2: Create Role LambdaRDSManagement

The second step is to create a role which will be associated with lambda function and allow it to manage RDS instances.

Navigate to Services -> Roles -> Create new Role.

Select ‘AWS Lambda’ in ‘AWS Service Role’ section. Search for the policy we created previously, select it, and press ‘Next’.

Screen5:-

5.png

Put “LambdaRDSManagement” as a Role Name, set some description and press ‘Create Role’.

Screen6:-

6.png

Screen7:-

7.png

Screen8:-

8.png

Step 3: Create Lambda Function ManageRDSInstances

Now we are ready to go and create lambda function which will manage our instances. Navigate to Services -> Lambda -> Create a Lambda function -> Blank function. Let’s call it ‘ManageRDSInstances’, select latest Node js 6.x as a runtime. Ignore lambda function code for now, and select ‘Choose an existing role’ in ‘Role’ field. You have to be able to find the previously created role in ‘Existing role’ field. Press ‘Next’ -> ‘Create function’.

Screen9:-

9.png

Now let’s upload our archive zip file previously downloaded in local machine to newly created lambda function.

Services -> Lambda -> ManageRDSInstances, and change Code entry type to ‘Upload a .ZIP file’. Press ‘Upload’, select your zip file and press ‘Save’.

Screen10:-

10

Now we need to configure test event: Actions -> Configure test event.

Screen11:-

11.png

Where “tomcatdb01” and “mysqlbiz” are testing RDS instances. After pressing Press ‘Save’ and ‘Test’, you will see that your RDS instances changed state to ‘Stopping’ and soon to ‘Stopped’.

After they are stopped, you can run the same test with action ‘start’, which will run change state of RDS instances to running.

Step 4: Create CloudWatch rule to schedule the event

The last thing is to set up CloudWatch rules to trigger these function on schedule.
Services -> CloudWatch -> Rules -> Create Rule.

Screen12:-

12.png

Select Schedule instead of default Event Pattern. Now you need to set up cron time. Keep in mind that time must be set in GMT timezone. For instance, to start instances every day at 8 am in cron time will look like this: ‘* 8 * * *’.
After you set cron time for waking up your instances, select Lambda function as a Target and pick your newly created lambda function. Then in Configure Input section put your JSON to Constant(JSON text) field:

{ "instances": ["some-test-instance-1",
"some-test-instance-2"], "action":"start" }

Screen13:-

13.png

Screen14:-

14.png

Now your instances will be woken on every morning at 8AM. Create a similar rule with correct cron time for stopping them, do not forget to change action from start to stop in the json:

{ "instances": ["some-test-instance-1",
"some-test-instance-2"], "action":"stop" }

So now we have schedule the Lambda functions to trigger at schedule interval to start and stop RDS instances and save significant cost in AWS billing.

Leave a Reply

Your email address will not be published. Required fields are marked *