Start Stop Your RDS Instance Using Lambda and Cloudwatch

Today I will show you, how you can start and stop your AWS RDS instance using Lambda and Cloudwatch.

Step 1: Create a Policy which has RDS, start-stop permission and has Cloudwatch permission.

Create new Policy and paste the below policy in it:

 1{
 2    "Version": "2012-10-17",
 3    "Statement": [
 4        {
 5            "Effect": "Allow",
 6            "Action": [
 7                "rds:DescribeDBInstances",
 8                "rds:StopDBInstance",
 9                "rds:StartDBInstance",
10                "lambda:GetFunction",
11                "lambda:GetFunctionConfiguration"
12            ],
13            "Resource": "*"
14        },
15        {
16            "Effect": "Allow",
17            "Action": [
18                "logs:CreateLogGroup",
19                "logs:CreateLogStream",
20                "logs:PutLogEvents",
21                "lambda:GetFunction",
22                "lambda:GetFunctionConfiguration"
23            ],
24            "Resource": "*"
25        }
26    ]
27}

Note: To know how to create a custom IAM policy , Please follow this link

Step 2: Create an IAM role and attached the above policy.

Note: Please check if Trust Relationship has below policy:

 1{
 2  "Version": "2012-10-17",
 3  "Statement": [
 4    {
 5      "Effect": "Allow",
 6      "Principal": {
 7        "Service": "lambda.amazonaws.com"
 8      },
 9      "Action": "sts:AssumeRole"
10    }
11  ]
12}

Note: To know how to create IAM Role, Please follow the this link

Step 3: Create a Lambda Function to Start the RDS Instance:

Create a Lambda function and select the python language. Copy/paste the below script in the lambda function:

1import boto3
2
3def lambda_handler(event, context):
4    rds = boto3.client('rds')
5    print 'starting DEV RDS instance'
6    rds.start_db_instance(DBInstanceIdentifier='#RDS-identifier-name')

Note: Please change the "#RDS-identifier-name" with your RDS DB Instance identifier.

Step 4: Create a Lambda Function for Stopping the RDS Instance:

Create a Lambda function and select the python language. Copy/paste the below script in the lambda function:

1import boto3
2
3def lambda_handler(event, context):
4    rds = boto3.client('rds')
5    print 'stopping DEV RDS instance'
6    rds.stop_db_instance(DBInstanceIdentifier='#RDS-identifier-name')

Note: Please change the "#RDS-identifier-name" with your RDS DB Instance identifier.

Step 5: Save the both lambda function and test both the functions are working or not.

Step 6: Now Open AWS CloudWatch to configure the scheduler.

Open CloudWatch Service and Click on the Rules menu, present in the Left side of your AWS console.

CloudWatch scheduler

Step 7: Create a New Rule.

Click in Create Rule and Select schedule from the Event Source.

CloudWatch Rule image

Step 8: Select the Cron expression and Enter the Timing:

Here I want to run the start script at 11:30 GMT Monday to Friday every month. (5:00 PM IST)

CloudWatch scheduler image

Note: Time in CloudWatch is in GMT. You have to check only to convert IST into GMT. You can use this link to convert the time from IST to GMT.

Also, Use this link to learn more about the CloudWatch cron format.

Step 10: Select the Lambda function in "Targets" and select your function from the drop-down menu. Here you have to select the start lambda function from the "Function" menu.

CloudWatch target

Step 11: Last, enter the Rule name , the description and check the "Enabled" state check-box.

CloudWatch Last Step

Step 12: Now Click on "Create rule" to complete the step.

CloudWatch Create Rule

Now Create the Rule for Stopping the RDS instance. Follow the same steps ( from 6th to 12th ) and select the Stop lambada script.

Note: Please note all service should be in the same region.

Enjoy :)