How to stop/start AWS RDS with AWS lambda function or AWS CLI by cronjob expression
Nowadays, The AWS RDS provides and supports many database instances such as SQL Server, Oracle, MySQL, MariaDB, and PostgreSQL. You can easily create, configure, and manage the AWS RDS SQL Server in AWS dashboard console web UI. However, this service is not free and charge costly depending on your context and workload.
The purpose of this article, I would like to share one of the effective ways how to reduce the cost of AWS RDS instances by using the lambda function which is used for the development phase on SIT/UAT environments in the company or organization. In this context, we will focus on stopping/starting an AWS RDS instance on the fly using lambda function with Python Language and cronjob expression at the regular time interval in Cloud Watch Event.
RDS Database instance
You can quickly create /start or stop RDS instances using AWS web console or AWS CLI. In the following screenshot, you can see that instance is in Available status.
In the RDS dashboard, select the instance and go to Actions.
- Stop: To stop the RDS instance
- Reboot: To restart RDS instance services
- Delete: if we want to remove the RDS database
- Take Snapshot: RDS automatically takes snapshots to recover data in case of any issues. You can use this option to take a manual snapshot
- Restore to point in time: We can restore RDS instance to a specific point in time to recover data
Create IAM Role
First, we must create a policy for the IAM role to be attached to.
- Go to the AWS console
- Choose IAM service
- Click on Role
- Attach Policies
- Click on the JSON tab and paste the JSON shown below to gain access to certain RDS actions and Cloud Watch Log Events
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"rds:DescribeDBInstances",
"rds:StopDBInstance",
"rds:StartDBInstance"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}
Create the AWS Lambda function
Before creating a Lambda function, make sure your region is the same as the region where you created the DB instance.
Creating lambda function with Python 3.8: stop_start_rds_instance
- Go to services
- Click on Lambda
- Click on Create Function
- Choose Author from Scratch
Enter the following information on the window:
Name: stop_start_rds_instance
Runtime: Python 3.8
Role: Choose an existing role
Role Name: RDS_Stop_Start_Lamda_Role
Click on Create Function
How to Set the Schedule in CloudWatch Event?
- Go to Service
- Cloudwatch
- Events →Rules
CloudWatch Event : Cron-job with Cloudwatch Events :
start instances : 8AM (phnom Penh) — UTC to GMT + 7 :
Cron expression : 0 1 ? * MON-FRI *
Targets : choose Lambda function : (stop_start_rds_instance)
Json: { “action”: “start”, “dbInstance”: “rds-name” }
Stop rds instance: 7:15pm
Stop instances : 7:15pm (phnom Penh) — UTC to GMT + 7 :
Cron expression : 15 12 ? * MON-FRI *
Targets : choose Lambda function : (stop_start_rds_instance)
Json: { “action”: “stop”, “dbInstance”: “rds-tmt” }
Stop/Start using AWS CLI
Before executing this command line make sure you have enough permissions to stop/start rds instances. AWS CLI is available for Windows/Linux/MacOS
#Stop/start rds instance with aws instance>aws rds start-db-instance -db-instance-identifier## with profile>aws rds start-db-instance -db-instance-identifier -profile production/uat/sit##stop rds instance>aws rds stop-db-instance -db-instance-identifier## with profile>aws rds stop-db-instance –db-instance-identifier ssasinrds –profile production
Using Cron Job and bash script
>nano rds.sh (location : /opt/rds.sh)#!/usr/bin/env bash
# -----------------------------------------------------------------
# This is the Unix startup script for the application
# Author : Mak Sophea
# Version : 1.0
# -----------------------------------------------------------------if [ $# -eq 2]; then
echo "DatabaseName $1 with Action $2 "
else
echo "Failed to execute $#. The usage is ./rds.sh [dbName] stop|start"
exit 1;
fiDB_NAME=$1
ACTION=$2if [ $ACTION == 'start' ]; then
aws rds start-db-instance -db-instance-identifier
elif [ $ACTION == 'stop' ]; then
aws rds stop-db-instance -db-instance-identifier
else
echo "Failed to execute $# . The usage is ./rds.sh [dbName] stop|start"
exit 1;
fi
if [ $? != 0 ]; then
echo "Something wrong with action $ACTION on db instance $DB_NAME"
exit 1;
fi
echo "db instance $DB_NAME with action $ACTION successfully"
Cron-Job
````
# Cron job
````
$crontab -e#start the db instance every 7AM Mon-Fri
0 8 * * MON-FRI /opt/rds.sh dbName start#stop the db instance every 7PM Mon-Fri
0 19 * * MON-FRI /opt/rds.sh dbName start````
In Conclusion
You can now start/stop the AWS RDS instance using the Lambda function or AWS CLI.
If this article is helpful, please support to press on the clap 👏 button and help to share with other readers to reach this story as well.