> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Keeping Track of Models Using the SageMaker Model Registry

> Shows how to register, version, approve, and deploy machine learning models using Amazon SageMaker Model Registry with the Python SDK and Boto3

This lesson demonstrates how to use the SageMaker Model Registry to track model artifacts, manage versions, and deploy approved models. The examples are executed from a Jupyter notebook in SageMaker Studio and use the SageMaker Python SDK along with Boto3.

We will:

* register a model artifact as a model package,
* register that model into the SageMaker Model Registry with an approval status, and
* approve a model version and obtain the latest approved model for deployment.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/AEn6k0ZqpTTFBjAr/images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/sagemaker-model-registry-register-approve-deploy.jpg?fit=max&auto=format&n=AEn6k0ZqpTTFBjAr&q=85&s=06b8fbac6adb9ac208938a93aa6b0f6e" alt="A slide titled &#x22;Agenda&#x22; listing three steps for using SageMaker Model Registry: (1) register a model artifact as a model package, (2) register a model with pending approval status into the registry, and (3) approve and deploy a model from the registry." width="1920" height="1080" data-path="images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/sagemaker-model-registry-register-approve-deploy.jpg" />
</Frame>

This notebook covers the following SageMaker SDK usage:

1. create a legacy model package using Model.create,
2. register a model into the Model Registry using Model.register,
3. list and obtain the latest approved package from the registry, and
4. deploy the approved package to an endpoint.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/AEn6k0ZqpTTFBjAr/images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/demo-steps-register-deploy-ml-model.jpg?fit=max&auto=format&n=AEn6k0ZqpTTFBjAr&q=85&s=6a41511d52f8d24135248b0e2ebc72e3" alt="A slide titled &#x22;Demo Steps&#x22; that outlines a four-step workflow for registering and deploying a machine learning model. The steps are: 01 Open Notebook, 02 Register Basic Model Package, 03 Register Model into Model Registry, and 04 Approve & Deploy." width="1920" height="1080" data-path="images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/demo-steps-register-deploy-ml-model.jpg" />
</Frame>

Getting started: open SageMaker Studio, launch JupyterLab, and open the notebook named "Model Registry Demo".

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/AEn6k0ZqpTTFBjAr/images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/sagemaker-jupyterlab-launcher-file-browser.jpg?fit=max&auto=format&n=AEn6k0ZqpTTFBjAr&q=85&s=884962a1a8afbdd20cf8b45edf9b27df" alt="A screenshot of an AWS SageMaker / JupyterLab interface showing a file browser on the left and a Launcher on the right. The Launcher displays notebook, console, and other app icons (Python, Glue/Spark, Terminal, Markdown, etc.)." width="1920" height="1080" data-path="images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/sagemaker-jupyterlab-launcher-file-browser.jpg" />
</Frame>

## 1. Imports and session setup

Start by importing required libraries and creating a SageMaker session and role reference. This session object is used throughout the notebook for SDK operations.

```python theme={null}
# imports and session setup
import boto3
import sagemaker
from sagemaker import get_execution_role
from sagemaker.session import Session
from sagemaker import Model, ModelPackage

sagemaker_session = Session()
role = get_execution_role()

print(role)
```

The printed value is the IAM execution role ARN associated with your notebook environment.

## 2. Legacy model package (Model.create)

If you already have a trained model artifact saved in S3 and want a quick way to create a SageMaker model package (legacy flow), call `Model.create()` by supplying the S3 model artifact and the appropriate container image for the framework.

* model artifact (output of a training job)
* container image URI for the training framework (for example, Linear Learner)

Example:

```python theme={null}
# Reference model artifact in S3 (this is the trained model output)
model_artifact = 's3://sagemaker-eu-central-1-485186561655/house-price-linearlearner-demo/output/linear-learner-2025-05-06-13-48-11-566/output/model.tar.gz'

# Determine region and fetch container image URI for the Linear Learner framework
region = sagemaker_session.boto_region_name
container_image_uri = sagemaker.image_uris.retrieve(framework="linear-learner", region=region)

print(f"SageMaker Linear Learner Image URI: {container_image_uri}")

# Assign a meaningful name to the model
model_name = 'kodekloud-house-prices-demo'

# Create the SageMaker model (legacy model package)
model = Model(
    image_uri=container_image_uri,
    model_data=model_artifact,
    role=role,
    name=model_name
)

# Register the model package (legacy)
model.create()
```

After calling `model.create()`, the model package shows up in the SageMaker console under Inference → Models, and you can create an endpoint directly from that model object for quick testing.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/AEn6k0ZqpTTFBjAr/images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/s3-console-output-model-tar-gz.jpg?fit=max&auto=format&n=AEn6k0ZqpTTFBjAr&q=85&s=8599c7075c5790ed8345ff86ae0c9e53" alt="A screenshot of the Amazon S3 web console showing the contents of an &#x22;output/&#x22; folder. It lists a single object named &#x22;model.tar.gz&#x22; (1.2 KB) with actions like Copy S3 URI, Download, Open, and Delete." width="1920" height="1080" data-path="images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/s3-console-output-model-tar-gz.jpg" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/AEn6k0ZqpTTFBjAr/images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/sagemaker-model-kodekloud-house-prices.jpg?fit=max&auto=format&n=AEn6k0ZqpTTFBjAr&q=85&s=55ad0ccf6a9adf616a0031dbe6b0e25f" alt="A screenshot of the Amazon SageMaker console showing a model page titled &#x22;kodekloud-house-prices-demo&#x22; with model settings, container details, S3 model data location, and network information. The page also shows action buttons (Create endpoint, Create batch transform job) and a large mouse cursor." width="1920" height="1080" data-path="images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/sagemaker-model-kodekloud-house-prices.jpg" />
</Frame>

<Callout icon="lightbulb" color="#1CB2FE">
  Model.create is great for rapid prototyping and one-off endpoints. For production tracking, reproducibility, and approval workflows, prefer the SageMaker Model Registry with Model.register.
</Callout>

## 3. Model Registry: create a Model Package Group and register a model version

The Model Registry stores model versions inside a Model Package Group and provides metadata, metrics, versioning, lineage, and approval workflows. Use the Boto3 SageMaker client to create a model package group, then call `Model.register()` to add a model version.

Create the package group:

```python theme={null}
# Create a model package group using the low-level boto3 client
model_package_group_name = 'house-price-model-registry-demonstration'
model_package_group_description = 'KodeKloud model package group for house price prediction'

sagemaker_client = boto3.client('sagemaker')

try:
    sagemaker_client.create_model_package_group(
        ModelPackageGroupName=model_package_group_name,
        ModelPackageGroupDescription=model_package_group_description
    )
    print(f'Created Model Package Group: {model_package_group_name}')
except sagemaker_client.exceptions.ResourceInUse:
    print(f'Model Package Group {model_package_group_name} already exists.')
```

Register the model artifact into the registry as a model package version. Set an initial approval status (for example, `PendingManualApproval`) and attach any custom metadata you want to track.

```python theme={null}
# Register the model into the model registry
model_approval_status = "PendingManualApproval"
customer_metadata_properties = {"ModelType": "HousePricePrediction"}

model_package = model.register(
    content_types=["text/csv"],
    response_types=["text/csv"],
    inference_instances=["ml.t2.medium", "ml.m5.large"],
    transform_instances=["ml.m5.large"],
    model_package_group_name=model_package_group_name,
    approval_status=model_approval_status,
    customer_metadata_properties=customer_metadata_properties,
)

print(f"Model package version ARN: {model_package.model_package_arn}")
```

After registration, the version appears in SageMaker Studio → Model Registry under the specified Model Package Group with the approval status you supplied.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/AEn6k0ZqpTTFBjAr/images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/sagemaker-model-registry-version-1-metrics.jpg?fit=max&auto=format&n=AEn6k0ZqpTTFBjAr&q=85&s=fea59ecf4854126c54f1bba3b7ad2bb2" alt="A screenshot of the Amazon SageMaker Model Registry &#x22;Version 1&#x22; overview showing a model training status and tabs for Overview/Activity/Lineage. The main panel displays a metrics table with entries like validation:mae, validation:mse, validation:r2 and their numeric values." width="1920" height="1080" data-path="images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/sagemaker-model-registry-version-1-metrics.jpg" />
</Frame>

In the Model Registry UI you can inspect:

* Metrics collected during training (MAE, MSE, R2, etc.),
* Artifacts (training and validation datasets, model artifact S3 paths),
* Hyperparameters used during training,
* The container image associated with the model,
* Activity feed (registration, approval, deployment events),
* Lineage that links datasets, training jobs, and containers to the model.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/AEn6k0ZqpTTFBjAr/images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/sagemaker-studio-model-overview-hyperparams.jpg?fit=max&auto=format&n=AEn6k0ZqpTTFBjAr&q=85&s=cc3e22dabe0fbb3b0ccf7d2607543ffd" alt="A screenshot of an AWS SageMaker Studio model version overview displaying hyperparameters (epochs = 10, mini_batch_size = 32, predictor_type = regressor) with Training marked &#x22;Complete&#x22; and Deploy &#x22;Pending Approval.&#x22; The left sidebar shows navigation items (Performance, Artifacts, Hyperparameters) and a large mouse cursor is visible." width="1920" height="1080" data-path="images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/sagemaker-studio-model-overview-hyperparams.jpg" />
</Frame>

## 4. Approve a model version (in the UI) and deploy the latest approved version

Once a model version is approved in the Model Registry UI (for example, change status from `PendingManualApproval` → `Approved`), you can programmatically find the latest approved version and deploy it.

<Callout icon="warning" color="#FF6B6B">
  Approving a model should follow your organization's validation and governance processes. Only approve models that meet performance, fairness, and security requirements.
</Callout>

List the latest approved model packages in the package group (sorted by creation time) and select the most recent:

```python theme={null}
# List the latest approved model packages in the package group (descending by creation time)
response = sagemaker_client.list_model_packages(
    ModelPackageGroupName=model_package_group_name,
    ModelApprovalStatus='Approved',
    SortBy='CreationTime',
    SortOrder='Descending'
)

if not response.get('ModelPackageSummaryList'):
    raise RuntimeError('No approved model packages found.')

latest_approved_model_package_arn = response['ModelPackageSummaryList'][0]['ModelPackageArn']
print(f'Latest Approved Model Package ARN: {latest_approved_model_package_arn}')
```

Create a ModelPackage object from the approved ARN and deploy it to a real-time endpoint:

```python theme={null}
# Create a ModelPackage object and deploy it
model_package = ModelPackage(
    role=role,
    model_package_arn=latest_approved_model_package_arn,
    sagemaker_session=sagemaker_session
)

predictor = model_package.deploy(
    initial_instance_count=1,
    instance_type="ml.m5.large",
    endpoint_name="latest-house-price-model"
)
```

Deployment can take several minutes. Once deployed, the endpoint will appear under Endpoints in SageMaker Studio/Console.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/AEn6k0ZqpTTFBjAr/images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/sagemaker-studio-house-price-deploy-approved.jpg?fit=max&auto=format&n=AEn6k0ZqpTTFBjAr&q=85&s=07b04e6daded5091997b38292cc83a98" alt="Screenshot of the Amazon SageMaker Studio model version overview for a &#x22;House Price&#x22; model, showing container locations and available instance types. A &#x22;Deploy — Approved&#x22; panel is highlighted with a large cursor over the Deploy button." width="1920" height="1080" data-path="images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/sagemaker-studio-house-price-deploy-approved.jpg" />
</Frame>

Deploying a model created from a Model Registry package will also create a model entry under the legacy Models console (Inference → Models), since the deployed artifact is a managed model package.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/AEn6k0ZqpTTFBjAr/images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/sagemaker-models-two-entries-mouse-cursor.jpg?fit=max&auto=format&n=AEn6k0ZqpTTFBjAr&q=85&s=cd508444ea64efda7371c13766e3366a" alt="A screenshot of the Amazon SageMaker &#x22;Models&#x22; console in a web browser showing two model entries (house-price-model-registry-demonstratio... and kodekloud-house-prices-demo) with the left navigation menu visible. A large black mouse cursor is shown near the center of the screen." width="1920" height="1080" data-path="images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/sagemaker-models-two-entries-mouse-cursor.jpg" />
</Frame>

After deployment you can confirm the endpoint status:

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/AEn6k0ZqpTTFBjAr/images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/sagemaker-studio-endpoint-latest-house-price.jpg?fit=max&auto=format&n=AEn6k0ZqpTTFBjAr&q=85&s=22c00074e9553c11ee9c4b2809a2886a" alt="A screenshot of the Amazon SageMaker Studio &#x22;Endpoints&#x22; page showing one deployed endpoint named &#x22;latest-house-price-model&#x22; with status &#x22;In service.&#x22; The dark-themed UI includes a left navigation sidebar and a large mouse cursor hovering over the endpoint list." width="1920" height="1080" data-path="images/AWS-SageMaker/Persona-SageMaker-Activities-Data-Scientist/Demo-Keeping-Track-of-Models-Using-the-SageMaker-Model-Registry/sagemaker-studio-endpoint-latest-house-price.jpg" />
</Frame>

## Quick comparison: Model.create vs Model.register

| Resource                            | Use Case                                           | Benefits                                                                    |
| ----------------------------------- | -------------------------------------------------- | --------------------------------------------------------------------------- |
| Model.create (legacy model package) | Rapid prototyping, one-off endpoints               | Fast, minimal workflow to create a model package and deploy                 |
| Model.register + Model Registry     | Production model lifecycle, versioning, governance | Versioning, approval workflows, metrics, lineage, metadata, reproducibility |

## Summary

* Model.create is a quick, legacy method to create model packages for prototyping and rapid testing.
* Model.register combined with the SageMaker Model Registry is the recommended approach for production: it provides robust versioning, approval workflows, stored metrics, activity logs, and lineage for reproducibility.
* After a model version is approved in the registry, you can programmatically fetch the latest approved package and deploy it as a real-time endpoint.

We will also explore different hosting options—real-time endpoints and batch transforms—and when to choose each approach.

## Links and references

* SageMaker Model Registry: [https://docs.aws.amazon.com/sagemaker/latest/dg/model-registry.html](https://docs.aws.amazon.com/sagemaker/latest/dg/model-registry.html)
* SageMaker Python SDK: [https://sagemaker.readthedocs.io/](https://sagemaker.readthedocs.io/)
* Boto3 SageMaker client: [https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html)
* SageMaker Hosting (endpoints and batch transforms): [https://docs.aws.amazon.com/sagemaker/latest/dg/deploy-model.html](https://docs.aws.amazon.com/sagemaker/latest/dg/deploy-model.html)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/aws-sagemaker/module/36db8fab-85cc-40f0-8594-573631b0425b/lesson/a5157b31-a690-4f07-a0ee-c1a8cfd8f212" />
</CardGroup>
