Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import boto3
- # Author: Mark Young
- # Date: 19th December 2017
- # Detail: Stop and start RDS instances based on Tags
- # Note: Aurora databases are currently not supported for shutdown and startup methods.
- #
- # Audit: 1.0 - M.Y. Original
- #
- def lambda_handler(event, context):
- Tag = 'MYSQLTEST'
- Key = 'Application'
- client = boto3.client('rds')
- response = client.describe_db_instances()
- for resp in response['DBInstances']:
- db_instance_arn = resp['DBInstanceArn']
- response = client.list_tags_for_resource(ResourceName=db_instance_arn)
- for tags in response['TagList']:
- if tags['Key'] == str(Key) and tags['Value'] == str(Tag):
- status = resp['DBInstanceStatus']
- InstanceID = resp['DBInstanceIdentifier']
- print(InstanceID)
- #print(status)
- if status == 'available':
- print("shutting down %s "+ InstanceID)
- client.stop_db_instance(DBInstanceIdentifier= InstanceID)
- #print ("Do something with it : %s" % db_instance_arn)
- elif status == 'stopped':
- print("starting up %s " % InstanceID)
- client.start_db_instance(DBInstanceIdentifier= InstanceID)
- else:
- print("The database is " + status + " status!")
- return { 'message' : 'NA'}
RAW Paste Data