{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b54f92ec",
   "metadata": {},
   "source": [
    "### This notebook shows how integration with AWS Sagemaker works\n",
    "- how to create endpoint using model saved in MLFlow\n",
    "- how to predict using Sagemaker endpoint\n",
    "- how to delete resources if inference setup is no more needed"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f44544b2",
   "metadata": {},
   "source": [
    "<b>NB! By running this notebook you will take responsibility for shutting down/deleting created Sagemaker resources.</b>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5680019b",
   "metadata": {},
   "source": [
    "Hopefully in the near future there will be availability for serverless endpoints also as it's currently in development: https://github.com/mlflow/mlflow/pull/9085/files"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ab8562f",
   "metadata": {},
   "source": [
    "<b>Notebook setup</b>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "feb761f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import libraries and setup connection\n",
    "import mlflow\n",
    "import mlflow.deployments\n",
    "\n",
    "import random\n",
    "import pandas as pd\n",
    "import boto3\n",
    "import json\n",
    "from sklearn import datasets\n",
    "\n",
    "tracking_uri = 'https://dev-orch-mlflow-service.dev.theorchard.io'\n",
    "mlflow.set_tracking_uri(tracking_uri)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4313582e",
   "metadata": {},
   "source": [
    "<b>Prerequisities:</b><br>\n",
    "    In order to run next steps, MLFlow image repository must reside in the AWS ECR"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b236385",
   "metadata": {},
   "source": [
    "<b>Deploy model</b>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "270b46fe",
   "metadata": {},
   "source": [
    "Make sure deployment name will be less than 35 characters because deployment will add random postfix like this at the end:<br>\n",
    "    <i>-model-borg4qqw1236zexafyvabcd</i><br>\n",
    "But maximum 64 characters are allowed.<br>\n",
    "This is used to distinquish deployment related objects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3eda5ded",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using deployment name: sagemaker-sk-randforest-94\n"
     ]
    }
   ],
   "source": [
    "rand_id = random.randint(0, 100)\n",
    "run_id = '2695c2d819bc413a8ac3582a474cc95f' # rund id of the champion model chosen in the \"model_comparison\" notebook\n",
    "deployment_name = f'sagemaker-sk-randforest-{rand_id}'\n",
    "print(f'Using deployment name: {deployment_name}')\n",
    "image_ecr_url = \"103233932089.dkr.ecr.us-east-1.amazonaws.com/mlflow-base-venv:2.5.0\"\n",
    "region = \"us-east-1\"\n",
    "model_uri = \"runs:/\" + run_id + \"/model\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "194d1269",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023/09/11 13:17:55 INFO mlflow.sagemaker: Using the python_function flavor for deployment!\n",
      "2023/09/11 13:17:55 INFO mlflow.sagemaker: tag response: {'ResponseMetadata': {'RequestId': 'F2GSERQX22D2EA0M', 'HostId': 'Q9YmBMNaDclD0QhE7Lyui5TyMrtEgNYL5Nc66l4v/DnwjyY/QZz8qXmR3nxlKYhIrabZFwWy6LY=', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amz-id-2': 'Q9YmBMNaDclD0QhE7Lyui5TyMrtEgNYL5Nc66l4v/DnwjyY/QZz8qXmR3nxlKYhIrabZFwWy6LY=', 'x-amz-request-id': 'F2GSERQX22D2EA0M', 'date': 'Mon, 11 Sep 2023 13:17:56 GMT', 'x-amz-version-id': 'LdvzFZSIoJtSmjjjotUMRkWG9iYMTkiB', 'server': 'AmazonS3', 'content-length': '0'}, 'RetryAttempts': 0}, 'VersionId': 'LdvzFZSIoJtSmjjjotUMRkWG9iYMTkiB'}\n",
      "2023/09/11 13:17:55 INFO mlflow.sagemaker: Creating new endpoint with name: sagemaker-sk-randforest-94 ...\n",
      "2023/09/11 13:17:56 INFO mlflow.sagemaker: Created model with arn: arn:aws:sagemaker:us-east-1:103233932089:model/sagemaker-sk-randforest-94-model-borg4qqwqi6zeaafyvcqyg\n",
      "2023/09/11 13:17:57 INFO mlflow.sagemaker: Created endpoint configuration with arn: arn:aws:sagemaker:us-east-1:103233932089:endpoint-config/sagemaker-sk-randforest-94-config-vabt3jphabtt2wwwaxiqvd-q\n",
      "2023/09/11 13:17:57 INFO mlflow.sagemaker: Created endpoint with arn: arn:aws:sagemaker:us-east-1:103233932089:endpoint/sagemaker-sk-randforest-94\n",
      "2023/09/11 13:17:57 INFO mlflow.sagemaker: Waiting for the deployment operation to complete...\n",
      "2023/09/11 13:17:57 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:18:17 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:18:38 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:18:58 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:19:18 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:19:38 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:19:58 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:20:19 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:20:39 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:20:59 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:21:19 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:21:39 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:22:00 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:22:20 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:22:40 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:23:00 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:23:20 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:23:41 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:24:01 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:24:21 INFO mlflow.sagemaker: Waiting for endpoint to reach the \"InService\" state. Current endpoint status: \"Creating\"\n",
      "2023/09/11 13:24:41 INFO mlflow.sagemaker: The deployment operation completed successfully with message: \"The SageMaker endpoint was created successfully.\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'name': 'sagemaker-sk-randforest-94', 'flavor': 'python_function'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "deployment_client = mlflow.deployments.get_deploy_client(\"sagemaker:/\" + region)\n",
    "\n",
    "deployment_client.create_deployment(\n",
    "    name=deployment_name,\n",
    "    model_uri=model_uri,\n",
    "    config={\n",
    "      \"image_url\": image_ecr_url,\n",
    "        \"bucket\": \"dev-cucumbers\",\n",
    "      #\"execution_role_arn\": exec_role\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ec650a5",
   "metadata": {},
   "source": [
    "Check deployment status"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ade8ffea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MLflow SageMaker Deployment status is: InService\n"
     ]
    }
   ],
   "source": [
    "deployment_info = deployment_client.get_deployment(name=deployment_name)\n",
    "print(f\"MLflow SageMaker Deployment status is: {deployment_info['EndpointStatus']}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7ae7483",
   "metadata": {},
   "source": [
    "We also can see from AWS console that endpoint is running:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95b6fa06",
   "metadata": {},
   "source": [
    "![running_endpoint.png](attachment:running_endpoint.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7707315f",
   "metadata": {},
   "source": [
    "List all deployments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "477840b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The following MLflow SageMaker Deployments exist in us-east-1: \n",
      " ['sagemaker-sk-randforest-94']\n"
     ]
    }
   ],
   "source": [
    "deployments = deployment_client.list_deployments()\n",
    "\n",
    "deployment_names = [deployment['EndpointName'] for deployment in deployments]\n",
    "\n",
    "print(f\"The following MLflow SageMaker Deployments exist in {region}: \\n {deployment_names}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e55a88be",
   "metadata": {},
   "source": [
    "In overall this deplyoment created following objects:\n",
    "* model artifact - located in S3 dev-cucumbers bucket\n",
    "* model - located in Sagemaker -> models\n",
    "* model configuraiton - located in Sagemaker -> endpoint configurations\n",
    "* model endpoint - located in Sagemaker -> Endpoints"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b8813146",
   "metadata": {},
   "outputs": [],
   "source": [
    "iris = datasets.load_iris()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9f659058",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.4 0.2]\n",
      " [3.5 1. ]\n",
      " [5.1 1.9]]\n",
      "[0 1 2]\n"
     ]
    }
   ],
   "source": [
    "# Load some data for inference\n",
    "inference_df = iris.data[[0,60, 101], 2:]\n",
    "print(inference_df)\n",
    "\n",
    "targets = iris.target[[0,60, 101]]\n",
    "print(targets)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09fb8595",
   "metadata": {},
   "source": [
    "### Get inferences from Sagemaker endpoint"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a88f6e76",
   "metadata": {},
   "source": [
    "<b>Predict using Mlflow deployment reference</b>:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "44e4cc0e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Prediction response: {'predictions': [0.0, 1.0, 2.0]}\n"
     ]
    }
   ],
   "source": [
    "predictions = deployment_client.predict(deployment_name, inference_df)\n",
    "\n",
    "print(f\"Prediction response: {predictions}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5b4b60c",
   "metadata": {},
   "source": [
    "<b>Predict using boto3 library</b>:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4c28c0c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "endpoint=deployment_name\n",
    "client = boto3.client('sagemaker-runtime')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9640b51e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>column_a</th>\n",
       "      <th>column_b</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3.5</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5.1</td>\n",
       "      <td>1.9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   column_a  column_b\n",
       "0       1.4       0.2\n",
       "1       3.5       1.0\n",
       "2       5.1       1.9"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_df = pd.DataFrame(inference_df, columns = ['column_a','column_b'])\n",
    "query_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9b28a94e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'dataframe_split': {'columns': ['column_a', 'column_b'], 'data': [[1.4, 0.2], [3.5, 1.0], [5.1, 1.9]]}}\n"
     ]
    }
   ],
   "source": [
    "input_data = {\"dataframe_split\": {\"columns\": query_df.to_dict(orient=\"split\")['columns'], \"data\": query_df.to_dict(orient=\"split\")['data']}}\n",
    "print(input_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b9f0284",
   "metadata": {},
   "source": [
    "Using json type of data for inference:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b38a9f10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"predictions\": [0.0, 1.0, 2.0]}\n"
     ]
    }
   ],
   "source": [
    "data = {\"dataframe_split\": {\"columns\": query_df.to_dict(orient=\"split\")['columns'], \"data\": query_df.to_dict(orient=\"split\")['data']}}\n",
    "\n",
    "prediction = client.invoke_endpoint(\n",
    "    EndpointName=endpoint,\n",
    "    Body=json.dumps(input_data),\n",
    "    ContentType='application/json'\n",
    ")\n",
    "prediction = prediction['Body'].read().decode(\"ascii\")\n",
    "print(prediction)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a9bd5b1",
   "metadata": {},
   "source": [
    "Manually constructing input for inference:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "25554596",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'predictions': [0.0, 1.0, 2.0]}\n"
     ]
    }
   ],
   "source": [
    "prediction = client.invoke_endpoint(\n",
    "    EndpointName=endpoint,\n",
    "    Body=\"{\\\"dataframe_split\\\": {\\\"columns\\\":[\\\"column_a\\\", \\\"column_b\\\"],\\\"data\\\":[[1.4, 0.2], [3.5, 1.0], [5.1, 1.9]]}}\",\n",
    "    ContentType='application/json'\n",
    ")\n",
    "\n",
    "print(str(json.loads(prediction['Body'].read())))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "730d7c47",
   "metadata": {},
   "source": [
    "### Delete endpoint"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13655b38",
   "metadata": {},
   "source": [
    "Command <b>delete_deployment</b> will delete endpoint, endpoint configuration, model and related S3 folder.<br>\n",
    "<b>get_deployment</b> command also fail and this is expected becaue the endpoint is not anymore available. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a0116ffe",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023/09/11 13:25:40 INFO mlflow.sagemaker: Deleted endpoint with arn: arn:aws:sagemaker:us-east-1:103233932089:endpoint/sagemaker-sk-randforest-94\n",
      "2023/09/11 13:25:40 INFO mlflow.sagemaker: Waiting for the delete operation to complete...\n",
      "2023/09/11 13:25:40 INFO mlflow.sagemaker: Deletion is still in progress. Current endpoint status: Deleting\n",
      "2023/09/11 13:25:45 INFO mlflow.sagemaker: The deletion operation completed successfully with message: \"The SageMaker endpoint was deleted successfully.\"\n",
      "2023/09/11 13:25:45 INFO mlflow.sagemaker: Cleaning up unused resources...\n",
      "2023/09/11 13:25:45 INFO mlflow.sagemaker: Deleted associated endpoint configuration with arn: arn:aws:sagemaker:us-east-1:103233932089:endpoint-config/sagemaker-sk-randforest-94-config-vabt3jphabtt2wwwaxiqvd-q\n",
      "2023/09/11 13:25:46 INFO mlflow.sagemaker: Deleted associated model with arn: arn:aws:sagemaker:us-east-1:103233932089:model/sagemaker-sk-randforest-94-model-borg4qqwqi6zeaafyvcqyg\n"
     ]
    },
    {
     "ename": "MlflowException",
     "evalue": "There was an error while retrieving the deployment: An error occurred (ValidationException) when calling the DescribeEndpoint operation: Could not find endpoint \"arn:aws:sagemaker:us-east-1:103233932089:endpoint/sagemaker-sk-randforest-94\".\n",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mClientError\u001b[0m                               Traceback (most recent call last)",
      "File \u001b[0;32m~/anaconda3/envs/python3/lib/python3.10/site-packages/mlflow/sagemaker/__init__.py:2657\u001b[0m, in \u001b[0;36mSageMakerDeploymentClient.get_deployment\u001b[0;34m(self, name, endpoint)\u001b[0m\n\u001b[1;32m   2654\u001b[0m     sage_client \u001b[38;5;241m=\u001b[39m boto3\u001b[38;5;241m.\u001b[39mclient(\n\u001b[1;32m   2655\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msagemaker\u001b[39m\u001b[38;5;124m\"\u001b[39m, region_name\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mregion_name, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39massume_role_credentials\n\u001b[1;32m   2656\u001b[0m     )\n\u001b[0;32m-> 2657\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43msage_client\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdescribe_endpoint\u001b[49m\u001b[43m(\u001b[49m\u001b[43mEndpointName\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   2658\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m exc:\n",
      "File \u001b[0;32m~/anaconda3/envs/python3/lib/python3.10/site-packages/botocore/client.py:535\u001b[0m, in \u001b[0;36mClientCreator._create_api_method.<locals>._api_call\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m    534\u001b[0m \u001b[38;5;66;03m# The \"self\" in this scope is referring to the BaseClient.\u001b[39;00m\n\u001b[0;32m--> 535\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_make_api_call\u001b[49m\u001b[43m(\u001b[49m\u001b[43moperation_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/anaconda3/envs/python3/lib/python3.10/site-packages/botocore/client.py:980\u001b[0m, in \u001b[0;36mBaseClient._make_api_call\u001b[0;34m(self, operation_name, api_params)\u001b[0m\n\u001b[1;32m    979\u001b[0m     error_class \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mexceptions\u001b[38;5;241m.\u001b[39mfrom_code(error_code)\n\u001b[0;32m--> 980\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m error_class(parsed_response, operation_name)\n\u001b[1;32m    981\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n",
      "\u001b[0;31mClientError\u001b[0m: An error occurred (ValidationException) when calling the DescribeEndpoint operation: Could not find endpoint \"arn:aws:sagemaker:us-east-1:103233932089:endpoint/sagemaker-sk-randforest-94\".",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mMlflowException\u001b[0m                           Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[16], line 3\u001b[0m\n\u001b[1;32m      1\u001b[0m deployment_client\u001b[38;5;241m.\u001b[39mdelete_deployment(name\u001b[38;5;241m=\u001b[39mdeployment_name)\n\u001b[0;32m----> 3\u001b[0m validation_info \u001b[38;5;241m=\u001b[39m \u001b[43mdeployment_client\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_deployment\u001b[49m\u001b[43m(\u001b[49m\u001b[43mname\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdeployment_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m      4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mMLflow SageMaker Deployment status is: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mvalidation_info[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mEndpointStatus\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[0;32m~/anaconda3/envs/python3/lib/python3.10/site-packages/mlflow/sagemaker/__init__.py:2659\u001b[0m, in \u001b[0;36mSageMakerDeploymentClient.get_deployment\u001b[0;34m(self, name, endpoint)\u001b[0m\n\u001b[1;32m   2657\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m sage_client\u001b[38;5;241m.\u001b[39mdescribe_endpoint(EndpointName\u001b[38;5;241m=\u001b[39mname)\n\u001b[1;32m   2658\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m exc:\n\u001b[0;32m-> 2659\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m MlflowException(\n\u001b[1;32m   2660\u001b[0m         message\u001b[38;5;241m=\u001b[39m\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThere was an error while retrieving the deployment: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mexc\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   2661\u001b[0m     )\n",
      "\u001b[0;31mMlflowException\u001b[0m: There was an error while retrieving the deployment: An error occurred (ValidationException) when calling the DescribeEndpoint operation: Could not find endpoint \"arn:aws:sagemaker:us-east-1:103233932089:endpoint/sagemaker-sk-randforest-94\".\n"
     ]
    }
   ],
   "source": [
    "deployment_client.delete_deployment(name=deployment_name)\n",
    "\n",
    "validation_info = deployment_client.get_deployment(name=deployment_name)\n",
    "print(f\"MLflow SageMaker Deployment status is: {validation_info['EndpointStatus']}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda_python3",
   "language": "python",
   "name": "conda_python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
