{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# SageMaker model deployment as CI/CD pipeline\n",
"This notebook demonstrates how to use SageMaker Project template for CI/CD model deployment. You are going to implement:
\n",
"1. Load the data for the iris multi-class classification problem
\n",
"2. Use a SageMaker built-in estimator [XGBoost](https://docs.aws.amazon.com/sagemaker/latest/dg/xgboost.html) to train the model on the dataset
\n",
"3. Create a [SageMaker pipeline](https://docs.aws.amazon.com/sagemaker/latest/dg/pipelines.html) to train and register the model
\n",
"4. Select the latest model package from the model package group and set the status to `Approved` and launch the model deployment CI/CD pipeline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load packages and get environment configuration "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if False:\n",
" !pip install -U sagemaker"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import pandas as pd\n",
"import numpy as np\n",
"import sagemaker\n",
"import json\n",
"import boto3\n",
"from sagemaker import get_execution_role\n",
"import sagemaker.session\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn import datasets\n",
"\n",
"sm = boto3.client(\"sagemaker\")\n",
"ssm = boto3.client(\"ssm\")\n",
"\n",
"def get_environment(project_name, ssm_params):\n",
" r = sm.describe_domain(\n",
" DomainId=sm.describe_project(\n",
" ProjectName=project_name\n",
" )[\"CreatedBy\"][\"DomainId\"]\n",
" )\n",
" del r[\"ResponseMetadata\"]\n",
" del r[\"CreationTime\"]\n",
" del r[\"LastModifiedTime\"]\n",
" r = {**r, **r[\"DefaultUserSettings\"]}\n",
" del r[\"DefaultUserSettings\"]\n",
"\n",
" i = {\n",
" **r,\n",
" **{t[\"Key\"]:t[\"Value\"] \n",
" for t in sm.list_tags(ResourceArn=r[\"DomainArn\"])[\"Tags\"] \n",
" if t[\"Key\"] in [\"EnvironmentName\", \"EnvironmentType\"]}\n",
" }\n",
"\n",
" for p in ssm_params:\n",
" try:\n",
" i[p[\"VariableName\"]] = ssm.get_parameter(Name=f\"{i['EnvironmentName']}-{i['EnvironmentType']}-{p['ParameterName']}\")[\"Parameter\"][\"Value\"]\n",
" except:\n",
" i[p[\"VariableName\"]] = \"\"\n",
"\n",
" return i\n",
"\n",
"def get_session(region, default_bucket):\n",
" \"\"\"Gets the sagemaker session based on the region.\n",
"\n",
" Args:\n",
" region: the aws region to start the session\n",
" default_bucket: the bucket to use for storing the artifacts\n",
"\n",
" Returns:\n",
" sagemaker.session.Session instance\n",
" \"\"\"\n",
"\n",
" boto_session = boto3.Session(region_name=region)\n",
"\n",
" sagemaker_client = boto_session.client(\"sagemaker\")\n",
" runtime_client = boto_session.client(\"sagemaker-runtime\")\n",
" return sagemaker.session.Session(\n",
" boto_session=boto_session,\n",
" sagemaker_client=sagemaker_client,\n",
" sagemaker_runtime_client=runtime_client,\n",
" default_bucket=default_bucket,\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Shutting down your kernel for this notebook to release resources.
\n", "\n", " \n", "" ] } ], "metadata": { "instance_type": "ml.t3.medium", "kernelspec": { "display_name": "Python 3 (Data Science)", "language": "python", "name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-east-1:081325390199:image/datascience-1.0" }, "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.7.10" }, "metadata": { "interpreter": { "hash": "ac2eaa0ea0ebeafcc7822e65e46aa9d4f966f30b695406963e145ea4a91cd4fc" } } }, "nbformat": 4, "nbformat_minor": 4 }