def cloudtrail_cleanup()

in subfunctions/ALE_cleanup_single.py [0:0]


def cloudtrail_cleanup():
    """Function to clean up CloudTrail Logs"""
    logging.info("Cleaning up CloudTrail Logs.")
    try:
        logging.info("Cleaning up CloudTrail Logs created by Assisted Log Enabler for AWS.")
        trail_list: list = []
        removal_list: list = []
        logging.info("DescribeTrails API Call")
        cloudtrail_trails = cloudtrail.describe_trails()
        for trail in cloudtrail_trails['trailList']:
            trail_list.append(trail['TrailARN'])
        logging.info("Listing CloudTrail trails created by Assisted Log Enabler for AWS.")
        print("Full trail list")
        print(trail_list)
        for removal_trail in trail_list:
            logging.info("Checking tags for trails created by Assisted Log Enabler for AWS.")
            logging.info("ListTags API Call")
            trail_tags = cloudtrail.list_tags(
                ResourceIdList=[removal_trail]
            )
            for tag_lists in trail_tags['ResourceTagList']:
                for key_info in tag_lists['TagsList']:
                    print(key_info)
                    if key_info['Key'] == 'workflow' and key_info['Value'] == 'assisted-log-enabler':
                        removal_list.append(removal_trail)
        print("Trails to be removed")
        print(removal_list)
        for delete_trail in removal_list:
            logging.info("Deleting trails created by Assisted Log Enabler for AWS.")
            logging.info("DeleteTrail API Call")
            cloudtrail.delete_trail(
                Name=delete_trail
            )
            logging.info(delete_trail + " has been deleted.")
            time.sleep(1)
    except Exception as exception_handle:
        logging.error(exception_handle)