In this article , I have explained how you can reduce the usage of RDS instances by Auto Start & Stop RDS instances using Lambda

Services Involved:

We will be using the following AWS services to implement this setup.

RDS : Running RDS Instance , which will be automatically started and stoped at regular intervals by lambda function.

IAM : We need a custom IAM policy and execution role for the lambda function.

CLOUDWATCH EVENT : A cloudwatch event rule , where we will setup scheduled cron to trigger the function.

LAMBDA : We will configure the function here and we will provide the details of EC2 instances.

Create a IAM policy and Role:

We need to create an IAM policy with the execution role and then we will attach this policy to the lambda function so that it will be able to manage the RDS instances.

To create the IAM policy, Go to IAM Console,

https://console.aws.amazon.com/iam/home?region=ap-southeast-1#/home

Choose Policy in the left pane, Click Create Policy

You will see the following page, Choose JSON

RDS Instances

Remove the default values and copy / paste the below configuration,

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "arn:aws:logs:*:*:*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "rds:StopDBInstance",
"rds:StartDBInstance" ], "Resource": "*" } ] }

Click Review Policy, Give a name for the policy and choose Create policy.

Now We have to create a Role and attach the policy which we have created.

To create role, Choose Roles , Click Create Role

Under AWS Services , Choose Lambda

RDS Instances

Click Next: Permissions

type the name of the IAM policy we created earlier, Check the policy and Choose Next: Tags

Provide a name for the Role and then click Create Role.

Now , The Role with the required permissions for the lambda function is ready, Lets go ahead and create a lambda function.

Creating Lambda Function:

To create a Lambda function , Go to Lambda Console,

https://ap-southeast-1.console.aws.amazon.com/lambda/home?region=ap-southeast-1#/discover

Choose Create function, choose Author from Scratch

RDS Instances

For Function name, Give a unique name that describes the purpose of this function. eg: autostartstoprds

For Runtime , Choose Python 3.7

RDS Instances

Under permissions, expand choose or create execution role

Under execution role , Choose use a existing role (We should select the role which we have created).

Under Existing role, choose that you have created.

RDS Instances

Choose Create function.

Under Function Code section, Copy and paste the below code.

Dont forget to change the RDSĀ  instance name and the region where you’re running RDS instances.

Here is the code to stop the RDS Instances based on the RDS Instance name provided.

import boto3
region = 'ap-southeast-1'
rdsInstances = ['test1','test2']
def lambda_handler(event, context):
rds = boto3.client('rds', region_name=region)
for i in rdsInstances:
print('Stoping RDS '+ i)
rds.stop_db_instance(DBInstanceIdentifier=i)
print 'stopped your RDS instances: ' + str(rdsInstances)

Set the timeout to 10 sec, But you can give the timeout and Memory as per your requirements.

Choose Save.

We have lambda function with requirement permission to manage RDS Instance, Lets test it.

Testing Lambda Functions:

To test the lambda function, Choose the function you have created.

Choose Actions and Create a test events by clicking configure test events

RDS Instances
RDS Instances

Give a name for the event and Click Create.

Click Test.

RDS Instances

If everything is configured properly, You should see that the RDS Instances should be in stopped state.

Create Cloudwatch Event Rule to Trigger Lambda Function:

Go to Cloudwatch Console,

https://ap-southeast-1.console.aws.amazon.com/cloudwatch/home?region=ap-southeast-1#

Choose Rules under Events,

Click Create Rule , In Event source, You can choose either Fixed rate in hours , minutes , days or based on the Scheduled Cron expression.

Under Targets, Choose the function which you have created.

Choose Configure details, Give the name for the rule and Check enabled

Choose Create Rule.

Now Based on the Cloudwatch event rule, The lambda function will be triggered and the RDS instances will be stopped.

To Auto-start the RDS Instances:

We have to follow the same steps to start the instance Automatically.

IAM Role:

You can use the same IAM role which we have already created.

LAMBDA:

You need to create a new lambda function with the same settings, Named: startrdsinstance.

But we have to make small changes in the function code. use the below code for this function.

import boto3
region = 'ap-southeast-1'
rdsInstances = ['test1','test2']def lambda_handler(event, context):
rds = boto3.client('rds', region_name=region)
for i in rdsInstances:
print('Starting RDS '+ i)
rds.start_db_instance(DBInstanceIdentifier=i)
print 'started your RDS instances: ' + str(rdsInstances)

Make sure you have configured with correct region and RDS instance name.

Cloudwatch Event:

We have to create a new Cloudwatch event rule and configure an expression that triggers the lambda function when to start the RDS instance.

Once everything is configured properly, RDS instances will be automatically started as per the scheduled expression.

We have successfully automated the process of starting and stopping the instances to reduce the usage of RDS Instance during Non-production hours.

If you have liked it, Please do checkout my other articles.