September 1, 2024
Implement End-to-End MLOps with SageMaker Projects
Implementing robust machine learning pipelines remains a challenge for many organizations. Getting models from development into production…

By Christopher Adamson
9 min read
Implementing robust machine learning pipelines remains a challenge for many organizations. Getting models from development into production can be complex, error-prone, and time-consuming. MLOps seeks to solve these challenges by bringing DevOps best practices like automation, monitoring, and collaboration to machine learning systems.
In this tutorial, we will explore how to build end-to-end MLOps workflows using Amazon SageMaker and other AWS services. SageMaker provides a fully managed platform to streamline MLOps processes like model building, training, deployment, and monitoring. We will use key SageMaker features like Projects, Training Jobs, Endpoints, and Debugger to operationalize a machine learning model from start to finish.
To begin, we will set up a Git repository for storing our model code and configurations. Version control with Git enables collaboration across teams and auditing of changes. Next, we will use SageMaker training jobs to build, train, and register a scikit-learn model that predicts housing prices. The trained model will be persisted in S3 for later deployment.
We will then create endpoints in SageMaker to serve real-time predictions using the registered model. The endpoints can be scaled up to handle production workloads. To automate these workflows, we will build continuous integration and delivery pipelines with CodeBuild and CodePipeline. The pipelines allow deploying updated models automatically whenever code changes.
Finally, we will enable monitoring for our deployed models using SageMaker Debugger, Model Monitor, and CloudWatch. Detecting data drift, performance issues, and failures quickly is crucial in production systems. Together, these strategies will give us a complete MLOps platform for rapid, reliable delivery of machine learning applications.
In this tutorial, we will use SageMaker Projects to:
- Set up a Git repository with our model code
- Build, train, and register a machine learning model
- Create endpoints to deploy the model
- Set up continuous integration and delivery pipelines
- Monitor the model after deployment
Prerequisites
To follow this tutorial, you need:
- An AWS account
- AWS CLI installed and configured
- Git installed
- Basic knowledge of Git, Python, and machine learning
Set Up a Git Repository
We will use a Git repository to store all the code, configurations, and assets required for our MLOps project. Using version control brings several benefits:
Collaboration — Multiple team members can work together on the project using Git branching and merging.
Reproducibility — Git tracks all changes made to the project over time, enabling revisiting previous versions.
Auditability — Contributions and changes made by each team member are logged in the commit history.
Automation — Git triggers can be set up to run tests, builds, and deployments automatically.
Create the Git Repository
First, we need to create a new Git repository to store our project:
- On GitHub, click on the + button and select 'New repository'. Give it a name like sagemaker-mlops-demo.
- On GitLab, click on the + button, select 'New project' and create a blank project.
- On AWS CodeCommit, use the AWS CLI or console to create a new repository.
We'll use a remote GitHub repository for this tutorial.
Clone the Repository
Next, clone the remote repository to your local machine:
This creates a local copy of the repository where we can add code and commit changes.
Create Folder Structure
Organize the project by creating the following folder structure:
model/ will contain our model code
pipeline/ will contain CI/CD pipeline configs
scripts/ will contain training and other scripts
templates/ will contain SageMaker templates
This structure separates code based on functionality for easier maintainability. The Git repository is now ready for us to start adding model code and configs.
Build, Train, and Register a Model
Now we will build, train, and register a machine learning model using SageMaker training jobs.
Define the Model
First, we need to implement our model in code. In model/model.py:
- Import libraries like sklearn, pandas, numpy
- Define a class that extends sklearn.BaseEstimator
- Implement the **fit()**and predict() methods
- Add any preprocessing/feature engineering code
For example, we can implement a simple sklearn linear regression model:
Train the Model
Next, we will write a script to execute model training:
In scripts/train.py:
- Load dataset and extract features/labels
- Initialize the model class
- Train the model by calling fit()
- Evaluate model performance
- Serialize the model and save it to S3
For example:
Create a SageMaker Training Job
We can train the model on SageMaker using a TrainingJob.
In templates/template.yaml:
- Define a TrainingJob resource
- Configure the Docker container to use
- Pass the training script as an input
- Save model artifacts to S3
For example:
This will train the model on SageMaker when the template is executed.
Register the Model
To serve the model, it needs to be registered with SageMaker:
- After training, register the model by specifying the S3 path where it is persisted.
- This creates a model package that can be used for deployment.
For example:
The model is now registered and ready to be deployed!
Create Endpoints for Model Deployment
To serve predictions, we need to create and deploy endpoints hosting our model.
Define Endpoint Config
The endpoint configuration defines the model artifacts, instance type, and other configurations required for the endpoint:
In template.yaml, define an EndpointConfig:
- Specify the name of the registered model package
- Set the instance type, instance count, and other hardware configs
- Enable data capture for debugging and monitoring
For example:
Create Endpoint
Then define a Endpoint that points to this endpoint configuration:
When executed, this template will deploy the registered model as a scalable, production-ready endpoint.
Update Endpoint for New Models
To update the endpoint when we have a new model:
- Retrain the model and re-register it with a new package name
- Update the endpoint config to point to the new package
- Update the endpoint to use the new config
This enables continuous model deployment. The endpoint can now serve real-time predictions for our ML model.
Set Up CI/CD Pipelines
To automate the model development workflows, we will use AWS CodePipeline to create CI/CD pipelines.
Continuous Integration Pipeline
First, we need a pipeline for continuous integration when code is committed to the Git repository:
In pipeline/buildspec.yml:
· Define a CodeBuild buildspec with steps for:
A. Installing dependencies
B. Running linting and tests
C. Building and registering the model
D. Pushing model artifacts to S3
For example:
- In CodePipeline, create a pipeline triggered on Git commits
- Add the CodeBuild project as a stage to run buildspec.yml
This automates model training and registry on every code change.
Continuous Delivery Pipeline
Next, create a pipeline to handle continuous delivery when new model artifacts are available:
- Configure CodePipeline to detect changes in the S3 models bucket
- Add stages to deploy updated endpoint configurations and endpoints
For example:
This deploys new model versions automatically.
Add Integrations
Additional integrations can be set up:
- Send Slack notifications on pipeline events
- Run integration tests before deployment
- Add manual approval gates before production deployment
This gives complete CI/CD automation for SageMaker models!
Monitor Models Post-Deployment
We need to monitor our deployed models for drift, errors, and performance.
- Use SageMaker Model Monitor to detect data drift.
- Set up CloudWatch alarms for monitoring endpoints.
- Use SageMaker Debugger to debug models in production.
- Integrate with Prometheus and Grafana for visualizations.
This enables us to catch issues and mitigate risks.
AWS CLI Commands for Implementing MLOps Workflows with SageMaker
Here are some example AWS CLI commands for implementing MLOps workflows with SageMaker:
Create a SageMaker notebook instance
Start a training job
Register a model
Create an endpoint configuration
Create an endpoint
Update an existing endpoint
Start a pipeline execution
Get execution status
Create a CodeBuild project
Start a CodeBuild build
List SageMaker jobs
Monitor data capture
Analyze captured data
This covers common MLOps workflows like training, deployment, CI/CD pipelines, and monitoring. The AWS CLI provides a programmatic way to automate these tasks.
Example container.json
Here's an example of the container.json file used when registering a model with SageMaker:
This JSON file specifies the configurations for the Docker container that will serve the model:
ContainerHostname: The hostname of the container running the model server.
Image: The URI of the Docker image containing the model server code. This is typically an ECR image that was built from the model training container.
Mode: Indicates whether the container is hosting a single model or multiple models. Valid values are SingleModel or MultiModel.
ModelDataUrl: The S3 path where the trained model artifacts are stored. SageMaker will download these artifacts into the container during deployment.
Environment: Environment variables to pass to the Docker container. Here we specify the entry point script and model directory:
- SAGEMAKER_PROGRAM: The script that will be invoked when the container receives a prediction request.
- SAGEMAKER_SUBMIT_DIRECTORY: The directory where the model artifacts are downloaded.
This configuration allows SageMaker to set up a Docker container with the model server and artifacts for serving predictions. The CreateModel API expects the container definitions to be passed in as a JSON file using the file:// prefix.
Example variants.json
Here's an example of the variants.json file used when creating an endpoint configuration in SageMaker:
This JSON file defines the production variants for the SageMaker endpoint. Each variant specifies a model and the hardware configurations for deploying that model. You can deploy multiple variants of a model to an endpoint, each with different instance types or weights.
VariantName: A unique name for the production variant.
ModelName: The name of the SageMaker model to deploy for this variant. This must match the name of a model that you have already registered.
InitialInstanceCount: The number of instances to launch initially for this variant.
InstanceType: The EC2 instance type to use for this variant. Different instance types offer different levels of compute and memory resources.
InitialVariantWeight: Determines how much traffic is routed to this variant. Traffic is distributed across variants proportional to their weights.
You can add multiple variant definitions to the JSON array to deploy different models or configurations to the same endpoint. SageMaker will provision the instances and distribute the endpoint traffic according to the variant weights.
When creating the endpoint configuration, you pass in the variants.json file using the file:// prefix like this:
This tells SageMaker to read the variant configurations from the specified JSON file. Endpoint configs are a powerful way to deploy multi-model endpoints or A/B test different models in production.
Conclusion
In this tutorial, we went through an end-to-end MLOps workflow to operationalize a machine learning model using SageMaker and other AWS services. We started by setting up a Git repository to enable collaboration and keep track of changes during model development. For building and training the model, we used SageMaker training jobs that allowed us to scale up the compute resources as needed. The trained model was persisted in S3 and registered with SageMaker to create a model package for deployment.
To serve predictions, we created HTTPS endpoints in SageMaker that hosted our model behind a scalable backend. Updating the endpoint with a new model was also covered to continuously deliver the latest model versions. Automating these workflows was achieved using CodeBuild and CodePipeline to set up CI/CD pipelines triggered on code commits.
Lastly, we looked at how to monitor models post-deployment using SageMaker Debugger for debugging failures, Model Monitor for detecting data drift, and CloudWatch for tracking performance metrics. Together, these capabilities provide an end-to-end platform for developing, deploying, and maintaining machine learning systems reliably.
The key takeaways are that SageMaker standardizes many MLOps functions, Git enables tracking experiments and reproducing results, CI/CD pipelines automate delivery of models, and monitoring helps maintain model accuracy and performance after deployment. Using these strategies can help organizations accelerate developing and operating machine learning applications. The shortage of skilled MLOps engineers makes platforms like SageMaker even more valuable. With these best practices, productionizing machine learning workflows becomes more accessible across teams.