Introduction
AWS provides a fully managed Database service called RDS. What does mean the word fully managed. It has every things which are required to run a database in cloud and also we don’t need to worry about licensing. But there are some things that customers should be manage like availability of the database, choosing the right size of database engine, maintaining backups, cost optimization and so on.
If you want to know how to upgrade the RDS database with minimal downtime feel free to look the following links to know more.
Here we are going to discuss about Cost optimization with the run time of a database. For example, If you want to run your database instance only a certain time like 9 am to 9 pm. But manually start the database in morning 9 am and stop it in evening 9 pm is boring, right. Some times you forget to start the database and it will lead the application downtime in the working hours.
But if you setup an automation process which is stop and start the RDS database daily, at the time which you specified. Do you know how to do that? Don’t worry… Here is a solution that will give you a very simple end-to-end process.
AWS provides a service called System Manager, which is we are gonna use to stop and start our database. Let’s get into the details now.
Create IAM role and Policy for System Manager
-
In the AWS IAM console click create role.
-
Select the Trust entity type to AWS service and for Use cases, search for system manager and select it like the picture below.
-
Then click Next button.
Now Click on the Create policy button to create a new IAM policy.
Copy the below code and paste it under the JSON editor and click Next:Tags followed by click Next button.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"rds:Describe*",
"rds:Start*",
"rds:Stop*",
"rds:Reboot*"
],
"Resource": "*"
}
]
}
-
On the Policy Review page, Enter a name for your IAM policy like ‘StopStartRebootRDS’.
-
Then click Create policy.
-
Now navigate to the IAM Role page and click the refresh button.
-
And then search for stopstart and choose the IAM policy that you have created and click Next.
In the review page Enter role name like ‘StopStartRebootRDS’.
Scroll down to the bottom and you can able to see the attached permissions. Finally click Create role.
Create SSM Association for Stop RDS Instance
-
Navigate to the Systems Manager console and in the left side click the State Manager link under the Node management section.
-
Now click the Create an Automation button.
-
For Name enter StopInstanceRDS.
-
Under Document section, search ‘AWS-Stop’ and choose the ‘AWS-StopRdsInstance’.
-
For execution choose Simple execution.
-
Under Input Parameters section, for InstanceId, you have to provide the name of the RDS instance which you want to stop and start periodically.
-
For AutomationAssumeRole, select the role which we have created the beginning of this blog.
-
Under the Specify schedule section, Choose On Schedule.
-
For Specify with choose CRON schedule builder and followed by Daily. if you want to customize the timing for hourly you can select the Hourly type.
-
Enter the time when you want to trigger this manager to stop the database. This is using UTC time. So you have to provide the time in UTC.
-
Click the Apply association only at the next specified cron interval. otherwise it will triggered once this manager gets created.
-
Finally click the Create association button.
Create SSM Association for Start RDS Instance
-
Now we have to create an Association document for Start RDS Instance.
-
For Name enter ‘StartInstanceRDS’.
-
Under Document section, search ‘AWS-Start’ and choose the ‘AWS-StartRdsInstance’.
-
For execution choose Simple execution.
-
Under Input Parameters section, for InstanceId, you have to provide the name of the RDS instance which you want to stop and start periodically.
-
For AutomationAssumeRole, select that StopStartRebootRDS role.
-
Under the Specify schedule section, Choose On Schedule.
-
For Specify with choose CRON schedule builder and followed by Daily.
-
Enter the time in UTC when you want to trigger this manager to start the database.
-
Click the Apply association only at the next specified cron interval.
-
Finally click the Create association button.
Now you have successfully created Associations for Stop and Start RDS instance.
You can check the execution details. For this choose any on of the association and click the Execution history section.
Conclusion
Using this simple process will help you to automate your database run timings. And it saves the cost of the databases in dev and test environments when they’re not in use, thereby leading to a compute cost saving. However, keep in mind that although we’re stopping the databases, the storage costs for the databases still apply.
I hope this article is helps you to learn a new thing. If you have any doubts or needs any clarifications, feel free to comment it to below article. We will help you to clarify.