def print_tags(resource_list, tag_list, mode=''): """ - Prints tag keys, values applied to resources - output: cloudwatch logs - mode: INFO, DBUG, or UNKN (unknown or not provided) """ if mode == 0: mode_text = 'DBUG' else: mode_text = 'INFO' try: for resource in resource_list: logger.info('Tags successfully applied to resource: ' + str(resource)) ct = 0 for t in tag_list: logger.info('tag' + str(ct) + ': ' + str(t['Key']) + ' : ' + str(t['Value'])) ct += 1 if mode == 0: logger.debug('DBUGMODE = True, No tags applied') except Exception as e: logger.critical( "%s: problem printing tag keys or values to cw logs: %s" % (inspect.stack()[0][3], str(e))) return 1 return 0
def log_message(label, msg): """ logs all messages sent to stdout """ if label in critical_status: logger.critical(msg) elif label in warning_status: logger.warning(msg) else: logger.info(msg) return True
def delete_tags(resourceIds, region, tags): """ Removes tags from an EC2 resource """ client = boto3_session('ec2', region) try: for resourceid in resourceIds: response = client.delete_tags( Resources=[resourceid], Tags=tags ) if response['ResponseMetadata']['HTTPStatusCode'] == 200: logger.info('Existing Tags deleted from vol id %s' % resourceid) return True else: logger.warning('Problem deleting existing tags from vol id %s' % resourceid) return False except ClientError as e: logger.critical( "%s: Problem apply tags to ec2 instances (Code: %s Message: %s)" % (inspect.stack()[0][3], e.response['Error']['Code'], e.response['Error']['Message'])) return False
def export_json_object(dict_obj, filename=None, logging=True): """ Summary: exports object to block filesystem object Args: :dict_obj (dict): dictionary object :filename (str): name of file to be exported (optional) Returns: True | False Boolean export status """ try: if filename: try: with open(filename, 'w') as handle: handle.write(json.dumps(dict_obj, indent=4, sort_keys=True)) logger.info( '%s: Wrote %s to local filesystem location' % (inspect.stack()[0][3], filename)) handle.close() except TypeError as e: logger.warning( '%s: object in dict not serializable: %s' % (inspect.stack()[0][3], str(e))) else: json_str = json.dumps(dict_obj, indent=4, sort_keys=True) print(highlight(json_str, lexers.JsonLexer(), formatters.TerminalFormatter()).strip()) if logging: logger.info('%s: successful export to stdout' % inspect.stack()[0][3]) return True except OSError as e: logger.critical( '%s: export_file_object: error writing to %s to filesystem. Error: %s' % (inspect.stack()[0][3], filename, str(e))) return False if logging: logger.info('export_file_object: successful export to %s' % filename) return True
def json_tags(resource_list, tag_list, mode=''): """ - Prints tag keys, values applied to resources - output: cloudwatch logs - mode: INFO, DBUG, or UNKN (unknown or not provided) """ if mode == 0: mode_text = 'DBUG' else: mode_text = 'INFO' try: for resource in resource_list: if mode == 0: logger.debug('DBUGMODE enabled - Print tags found on resource %s:' % str(resource)) else: logger.info('Tags found resource %s:' % str(resource)) print(json.dumps(tag_list, indent=4, sort_keys=True)) except Exception as e: logger.critical( "%s: problem printing tag keys or values to cw logs: %s" % (inspect.stack()[0][3], str(e))) return False return True
def get_regions(): """ Summary. Returns list of region codes for all AWS regions worldwide Returns: TYPE: list """ try: client = boto3.client('ec2') region_response = client.describe_regions() regions = [ region['RegionName'] for region in region_response['Regions'] ] except ClientError as e: logger.critical( "%s: problem retrieving aws regions (Code: %s Message: %s)" % (inspect.stack()[0][3], e.response['Error']['Code'], e.response['Error']['Message'])) raise e return regions