How to minimise your costs in aws rds

Naguib (Nick) ihab
Jul 10, 2017 · 3 min read

If you’re using AWS to host your applications then you’d probably be treading carefully when it comes to the RDS instances. AWS has introduced this nice new feature that allows you to stop an RDS instance which essentially deletes it and keeps a snapshot and just deploys that snapshot again when you want to start it, but it makes life a lot easier especially if you put that kind of function in a batch file and just run that file whenever you need to.

In this article I’ll show you how to stop your RDS instances automatically every night and start them up again in the morning using AWS CLI.

Prerequisite: You have to have already setup your AWS CLI with your account and are able to run AWS commands, follow this link to set it up

Running it from Windows

Step #1 Update your CLI

Update your CLI to the latest version by running pip install --upgrade awscli to upgrade or just download and install the latest version

Now when you type in aws --version in your terminal you should see something like this:

C:\Users\nick>aws --version
aws-cli/1.11.109 Python/3.4.3 Windows/8 botocore/1.5.72

As I’m writing this on the 22nd of June 2017, the latest version for windows is 1.11.109

Step #2 Writing the script

Create a new batch file .bat and write the following script

@echo off
set rdslist=rds-instance-1 rds-instance-2
:retryaction
set /P action=Would you like to (1)start or (2)stop these instances %rdslist%:
IF %action%==1 (
set command=start
goto :start
)
IF %action%==2 (
set command=stop
goto :start
)
goto :retryaction:start
(for %%a in (%rdslist%) do (
call aws rds %command%-db-instance --db-instance-identifier %%a
))
pause

And replace the rds-instance & rds-instance-2 with your own instances names.

What we’re doing here is that we’re creating a list of our RDS instances and then iterating over each one and either stopping or starting them. What you should see when you run this script is the following:
Would you like to (1)start or (2)stop these instances rds-instance-1 rds-instance-2:
once you choose the action you want to take the batch file will run the start-db-instance or stop-db-instance command on each of your RDS instances.

You need to maintain that file and add,edit or delete RDS instances in that line 2.

At this stage you’re able to run that batch file and start or stop RDS instances at anytime.

Step #3 Automating the script

If you want to schedule it from your PC or windows server you can use the Task Scheduler

Once you open it click on Create a Basic Task and give it the name “Start RDS”. Continue with the wizard and select the trigger that you’re after, for example you may want the RDS instances to start when you start the computer, when you login or automatically at a particular time every day.
Keep going until you get to this screen

Add in the path to your batch file and the argument 1

Continue through the wizard and finish the task.
Now we need to add a small modification to our script to have it accept parameters instead of prompting a question:

@echo off
set rdslist=rds-instance-1 rds-instance-2
IF %1==1 (
set command=start
)
IF %1==2 (
set command=stop
)
(for %%a in (%rdslist%) do (
aws rds %command%-db-instance --db-instance-identifier %%a
))

That’s it! You now have two bash files, one that you can run manually to shut down and start instances whenever you need them and one that can run on a scheduled time or trigger.

Watch this space and I’ll publish how to do that using Linux pretty soon ;)

Naguib (Nick) ihab

Written by

Founder @ upilio.io (My Guinea Pigs App)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade