mobilefish

Untitled

Jan 13th, 2021
597
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import boto3
  2.  
  3. #   Author: Mark Young
  4. #   Date:   19th December 2017
  5. #   Detail: Stop and start RDS instances based on Tags
  6. #   Note:   Aurora databases are currently not supported for shutdown and startup methods.
  7. #
  8. #   Audit:  1.0 - M.Y. Original
  9. #
  10.  
  11. def lambda_handler(event, context):
  12.     Tag = 'MYSQLTEST'
  13.     Key = 'Application'
  14.  
  15.     client = boto3.client('rds')
  16.     response = client.describe_db_instances()
  17.  
  18.     for resp in response['DBInstances']:
  19.        db_instance_arn = resp['DBInstanceArn']
  20.  
  21.  
  22.        response = client.list_tags_for_resource(ResourceName=db_instance_arn)
  23.        for tags in response['TagList']:
  24.            if tags['Key'] == str(Key) and tags['Value'] == str(Tag):
  25.                status = resp['DBInstanceStatus']
  26.                InstanceID = resp['DBInstanceIdentifier']
  27.                print(InstanceID)
  28.                #print(status)
  29.                if status == 'available':
  30.                    print("shutting down %s "+ InstanceID)
  31.                    client.stop_db_instance(DBInstanceIdentifier= InstanceID)
  32.                    #print ("Do something with it : %s" % db_instance_arn)
  33.                elif status == 'stopped':
  34.                    print("starting up %s " % InstanceID)
  35.                    client.start_db_instance(DBInstanceIdentifier= InstanceID)
  36.                else:
  37.                    print("The database is " + status + " status!")
  38.    
  39.    
  40.     return { 'message' : 'NA'}
  41.  
  42.  
  43.  
RAW Paste Data