cd ../blog
MLOpsNovember 28, 20248 min read

Building Production MLOps with SageMaker Pipelines: A Practical Guide

A step-by-step guide to building production-ready ML pipelines with AWS SageMaker, including best practices for model deployment, monitoring, and governance.

Roger Vasconcelos
Roger Vasconcelos
AWS DevOps Architect
Building Production MLOps with SageMaker Pipelines: A Practical Guide

Photo by Kevin Ku on Unsplash

Moving machine learning models from Jupyter notebooks to production is one of the biggest challenges data science teams face. AWS SageMaker Pipelines provides a robust framework for building end-to-end ML workflows, but the documentation can be overwhelming.

In this guide, I'll share the approach I've used to help multiple companies build production MLOps platforms.

Why SageMaker Pipelines?

Before diving in, let's understand when SageMaker Pipelines makes sense:

Good fit:

  • You're already on AWS
  • You need reproducible, auditable ML workflows
  • You want managed infrastructure for training
  • Compliance requires full lineage tracking
  • Consider alternatives if:

  • You're multi-cloud
  • You need maximum flexibility (consider Kubeflow)
  • Your models are simple and don't need complex workflows
  • Core Components

    A production SageMaker Pipeline typically includes:

  • Data Processing Step - Transform raw data into features
  • Training Step - Train your model
  • Evaluation Step - Validate model performance
  • Condition Step - Gate based on metrics
  • Model Registration - Version and store approved models
  • Deployment Step - Deploy to endpoints
  • Step 1: Setting Up Your Pipeline Structure

    Here's the basic structure I recommend:

    from sagemaker.workflow.pipeline import Pipeline
    from sagemaker.workflow.steps import ProcessingStep, TrainingStep
    from sagemaker.workflow.conditions import ConditionGreaterThan
    from sagemaker.workflow.condition_step import ConditionStep
    
    # Define your pipeline
    pipeline = Pipeline(
        name="my-ml-pipeline",
        parameters=[
            processing_instance_type,
            training_instance_type,
            model_approval_status,
        ],
        steps=[
            processing_step,
            training_step,
            evaluation_step,
            condition_step,
        ],
    )
    

    Step 2: Data Processing

    Use SageMaker Processing for data preparation:

    from sagemaker.processing import ScriptProcessor
    
    processor = ScriptProcessor(
        role=role,
        image_uri=processing_image,
        instance_type="ml.m5.xlarge",
        instance_count=1,
    )
    
    processing_step = ProcessingStep(
        name="PreprocessData",
        processor=processor,
        inputs=[...],
        outputs=[...],
        code="preprocess.py",
    )
    

    Best Practice: Keep processing scripts idempotent and parameterized. This makes debugging much easier.

    Step 3: Model Training

    Configure training with appropriate instance types:

    from sagemaker.estimator import Estimator
    
    estimator = Estimator(
        image_uri=training_image,
        role=role,
        instance_count=1,
        instance_type="ml.m5.2xlarge",
        output_path=f"s3://{bucket}/models",
        hyperparameters={
            "epochs": 10,
            "learning_rate": 0.001,
        },
    )
    
    training_step = TrainingStep(
        name="TrainModel",
        estimator=estimator,
        inputs={"train": training_input},
    )
    

    Best Practice: Use Spot training for non-time-sensitive jobs—save up to 70% on training costs.

    Step 4: Model Evaluation and Gating

    Automatically evaluate models and gate deployment:

    from sagemaker.workflow.conditions import ConditionGreaterThan
    from sagemaker.workflow.condition_step import ConditionStep
    
    condition = ConditionGreaterThan(
        left=JsonGet(
            step_name="EvaluateModel",
            property_file=evaluation_report,
            json_path="metrics.accuracy.value"
        ),
        right=0.8,  # Minimum accuracy threshold
    )
    
    condition_step = ConditionStep(
        name="CheckModelAccuracy",
        conditions=[condition],
        if_steps=[register_step, deploy_step],
        else_steps=[notify_failure_step],
    )
    

    Step 5: Model Registry

    Register approved models for governance:

    from sagemaker.workflow.model_step import ModelStep
    from sagemaker.model import Model
    
    model = Model(
        image_uri=inference_image,
        model_data=training_step.properties.ModelArtifacts.S3ModelArtifacts,
        role=role,
    )
    
    register_step = ModelStep(
        name="RegisterModel",
        step_args=model.register(
            model_package_group_name="my-model-group",
            approval_status="PendingManualApproval",
        ),
    )
    

    Infrastructure as Code

    Always define your pipelines with Terraform:

    resource "aws_sagemaker_pipeline" "ml_pipeline" {
      pipeline_name         = "my-ml-pipeline"
      pipeline_display_name = "My ML Pipeline"
      role_arn             = aws_iam_role.sagemaker_role.arn
    
      pipeline_definition = jsonencode({
        // Pipeline definition
      })
    }
    

    Monitoring and Observability

    Set up comprehensive monitoring:

  • CloudWatch Metrics - Pipeline execution metrics
  • Model Monitor - Data drift detection
  • CloudTrail - Audit logging for compliance
  • SNS Notifications - Alert on failures
  • Common Pitfalls to Avoid

  • Not versioning data - Use S3 versioning or a feature store
  • Hardcoding values - Use pipeline parameters
  • Ignoring costs - Use spot training and right-size instances
  • Skipping model monitoring - Set up data drift detection from day one
  • Conclusion

    SageMaker Pipelines provides a solid foundation for production MLOps, but success requires thoughtful architecture. Start simple, automate everything, and iterate based on your team's needs.


    Building an MLOps platform? Let's discuss your requirements.

    AWSSageMakerMLOpsMachine LearningDevOps

    Share this article

    Roger Vasconcelos

    Roger Vasconcelos

    AWS DevOps Architect

    Senior AWS DevOps Architect with 10 AWS certifications and 20+ years of experience, running a boutique practice augmented by a fleet of AI agents.

    Learn more about me

    Need Help with Your Cloud Infrastructure?

    Let's discuss how I can help you implement these best practices in your organization.

    Get in Touch