Skip to content

Commit

Permalink
added delete sagemaker script
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandruvesa committed Aug 5, 2024
1 parent 76a4f44 commit 23d3151
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ RAG_MODEL_DEVICE = "cpu"
HUGGING_FACE_HUB_TOKEN="str"

#SAGEMAKER
SAGEMAKER_ENDPOINT_INFERENCE=llm-v1-llm-endpoint
SAGEMAKER_ENDPOINT_INFERENCE=llm-v2-llm-endpoint
SAGEMAKER_MODEL_INFERENCE=huggingface-pytorch-tgi-inference-2024-05-13-10-50-05-168
SAGEMAKER_ENDPOINT_CONFIG_INFERENCE=llm-v1-llm-endpoint
SAGEMAKER_ENDPOINT_CONFIG_INFERENCE=llm-v2-llm-endpoint
SAGEMAKER_INFERENCE_COMPONENT_INFERENCE=None

# SAGEMAKER EXECUTION ROLE
Expand Down
22 changes: 20 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
.PHONY: deploy-inference-endpoint
export AWS_PROFILE=decodingml

.PHONY: help create-sagemaker-role create-sagemaker-execution-role deploy-inference-endpoint delete-inference-endpoint

help:
@echo "Available commands:"
@echo " create-sagemaker-role Create the SageMaker role."
@echo " create-sagemaker-execution-role Create the SageMaker execution role."
@echo " deploy-inference-endpoint Deploy the inference endpoint."
@echo " delete-inference-endpoint Delete the inference endpoint and config."

create-sagemaker-role:
@echo "Creating the SageMaker role..."
poetry run python llm_engineering/core/aws/create_sagemaker_role.py

create-sagemaker-execution-role:
@echo "Creating the SageMaker execution role..."
poetry run python llm_engineering/core/aws/create_sagemaker_execution_role.py

deploy-inference-endpoint:
@echo "Deploying the inference endpoint..."
poetry run python llm_engineering/model/deploy/huggingface/run.py

delete-inference-endpoint:
@if [ -z "$(ENDPOINT_NAME)" ]; then \
echo "Error: ENDPOINT_NAME is not set. Usage: make delete-inference-endpoint ENDPOINT_NAME=<name>"; \
exit 1; \
fi
@echo "Deleting the inference endpoint and config..."
poetry run python llm_engineering/model/delete_inference_endpoint.py $(ENDPOINT_NAME)
68 changes: 68 additions & 0 deletions llm_engineering/model/delete_inference_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import logging
import sys

import boto3
from botocore.exceptions import ClientError

from llm_engineering.settings import settings

# Configure logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)


def delete_endpoint_and_config(endpoint_name):
"""
Deletes an AWS SageMaker endpoint and its associated configuration.
Args:
endpoint_name (str): The name of the SageMaker endpoint to delete.
Returns:
None
"""
try:
sagemaker_client = boto3.client(
"sagemaker",
region_name="eu-central-1",
aws_access_key_id=settings.AWS_ACCESS_KEY,
aws_secret_access_key=settings.AWS_SECRET_KEY,
)
except Exception as e:
logger.error(f"Error creating SageMaker client: {e}")
return

# Delete the endpoint
try:
sagemaker_client.delete_endpoint(EndpointName=endpoint_name)
logger.info(f"Endpoint '{endpoint_name}' deletion initiated.")
except ClientError as e:
logger.error(f"Error deleting endpoint: {e}")
return

# Get the endpoint configuration name
try:
response = sagemaker_client.describe_endpoint(EndpointName=endpoint_name)
config_name = response["EndpointConfigName"]
except ClientError as e:
logger.error(f"Error getting endpoint configuration name: {e}")
return

# Delete the endpoint configuration
try:
sagemaker_client.delete_endpoint_config(EndpointConfigName=config_name)
logger.info(f"Endpoint configuration '{config_name}' deleted.")
except ClientError as e:
logger.error(f"Error deleting endpoint configuration: {e}")


def run():
if len(sys.argv) != 2:
logger.error("Usage: python script_name.py <endpoint_name>")
sys.exit(1)

endpoint_name = sys.argv[1]
logger.info(f"Attempting to delete endpoint: {endpoint_name}")
delete_endpoint_and_config(endpoint_name)


if __name__ == "__main__":
run()

0 comments on commit 23d3151

Please sign in to comment.