Wednesday, September 5, 2018

Stop RDS clusters based on Tag values


AWS RDS is an easy to setup and operate relational Database available for MySQL, Postgress, MariaDB, Oracle and MS SQL Server and Amazon Aurora

AWS RDS is priced per hour based on the usage apart from this you can acutally control the pricing by taking up Reserved Instances for RDS or by starting/stopping based on the usage were in the latter is quite an overhead as we need to spend more time on admin activities rather actual worklog with RDS. Please visit the Amazon documentation on detailed pricing information on each type of the DB you launch https://aws.amazon.com/rds/mysql/pricing/




Now coming to the actual idea of saving cost, we can stop the RDS by end of office hours/EOD and actually leave it to the developer or users to start with the instances to save more :-) You are the best person to decide the way to go !!!



Steps to automate RDS stopping in AWS:

  1. Login to your AWS account via console
  2. Go to services Lambda -> Author from scratch -> Give a Name; select 'Runtime-> Python2.7; Choose a Role -> Choose an existing Role; Existing Role-> Role that is already available in your account to run the Lambda script
  3. Click on 'Create Function'
  4. Click on your lambda function which expands to the code IDE for writing your python code
  5. Now copy the code from my github account[https://github.com/gvnirmalaa/utilities/blob/master/l_stopRDS_UseTags.py] into the Python script IDE
  6. Now, from the designer pane -> click on 'Cloudwatch' -> to select the cloud watch rule which has the definition of scheduling timelines to execute this lambda script
Steps to create Cloud watch Rule:
  1. Goto or select service 'Cloudwatch' in your AWS console/account
  2. Click on 'Rules' -> 'Create Rule' -> 'Event source-> scheduler' -> 'cron expression'  -> define or set the timing by which the lambda script is to be scheduled
  3. From the top right corner, click on the 'Add Target' -> Lambda -> <<select the lambda function created from above step'
  4. Click on 'Configure Details'
  5. Now a cron scheduler is ready for executing any lambda scripts as per the requirement
Note: the script will work only when you add tags against your existing or new RDS as 'Stop:yes'

1 comment: