def metrics_catalog(region): '''Build a catalog of available metrics''' conn_ec2 = ec2.connect_to_region(region) conn_elb = elb.connect_to_region(region) conn_rds = rds.connect_to_region(region) conn_cloudwatch = cloudwatch.connect_to_region(region) catalog = {'ec2': {}, 'ebs': {}, 'elb': {}, 'rds': {}} # EC2 instances for reservation in conn_ec2.get_all_instances(): for instance in reservation.instances: catalog['ec2'][instance] = conn_cloudwatch.list_metrics( dimensions={'InstanceId': [instance.id]}) # EBS Volumes for volume in conn_ec2.get_all_volumes(): catalog['ebs'][volume] = conn_cloudwatch.list_metrics( dimensions={'VolumeId': [volume.id]}) # ELB instances for balancer in conn_elb.get_all_load_balancers(): catalog['elb'][balancer] = conn_cloudwatch.list_metrics( dimensions={'LoadBalancerName': [balancer.name]}) # RDS instances for instance in conn_rds.get_all_dbinstances(): catalog['rds'][instance] = conn_cloudwatch.list_metrics( dimensions={'DBInstanceIdentifier': [instance.id]}) return catalog
def create_rds_snapshot(access_key, secret_key, token): print( '*******************************Starting snapshot-rds***************************************' ) password = raw_input( "Enter RDS password for expoliting, Note that it does not prompt for old password!!!:" ) region = 'ap-southeast-1' rds_name = 'nimbostratus' print('\n Access Key: %s \nsecret_key: %s, \ntoken: %s' % (access_key, secret_key, token)) try: conn = connect_to_region(region, aws_access_key_id=access_key, aws_secret_access_key=secret_key, security_token=token) except Exception, e: print('Failed to connect to RDS: "%s"' % e.error_message) return
def metrics_catalog(region): '''Build a catalog of available metrics''' conn_ec2 = ec2.connect_to_region(region) conn_elb = elb.connect_to_region(region) conn_rds = rds.connect_to_region(region) conn_cloudwatch = cloudwatch.connect_to_region(region) catalog = {'ec2':{}, 'ebs':{}, 'elb':{}, 'rds':{}} # EC2 instances for reservation in conn_ec2.get_all_instances(): for instance in reservation.instances: catalog['ec2'][instance] = conn_cloudwatch.list_metrics( dimensions={'InstanceId': [instance.id]}) # EBS Volumes for volume in conn_ec2.get_all_volumes(): catalog['ebs'][volume] = conn_cloudwatch.list_metrics( dimensions={'VolumeId': [volume.id]}) # ELB instances for balancer in conn_elb.get_all_load_balancers(): catalog['elb'][balancer] = conn_cloudwatch.list_metrics( dimensions={'LoadBalancerName': [balancer.name]}) # RDS instances for instance in conn_rds.get_all_dbinstances(): catalog['rds'][instance] = conn_cloudwatch.list_metrics( dimensions={'DBInstanceIdentifier': [instance.id]}) return catalog
def create_snapshot(instance_id, purge_days=None, aws_key_id=None, aws_secret_key=None, region='eu-west-1'): today = datetime.date.today() conn = rds.connect_to_region(region, aws_access_key_id=aws_key_id, aws_secret_access_key=aws_secret_key) instances = conn.get_all_dbinstances(instance_id=instance_id) if len(instances) != 1: print "Unable to locate instance id {0} in region {1}".format(instance_id, region) sys.exit(1) instance = instances[0] snapshot_id = INSTANCE_ID_TMPL.format(instance_id, today.isoformat()) print "Creating snapshot {0} for instance {1}".format(snapshot_id, instance_id) instance.snapshot(snapshot_id) snapshot = next((s for s in conn.get_all_dbsnapshots(instance_id=instance_id) if s.id == snapshot_id), None) if snapshot: status = snapshot.status print "Creating snapshot {0} for instance {1}. Status: {2}".format(snapshot_id, instance_id, status) while status == 'creating': time.sleep(1) snapshot = next((s for s in conn.get_all_dbsnapshots(instance_id=instance_id) if s.id == snapshot_id), None) status = snapshot.status print "Created snapshot {0} for instance {1}. Status: {2}".format(snapshot_id, instance_id, status) if purge_days is not None: if status != 'available': print "Skipping purge as latest snapshot isn't marked as available" sys.exit(1) purge_day = today - datetime.timedelta(days=purge_days) purge_snapshot_id = INSTANCE_ID_TMPL.format(instance_id, purge_day.isoformat()) snapshot = next((s for s in conn.get_all_dbsnapshots(instance_id=instance_id) if s.id == purge_snapshot_id), None) if snapshot: print "Deleting snapshot {0} for instance {1}".format(snapshot.id, instance_id) conn.delete_dbsnapshot(snapshot.id) print "Deleted snapshot {0} for instance {1}".format(snapshot.id, instance_id)
def get_rds_instances(aws_access, aws_secret): """Get all RDS instances in east and west. """ ri = {'east':[],'west':[]} wcon = rds.connect_to_region('us-west-1', aws_access_key_id=aws_access, aws_secret_access_key=aws_secret) econ = rds.connect_to_region('us-east-1', aws_access_key_id=aws_access, aws_secret_access_key=aws_secret) iwest = wcon.get_all_dbinstances() ieast = econ.get_all_dbinstances() for i in iwest: ri['west'].append(i) for i in ieast: ri['east'].append(i) logger.debug("Amazon RDS instances: %s", ri) return ri
def setUp(self): from boto import rds self.env_id = uuid.uuid4().hex self.rds = rds.connect_to_region(self.region) config_path = os.path.join( os.path.dirname(inspect.getsourcefile(Controller)), "..", "config.yaml") self.config_defaults = self.load_config_defaults(config_path) self.group_rules = self.load_access_group_rules()
def main(argv=sys.argv): global logger, settings, ca, ri, aws_access, aws_secret, longs, seens, template if len(argv) != 2: usage(argv) config_uri = argv[1] setup_logging(config_uri) settings = get_appsettings(config_uri) logger = logging.getLogger('scripts') ca = settings.get('mysql.ca') or None aws_access = settings.get('aws.access') or None aws_secret = settings.get('aws.secret') or None rdsecon = rds.connect_to_region('us-east-1', aws_access_key_id=aws_access, aws_secret_access_key=aws_secret) seens = defaultdict(dict) longs = [] rdses = [] whitelist_str = settings['rds.whitelist'] or '' whitelist = whitelist_str.split(',') for item in whitelist: rdses.extend(rdsecon.get_all_dbinstances(instance_id=item)) logger.info('Got rds instances %s', rdses) # hardcoding this is dumb mmmkay? mylookup = TemplateLookup(settings['mako.templatedir']) template = mylookup.get_template('emailproclist.mako') flush_interval = datetime.timedelta(minutes=FLUSH_INTERVAL_MINUTES) last_flush = datetime.datetime.now() try: while True: for instance in rdses: longs.extend(get_proc_list_for('us-east-1', instance)) dtnow = datetime.datetime.now() if len(longs) > 0 and (dtnow - last_flush) > flush_interval: last_flush = datetime.datetime.now() flush_and_email() time.sleep(5) except KeyboardInterrupt: print "Exiting now" except Exception as e: logger.warn( "Longprocs failed: %s", e, exc_info=1)
def get_rds_instances_by_region(self, region): ''' Makes an AWS API call to the list of RDS instances in a particular region ''' try: if self.creds == {}: conn = rds.connect_to_region(region) else: conn = rds.connect_to_region(region, aws_access_key_id=self.creds['key'], aws_secret_access_key=self.creds['secret']) if conn: instances = conn.get_all_dbinstances() for instance in instances: self.add_rds_instance(instance, region) except boto.exception.BotoServerError, e: if not e.reason == "Forbidden": print "Looks like AWS RDS is down: " print e sys.exit(1)
def _create_rds_connection(self): conn = rds.connect_to_region( self.options.get('region_name'), aws_access_key_id=self.options.get( 'aws_access_key_id' ), aws_secret_access_key=self.options.get( 'aws_secret_access_key' ) ) return conn
def get_connection(cls, region=None): """ Connect to the given region, and return the connection. :param region: Defaults to ``awsfab_settings.DEFAULT_REGION`` if ``None``. """ region = region is None and awsfab_settings.DEFAULT_REGION or region connection = connect_to_region(region_name=region, **awsfab_settings.AUTH) if not connection: raise RdsRegionConnectionError(region) return connection
def get_rds_instances_by_region(self, region): ''' Makes an AWS API call to the list of RDS instances in a particular region''' try: conn = rds.connect_to_region(region) if conn: instances = conn.get_all_dbinstances() for instance in instances: self.add_rds_instance(instance, region) except boto.exception.BotoServerError, e: if not e.reason == "Forbidden": print "Looks like AWS RDS is down: " print e sys.exit(1)
def start_db_connection(): c = rds.connect_to_region("us-west-2") instances = c.get_all_dbinstances() db = instances[0] target_name = db.endpoint[0] dbc = MySQLdb.connect( host=target_name, user='******', passwd='fluffybunny', db='ecdb_beta') print 'Started DBC' return dbc
def get_rds_instances_by_region(self, region): ''' Makes an AWS API call to the list of RDS instances in a particular region ''' try: conn = rds.connect_to_region(region) if conn: instances = conn.get_all_dbinstances() for instance in instances: self.add_rds_instance(instance, region) except boto.exception.BotoServerError as e: print "Looks like AWS RDS is down: " print e sys.exit(1)
def __get_rds_instances_by_region(self, region): ''' Makes an AWS API call to the list of RDS instances in a particular region ''' try: conn = rds.connect_to_region(region, aws_access_key_id=self.ec2_access_key, aws_secret_access_key=self.ec2_secret_key) if conn: instances = conn.get_all_dbinstances() for instance in instances: yield instance except boto.exception.BotoServerError as e: print "RDS BotoServerError: " print e sys.exit(1)
def get_rds_instances_by_region(self, region): ''' Makes an AWS API call to the list of RDS instances in a particular region ''' try: conn = rds.connect_to_region(region) if conn: instances = conn.get_all_dbinstances() for instance in instances: self.add_rds_instance(instance, region) except boto.exception.BotoServerError, e: if e.error_code == 'AuthFailure': error = self.get_auth_error_message() if not e.reason == "Forbidden": error = "Looks like AWS RDS is down:\n%s" % e.message self.fail_with_error(error)
def cmd_handler(args): ''' Main entry point for the sub-command. :param args: The command line arguments as parsed by argparse ''' logging.debug('Starting snapshot-rds') try: conn = connect_to_region(args.region, aws_access_key_id=args.access_key, aws_secret_access_key=args.secret_key, security_token=args.token) except Exception, e: logging.critical('Failed to connect to RDS: "%s"' % e.error_message) return
def get_rds_instances_by_region(self, region): ''' Makes an AWS API call to the list of RDS instances in a particular region ''' # Hack to get the AWS account id. # Amazon does not provide any easy way to get it. ec2_conn = ec2.connect_to_region(region) sg = ec2_conn.get_all_security_groups() account_id = sg[0].owner_id try: conn = rds.connect_to_region(region) conn2 = rds2.connect_to_region(region) # To get RDS tags. if conn: instances = conn.get_all_dbinstances() for instance in instances: # NOTE: Boto 2.27.0 (latest as of 3/24/2014) # is not able to get tags from RDS instances # in a way like it can get EC2 tags. # Until there is a better solution, the following works. # Construct the ARN for this RDS instance, # so we can get its tags. arn = ':'.join([ 'arn', 'aws', 'rds', region, account_id, 'db', instance.id ]) # Get its raw tagset and # standardize it to the way Boto presents # EC2 tags. tagset = conn2.list_tags_for_resource(arn)['ListTagsForResourceResponse']['ListTagsForResourceResult']['TagList'] instance.tags = {tag['Key']: tag['Value'] for tag in tagset} self.add_rds_instance(instance, region) except boto.exception.BotoServerError, e: if not e.reason == "Forbidden": print "Looks like AWS RDS is down: " print e sys.exit(1)
def __get_rds_instances_by_region(self, region): ''' Makes an AWS API call to the list of RDS instances in a particular region ''' try: conn = rds.connect_to_region( region, aws_access_key_id=self.ec2_access_key, aws_secret_access_key=self.ec2_secret_key) if conn: instances = conn.get_all_dbinstances() for instance in instances: yield instance except boto.exception.BotoServerError as e: print "RDS BotoServerError: " print e sys.exit(1)
def get_rds_instances_by_region(region, inventory, index, records=None): """ Makes an AWS API call to the list of RDS instances in a particular region """ try: conn = rds.connect_to_region(region) if conn: instances = conn.get_all_dbinstances() for instance in instances: add_rds_instance(instance, region, inventory, index, records) except boto.exception.BotoServerError as e: print "Looks like AWS RDS is down: " print e sys.exit(1)
def initialise_connections(region): # FOR TESTING ON REMOTE MACHINE # ec2connection = ec2.connect_to_region(region_name=region, # aws_access_key_id=c.ACCESS_KEY_ID, # aws_secret_access_key=c.SECRET_ACCESS_KEY) # rds_connection = rds.connect_to_region(region_name=region, # aws_access_key_id=c.ACCESS_KEY_ID, # aws_secret_access_key=c.SECRET_ACCESS_KEY) # cw_connection = cwatch.connect_to_region(region_name=region, # aws_access_key_id=c.ACCESS_KEY_ID, # aws_secret_access_key=c.SECRET_ACCESS_KEY) ec2connection = ec2.connect_to_region(region_name=region, profile_name=sys.argv[1]) rds_connection = rds.connect_to_region(region_name=region, profile_name=sys.argv[1]) cw_connection = cwatch.connect_to_region(region_name=region, profile_name=sys.argv[1]) mysql_connection = func.connect_to_mysql_server() return ec2connection, rds_connection, cw_connection, mysql_connection
def create_rds_snapshot(access_key, secret_key, token): print('*******************************Starting snapshot-rds***************************************') password=raw_input("Enter RDS password for expoliting, Note that it does not prompt for old password!!!:") region='ap-southeast-1' rds_name='nimbostratus' print('\n Access Key: %s \nsecret_key: %s, \ntoken: %s' %(access_key, secret_key, token)) try: conn = connect_to_region(region, aws_access_key_id=access_key, aws_secret_access_key=secret_key, security_token=token) except Exception, e: print('Failed to connect to RDS: "%s"' % e.error_message) return
def get_running_instance_count(body): """Function to get running rds instance count Args: body (json string): post body in json string Returns: TYPE: Description """ body = dict(body) conn = rds.connect_to_region( region_name='us-west-2', aws_access_key_id=body["aws_access_key_id"], aws_secret_access_key=body["aws_secret_access_key"]) instance_status = conn.get_all_dbinstances() running_instances = 0 for item in instance_status: if (item.state_name == 'available'): running_instances += 1 return {"data": running_instances}
def get_rds_instances_by_region(self, region, assumedRoleObject): ''' Makes an AWS API call to the list of RDS instances in a particular region ''' try: conn = rds.connect_to_region(region, aws_access_key_id=assumedRoleObject.credentials.access_key, aws_secret_access_key=assumedRoleObject.credentials.secret_key, security_token=assumedRoleObject.credentials.session_token) if conn: instances = conn.get_all_dbinstances() for instance in instances: self.add_rds_instance(instance, region) except boto.exception.BotoServerError as e: error = e.reason if e.error_code == 'AuthFailure': error = self.get_auth_error_message() if not e.reason == "Forbidden": error = "Looks like AWS RDS is down:\n%s" % e.message self.fail_with_error(error)
def RdsList(RdsConn, Regions, Vpcid=''): """ Return list of AWSrds objects (optional filter by RDS instance) """ retlist = [] if type(Regions) is not list: raise TypeError('Region needs to be a list') for Reg in Regions: _regConn = rds.connect_to_region(Reg) get_allRds = _regConn.get_all_dbinstances() for _rds in get_allRds: # set Rds SubnetGroup dictionary # dbSubDict = {'name': '', 'subnets': [], 'description': ''} try: if _rds.DBSubnetGroupName: get_DbSubGrp = _regConn.get_all_db_subnet_groups(name=_rds.DBSubnetGroupName) _g = get_DbSubGrp[0] dbSubDict['name'] = _g.name dbSubDict['description'] = _g.description dbSubDict['subnets'] = _g.subnet_ids except AttributeError as e: pass if Vpcid: try: if _rds.VpcId == Vpcid: retlist.append(AWSrds(_rds, dbSubDict)) except AttributeError: # VpcId Attribute only exists if Rds Object is in a VPC # pass elif not Vpcid: retlist.append(AWSrds(_rds, dbSubDict)) return retlist
from fabric.operations import open_shell from fabric.utils import puts, indent from fabric.context_managers import prefix, settings, cd from fabric.contrib.console import confirm from fabric import colors import boto from boto import ec2, rds from os import path, urandom from hashlib import sha1 import time import ConfigParser import yaml region = 'us-west-2' ec2_connection = ec2.get_region(region).connect() rds_connection = rds.connect_to_region(region) env.security_group = 'wsgi_test' # wsgi_test env.instance_type = 't1.micro' env.keypair = 'wsgi_test' env.user = '******' env.key_filename = path.join('keypairs', 'wsgi_test.pem') @task def app(name): env.name = name env.hosts = get_instance_hostnames(name) @task
def get_info(self): ## boto initial connection required before connecting to a region ## boto.connect_vpc() awsRegionConn = BotoVPC.connect_to_region(self.region) for _vid in awsRegionConn.get_all_vpcs(): if _vid.id == self.vpc_id: self._valid = True self.cidr_block = _vid.cidr_block self.region = _vid.region.name for _sub in awsRegionConn.get_all_subnets(filters=[('vpcId', _vid.id)]): self.subnets[_sub.id] = { 'cidr': _sub.cidr_block, 'zone': _sub.availability_zone } if 'Name' in _vid.tags.keys(): self.name = _vid.tags['Name'] if 'Description' in _vid.tags.keys(): self.description = _vid.tags['Description'] for _sg in awsRegionConn.get_all_security_groups( filters={'vpc_id': self.vpc_id}): self.security_groups[_sg.id] = { 'name': _sg.name, 'description': _sg.description } for _resv in awsRegionConn.get_all_instances( filters={'vpc_id': self.vpc_id}): _inst = _resv.instances[0] self.instances[_inst.id] = { 'private_ip': _inst.private_ip_address, 'public_ip': _inst.ip_address, 'state': _inst.state, 'subnet_id': _inst.subnet_id, 'type': _inst.instance_type, 'avail_zone': _inst.placement, } if 'Name' in _inst.tags.keys(): self.instances[_inst.id]['Name'] = _inst.tags['Name'] # get RDS info - Call is required before switching regions# boto.connect_rds() rdsRegionConn = BotoRDS.connect_to_region(self.region) for _rds in rdsRegionConn.get_all_dbinstances(): try: if _rds.VpcId == self.vpc_id: self.rds[_rds.id] = { 'zone': _rds.availability_zone, 'class': _rds.instance_class, 'size': _rds.allocated_storage, } try: self.rds[_rds.id]['db_name'] = _rds.DBName except AttributeError as e: self.rds[_rds.id]['db_name'] = '' except AttributeError as e: # VpcId attribute is not available if not in a VPC # pass if not self._valid: raise VpcIdError("VpcId: %s Not found in AWS account!" % (self.vpc_id))
def __init__(self, region, **kwargs): self.conn = EC2Connection(region=get_region(region), **kwargs) self.rds = connect_to_region(region, **kwargs)
from fabric.operations import open_shell from fabric.utils import puts, indent from fabric.context_managers import prefix, settings, cd from fabric.contrib.console import confirm from fabric import colors import boto from boto import ec2, rds from os import path, urandom from hashlib import sha1 import time import ConfigParser import yaml region = 'us-west-2' ec2_connection = ec2.get_region(region).connect() rds_connection = rds.connect_to_region(region) env.security_group = 'wsgi_test' # wsgi_test env.instance_type = 't1.micro' env.keypair = 'wsgi_test' env.user = '******' env.key_filename = path.join('keypairs', 'wsgi_test.pem') @task def app(name): env.name = name env.hosts = get_instance_hostnames(name) @task def launch(repo = 'https://github.com/mattupstate/ansible-tutorial.git', tags = 'setup,deploy'): with settings(warn_only = True):
def __init__(self, aws_id, aws_secret_key, aws_region): """Sets aws rds connection.""" self.conn = rds.connect_to_region( aws_region, aws_access_key_id=aws_id, aws_secret_access_key=aws_secret_key)
def __init__(self, aws_id, aws_secret_key, aws_region): """Sets aws rds connection.""" self.conn = rds.connect_to_region(aws_region, aws_access_key_id=aws_id, aws_secret_access_key=aws_secret_key)
''' README @Vikash This script will generate RDS Snapshot ''' from boto import rds from boto import vpc import datetime connection=rds.connect_to_region('us-east-1',aws_access_key_id='',aws_secret_access_key='') dbInstances=connection.get_all_dbinstances() for instance in dbInstances: print "%s\t%s\t %s" %(instance.status,instance.endpoint,instance.id) _date=(datetime.date.today().strftime("%d-%B-%Y")) for instance in dbInstances: instance.snapshot(instance.id+_date)
def get_connection(): c = connect_to_region( config.region, aws_access_key_id=config.aws_access_key_id, aws_secret_access_key=config.aws_secret_access_key ) return c
def get_ec2(self, config): return rds.connect_to_region(self.get_region())
def __init__(self, region='us-east-1'): self.region = region self.ec2_conn = ec2.connect_to_region(self.region) self.rds_conn = rds.connect_to_region(self.region)
''' Returns a connection object to AWS. Defaults, service=ec2, region=us-east-1 ''' clean_env() self.conf = '%s/%s/%s.ini' % (os.getenv('AWS_CRED_DIR'), acct_name, acct_name) try: boto.config.load_credential_file(self.conf) except IOError, msg: print >> sys.stderr, 'ERROR: %s' % msg return False if service == 's3': self.conn = s3.connect_to_region(region) if service == 'ec2': self.conn = ec2.connect_to_region(region) if service == 'rds': self.conn = rds.connect_to_region(region) if service == 'rds2': self.conn = rds2.connect_to_region(region) if service == 'elb': self.conn = elb.connect_to_region(region) if service == 'sqs': self.conn = sqs.connect_to_region(region) if service == 'emr': self.conn = emr.connect_to_region(region) if service == 'route53': self.conn = route53.connect_to_region(region) if service == 'iam': self.conn = iam.connect_to_region('universal') if not self.conn: print >> sys.stderr, 'ERROR: Unknown service' return False