def delete_instances(instances) -> list: """Deletes all instances in the instances parameter. Args: instances: A list of instances you want deleted. Returns: A count of deleted instances """ terminated_instances = [] for instance in instances: instance_id = instance["InstanceId"] if helpers.check_in_whitelist(instance_id, WHITELIST_NAME): continue ec2 = boto3.resource(BOTO3_NAME) try: instance = ec2.Instance(instance_id) instance.terminate() # Terminate the instance except ClientError as error: error_string = "{0} on {1} - {2}".format(error, RESOURCE_NAME, instance_id) print(error_string) terminated_instances.append(error_string) continue terminated_instances.append(instance_id) return terminated_instances
def delete_dynamo(dynamo_client, dynamo_tables) -> list: """Deletes all instances in the instances parameter. Args: dynamo_client: A dunamo db boto3 client. dynamo_tables: A list of tables you want deleted. Returns: A count of deleted tables """ terminated_tables = [] for table in dynamo_tables: table_name = table if helpers.check_in_whitelist(table_name, WHITELIST_NAME): continue try: deletion_response = dynamo_client.delete_table( TableName=table_name) except ClientError as error: error_string = "{0} on {1} - {2}".format(error, RESOURCE_NAME, table_name) print(error_string) terminated_tables.append(error_string) continue terminated_tables.append(table_name) return terminated_tables
def delete_redshift_clusters(redshift_client, cluster_list) -> list: """Deletes all redshift clusters from a list Args: redshift_client: A redshift cluster cluster_list (list): A list of resources Returns: A list of terminated redshift clusters """ terminated_clusters = [] for cluster in cluster_list: cluster_id = cluster["ClusterIdentifier"] if helpers.check_in_whitelist(cluster_id, WHITELIST_NAME): continue try: redshift_client.delete_cluster( ClusterIdentifier=cluster_id, SkipFinalClusterSnapshot=True # TODO make this a option ) except ClientError as error: error_string = "{0} on {1} - {2}".format(error, RESOURCE_NAME, cluster_id) print(error_string) terminated_clusters.append(error_string) continue terminated_clusters.append(cluster_id) return terminated_clusters
def delete_resources(resource_client, resource_list) -> list: """Deletes all resources from a list Args: resource_client (boto3.Client): A boto3 client for the resource resource_list (list): A list of resources Returns: A list of terminated resources """ terminated_resources = [] for resource in resource_list: resource_actual_name = resource[ "ResourceName"] # Get the name used for the deletion here. if helpers.check_in_whitelist(resource_actual_name, WHITELIST_NAME): continue try: resource_client.delete_function(FunctionName=resource_actual_name) except ClientError as error: error_string = "{0} on {1} - {2}".format(error, RESOURCE_NAME, resource_actual_name) print(error_string) terminated_resources.append(error_string) continue terminated_resources.append(resource_actual_name) return terminated_resources
def delete_functions(lambda_client, function_list) -> list: """Deletes all instances in the instances parameter. Args: lambda_client: A lambda boto3 client function_list: A list of instances you want deleted. Returns: A count of deleted instances """ terminated_functions = [] for lambda_function in function_list: function_name = lambda_function["FunctionName"] if helpers.check_in_whitelist(function_name, WHITELIST_NAME): continue try: lambda_client.delete_function( FunctionName=function_name ) except ClientError as error: error_string = "{0} on {1} - {2}".format(error, RESOURCE_NAME, function_name) print(error_string) terminated_functions.append(error_string) continue terminated_functions.append(lambda_function["FunctionName"]) return terminated_functions
def delete_dynamo(dynamo_client, dynamo_tables) -> list: """Deletes all instances in the instances parameter. Args: dynamo_client: A dunamo db boto3 client. dynamo_tables: A list of tables you want deleted. Returns: A count of deleted tables """ terminated_tables = [] for table in dynamo_tables: table_name = table if helpers.check_in_whitelist(table_name, WHITELIST_NAME): continue try: deletion_response = dynamo_client.delete_table( TableName=table_name ) except ClientError as error: error_string = "{0} on {1} - {2}".format(error, RESOURCE_NAME, table_name) print(error_string) terminated_tables.append(error_string) continue terminated_tables.append(table_name) return terminated_tables
def delete_file_systems(efs_client, file_system_list) -> list: """Deletes all resources from a list Args: efs_client (boto3.Client): A boto3 client for EFS file_system_list (list): A list of resources Returns: A list of terminated resources """ terminated_file_systems = [] for file_system in file_system_list: file_system_id = file_system["FileSystemId"] # Get the name used for the deletion here. if helpers.check_in_whitelist(file_system_id, WHITELIST_NAME): # TODO need to create a cleaner for the mount targets continue try: efs_client.delete_file_system( FileSystemId=file_system_id ) except ClientError as error: error_string = "{0} on {1} - {2}".format(error, RESOURCE_NAME, file_system_id) print(error_string) terminated_file_systems.append(error_string) continue terminated_file_systems.append(file_system_id) return terminated_file_systems
def delete_rds(rds_client, rds_instances) -> list: """Deletes all instances in the instances parameter. Args: rds_client: A RDS boto3 client. rds_instances: A list of instances you want deleted. Returns: A count of deleted instances """ terminated_instances = [] for instance in rds_instances: rds_indentifier = instance["DBInstanceIdentifier"] if helpers.check_in_whitelist(rds_indentifier, WHITELIST_NAME): continue try: deletion_response = rds_client.delete_db_instance( DBInstanceIdentifier=rds_indentifier, SkipFinalSnapshot=True ) except ClientError as error: error_string = "{0} on {1} - {2}".format(error, RESOURCE_NAME, rds_indentifier) print(error_string) terminated_instances.append(error_string) continue terminated_instances.append(rds_indentifier) return terminated_instances
def delete_file_systems(efs_client, file_system_list) -> list: """Deletes all resources from a list Args: efs_client (boto3.Client): A boto3 client for EFS file_system_list (list): A list of resources Returns: A list of terminated resources """ terminated_file_systems = [] for file_system in file_system_list: file_system_id = file_system[ "FileSystemId"] # Get the name used for the deletion here. if helpers.check_in_whitelist( file_system_id, WHITELIST_NAME ): # TODO need to create a cleaner for the mount targets continue try: efs_client.delete_file_system(FileSystemId=file_system_id) except ClientError as error: error_string = "{0} on {1} - {2}".format(error, RESOURCE_NAME, file_system_id) print(error_string) terminated_file_systems.append(error_string) continue terminated_file_systems.append(file_system_id) return terminated_file_systems
def delete_buckets(buckets) -> list: """Deletes all buckets from a list Args: buckets (list): A list of s3 buckets Returns: A list of terminated buckets """ terminated_buckets = [] for bucket in buckets: bucket_name = bucket["Name"] if helpers.check_in_whitelist(bucket_name, WHITELIST_NAME, is_global=True): continue s3 = boto3.resource(BOTO3_NAME) bucket = s3.Bucket(bucket_name) # There is a much easier to do this then with pagination. Not sure if it works. try: bucket.objects.all().delete() # Delete the content of the bucket bucket.delete() # Delete the bucket itself except ClientError as error: error_string = "{0} on {1} - {2}".format(error, RESOURCE_NAME, bucket_name) print(error_string) terminated_buckets.append(error_string) terminated_buckets.append(bucket_name) return terminated_buckets
def delete_rds(rds_client, rds_instances) -> list: """Deletes all instances in the instances parameter. Args: rds_client: A RDS boto3 client. rds_instances: A list of instances you want deleted. Returns: A count of deleted instances """ terminated_instances = [] for instance in rds_instances: rds_indentifier = instance["DBInstanceIdentifier"] if helpers.check_in_whitelist(rds_indentifier, WHITELIST_NAME): continue try: deletion_response = rds_client.delete_db_instance( DBInstanceIdentifier=rds_indentifier, SkipFinalSnapshot=True) except ClientError as error: error_string = "{0} on {1} - {2}".format(error, RESOURCE_NAME, rds_indentifier) print(error_string) terminated_instances.append(error_string) continue terminated_instances.append(rds_indentifier) return terminated_instances
def delete_ecs_clusters(cluster_client, cluster_list) -> list: """Deletes all resources from a list Args: cluster_client: A ecs boto3 client cluster_list (list): A list of resources Returns: A list of terminated resources """ terminated_clusters = [] for cluster in cluster_list: cluster_arn = cluster # Get the name used for the deletion here. if helpers.check_in_whitelist(cluster_arn, WHITELIST_NAME): continue try: cluster_client.delete_cluster(cluster=cluster_arn) except ClientError as error: terminated_clusters.append("{0} on {1} - {2}" "".format(error, RESOURCE_NAME, cluster_arn)) terminated_clusters.append(cluster_arn) return terminated_clusters
def delete_ecs_clusters(cluster_client, cluster_list) -> list: """Deletes all resources from a list Args: cluster_client: A ecs boto3 client cluster_list (list): A list of resources Returns: A list of terminated resources """ terminated_clusters = [] for cluster in cluster_list: cluster_arn = cluster # Get the name used for the deletion here. if helpers.check_in_whitelist(cluster_arn, WHITELIST_NAME): continue try: cluster_client.delete_cluster( cluster=cluster_arn ) except ClientError as error: terminated_clusters.append("{0} on {1} - {2}" "".format(error, RESOURCE_NAME, cluster_arn)) terminated_clusters.append(cluster_arn) return terminated_clusters