{"id":281,"date":"2017-12-19T10:13:55","date_gmt":"2017-12-19T00:13:55","guid":{"rendered":"http:\/\/www.mrmarkyoung.com\/oracle\/?p=281"},"modified":"2017-12-19T10:13:55","modified_gmt":"2017-12-19T00:13:55","slug":"how-to-stop-and-start-rds-instance-in-aws-using-python","status":"publish","type":"post","link":"http:\/\/www.mrmarkyoung.com\/oracle\/2017\/12\/19\/how-to-stop-and-start-rds-instance-in-aws-using-python\/","title":{"rendered":"How to stop and start RDS instance in AWS using Python"},"content":{"rendered":"<p>I&#8217;ve been working on a project that requires us to shutdown and startup our development databases to save costs.. I&#8217;ve come up with a python script to perform the tasks. <\/p>\n<p>This could be modified to your own requirements.<\/p>\n<p>Here is the code:<\/p>\n<pre class=\"lang:default decode:true \" title=\"rds_stop_start.py\" >import boto3\r\n\r\n#   Author: Mark Young\r\n#   Date:   19th December 2017\r\n#   Detail: Stop and start RDS instances based on Tags\r\n#   Note:   Aurora databases are currently not supported for shutdown and startup methods.\r\n#\r\n#   Audit:  1.0 - M.Y. Original\r\n#\r\n\r\n\r\n\r\nTag = 'MYSQLTEST'\r\nKey = 'Application'\r\n\r\nclient = boto3.client('rds')\r\nresponse = client.describe_db_instances()\r\n\r\nfor resp in response['DBInstances']:\r\n   db_instance_arn = resp['DBInstanceArn']\r\n\r\n\r\n   response = client.list_tags_for_resource(ResourceName=db_instance_arn)\r\n   for tags in response['TagList']:\r\n       if tags['Key'] == str(Key) and tags['Value'] == str(Tag):\r\n           status = resp['DBInstanceStatus']\r\n           InstanceID = resp['DBInstanceIdentifier']\r\n           print(InstanceID)\r\n           #print(status)\r\n           if status == 'available':\r\n               print(\"shutting down %s \" % InstanceID)\r\n               client.stop_db_instance(DBInstanceIdentifier= InstanceID)\r\n               #print (\"Do something with it : %s\" % db_instance_arn)\r\n           elif status == 'stopped':\r\n                print(\"starting up %s \" % InstanceID)\r\n                client.start_db_instance(DBInstanceIdentifier= InstanceID)\r\n           else:\r\n                print(\"The database is \" + status + \" status!\")\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on a project that requires us to shutdown and startup our development databases to save costs.. I&#8217;ve come up with a python script to perform the tasks. This could be modified to your own requirements. Here is the code: import boto3 # Author: Mark Young # Date: 19th December 2017 # Detail: &#8230; <a title=\"How to stop and start RDS instance in AWS using Python\" class=\"read-more\" href=\"http:\/\/www.mrmarkyoung.com\/oracle\/2017\/12\/19\/how-to-stop-and-start-rds-instance-in-aws-using-python\/\" aria-label=\"Read more about How to stop and start RDS instance in AWS using Python\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[],"class_list":["post-281","post","type-post","status-publish","format-standard","hentry","category-aws"],"_links":{"self":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts\/281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/comments?post=281"}],"version-history":[{"count":1,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts\/281\/revisions"}],"predecessor-version":[{"id":282,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts\/281\/revisions\/282"}],"wp:attachment":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/media?parent=281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/categories?post=281"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/tags?post=281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}