Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabubellon/5d0c88959491b2c35c1a6dd3b28cec7d to your computer and use it in GitHub Desktop.
Save gabubellon/5d0c88959491b2c35c1a6dd3b28cec7d to your computer and use it in GitHub Desktop.
import boto3
## DELETANDO TODOS OS ENDPOINT
def get_all_endpoints(sagemaker_session, endpoint_names=[], next_token=""):
response = sagemaker_session.list_endpoints(MaxResults=10, NextToken=next_token)
response_endpoints = response.get("Endpoints")
response_next_token = response.get("NextToken")
for endpoint in response_endpoints:
endpoint_names.append(endpoint.get("EndpointsName"))
if response_next_token and str(response_next_token) != "":
endpoint_names = get_all_endpoints(
sagemaker_session, endpoint_names, response_next_token
)
return endpoint_names
def get_all_endpoint_configs(
sagemaker_session, endpoint_config_names=[], next_token=""
):
response = sagemaker_session.list_endpoint_configs(
MaxResults=10, NextToken=next_token
)
response_endpoint_configs = response.get("EndpointConfigs")
response_next_token = response.get("NextToken")
for endpoint in response_endpoint_configs:
endpoint_config_names.append(endpoint.get("EndpointConfigName"))
if response_next_token and str(response_next_token) != "":
endpoint_config_names = get_all_endpoint_configs(
sagemaker_session, endpoint_config_names, response_next_token
)
return endpoint_config_names
def get_all_models(sagemaker_session, models_names=[], next_token=""):
response = sagemaker_session.list_models(MaxResults=10, NextToken=next_token)
response_models = response.get("Models")
response_next_token = response.get("NextToken")
for endpoint in response_models:
models_names.append(endpoint.get("ModelName"))
if response_next_token and str(response_next_token) != "":
models_names = get_all_models(
sagemaker_session, models_names, response_next_token
)
return models_names
if __name__ == "__main__":
print(f"######################################")
print(f"Script de Limpeza de SageMaker INICIADO")
boto_session = boto3.Session(profile_name="profile-name")å
client = boto_session.client("sagemaker")
print(f"-------------------------------")
print(f"--------ENDPOINT---------------")
dev_endpoint_names = get_all_endpoints(client)
if len(dev_endpoint_names) > 0:
print(f"Os seguintes EndPoint serão excluidos:")
print(f"{dev_endpoint_names}")
choise = input("Confirmar [Y/n]: ")
if choise.upper() == "Y":
for item in dev_endpoint_names:
print(f"Excluindo EndPoint: {item}")
client.delete_endpoint(EndpointName=item)
else:
print("Nenhum EndPoint excluido")
else:
print("Nenhum EndPoint encontrado")
print(f"-------------------------------")
print(f"----ENDPOINT CONFIGURATION-----")
dev_endpoint_config_names = get_all_endpoint_configs(client)
if len(dev_endpoint_config_names) > 0:
print(f"Os seguintes EndPoint Configuration serão excluidos:")
print(f"{dev_endpoint_config_names}")
choise = input("Confirmar [Y/n]: ")
if choise.upper() == "Y":
for item in dev_endpoint_config_names:
print(f"Excluindo EndPoint Configuration: {item}")
client.delete_endpoint_config(EndpointConfigName=item)
else:
print("Nenhum EndPoint Configuration excluido")
else:
print("Nenhum EndPoint Configuration encontrado")
print(f"-------------------------------")
print(f"-----MODEL---------------------")
dev_models_name = get_all_models(client)
if len(dev_models_name) > 0:
print(f"Os seguintes Model serão excluidos:")
print(f"{dev_models_name}")
choise = input("Confirmar [Y/n]: ")
if choise.upper() == "Y":
for item in dev_models_name:
print(f"Excluindo Model: {item}")
client.delete_model(ModelName=item)
else:
print("Nenhum Model excluido")
else:
print("Nenhum Model encontrado")
print(f"Script de Limpeza de SageMaker FINALIZADO")
print(f"#########################################")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment