{"id":3794,"library":"sagemaker-mlops","title":"SageMaker MLOps","description":"The `sagemaker-mlops` library provides modular, reusable components for building MLOps pipelines on Amazon SageMaker. It simplifies the orchestration of machine learning workflows, including model building, training, evaluation, and deployment. The current version is 1.7.1, and it receives regular updates in line with SageMaker SDK and AWS service evolution.","status":"active","version":"1.7.1","language":"en","source_language":"en","source_url":"https://github.com/aws/sagemaker-mlops-modules","tags":["aws","sagemaker","mlops","pipeline","workflow","machine learning"],"install":[{"cmd":"pip install sagemaker-mlops","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for interacting with Amazon SageMaker services, model training, and pipeline definitions.","package":"sagemaker","optional":false},{"reason":"AWS SDK for Python, used for underlying AWS service interactions (S3, IAM, etc.).","package":"boto3","optional":false},{"reason":"Required for integrating MLOps constructs with AWS Cloud Development Kit (CDK) for infrastructure as code.","package":"aws-cdk-lib","optional":true},{"reason":"A core dependency of AWS CDK, used when defining CDK-based MLOps solutions.","package":"constructs","optional":true}],"imports":[{"note":"Primary component for defining model training and build steps.","symbol":"ModelBuild","correct":"from sagemaker_mlops.model_build import ModelBuild"},{"note":"Used for creating MLOps pipelines with MLflow tracking integration.","symbol":"MLFlowPipeline","correct":"from sagemaker_mlops.pipelines import MLFlowPipeline"},{"note":"Helper function to retrieve the SageMaker execution role from the current environment or session.","symbol":"get_execution_role","correct":"from sagemaker_mlops.utils import get_execution_role"}],"quickstart":{"code":"import os\nimport sagemaker\nfrom sagemaker_mlops.model_build import ModelBuild\nfrom sagemaker_mlops.utils import get_execution_role\n\n# Configure AWS environment (replace with your actual values or env vars)\naws_region = os.environ.get(\"AWS_REGION\", \"us-east-1\")\naws_account_id = os.environ.get(\"AWS_ACCOUNT_ID\", \"123456789012\") # Placeholder\nsagemaker_execution_role_arn = os.environ.get(\n    \"SAGEMAKER_ROLE_ARN\", f\"arn:aws:iam::{aws_account_id}:role/service-role/AmazonSageMaker-ExecutionRole-20231201T123456\"\n) # Ensure this role has SageMaker, S3, ECR permissions\n\n# Initialize SageMaker session\ntry:\n    sagemaker_session = sagemaker.Session(\n        sagemaker_client=sagemaker.boto_session.client(\"sagemaker\", region_name=aws_region),\n        default_bucket=f\"sagemaker-mlops-quickstart-{aws_account_id}-{aws_region}\" # Unique bucket name\n    )\nexcept Exception as e:\n    print(f\"Warning: Could not create SageMaker session directly, possibly due to missing credentials. Error: {e}\")\n    # Fallback for demonstration if not in an AWS environment\n    class MockSageMakerSession:\n        def default_bucket(self): return \"mock-sagemaker-bucket\"\n        def default_bucket_prefix(self): return \"mock-prefix\"\n        def upload_data(self, *args, **kwargs): pass\n    sagemaker_session = MockSageMakerSession()\n\n\n# Get SageMaker execution role (prioritize env var or default for local testing)\ntry:\n    # This function works best within a SageMaker Notebook or Studio environment\n    role = get_execution_role(sagemaker_session)\nexcept ValueError:\n    print(\"Could not retrieve SageMaker execution role from session. Using provided ARN.\")\n    role = sagemaker_execution_role_arn\n    if \"123456789012\" in role:\n        print(\"WARNING: Using placeholder SageMaker execution role ARN. Please update 'SAGEMAKER_ROLE_ARN' env var.\")\n\n# Define an example ModelBuild component for a SageMaker Pipeline\n# This component encapsulates a SageMaker Estimator configuration\nmodel_build = ModelBuild(\n    sagemaker_session=sagemaker_session,\n    role=role,\n    base_job_name=\"my-training-job\",\n    instance_type=\"ml.m5.xlarge\",\n    instance_count=1,\n    image_uri=sagemaker.image_uris.get_training_image(aws_region, \"pytorch\", \"1.13.1\", py_version=\"py39\"),\n    hyperparameters={\n        \"epochs\": 10,\n        \"batch_size\": 32,\n    },\n    input_data_config=[\n        sagemaker.TrainingInput(\n            s3_data=f\"s3://{sagemaker_session.default_bucket()}/data/train/\",\n            content_type=\"text/csv\",\n            s3_data_type=\"S3Prefix\"\n        )\n    ],\n    output_data_config={\n        \"s3_output_location\": f\"s3://{sagemaker_session.default_bucket()}/output/\"\n    },\n    metrics_definitions=[\n        {\"Name\": \"train:loss\", \"Regex\": \".*loss=([0-9\\\\.]+).*\"},\n    ]\n)\n\nprint(f\"Successfully instantiated ModelBuild component:\")\nprint(f\"- Role: {model_build.role}\")\nprint(f\"- Instance Type: {model_build.instance_type}\")\nprint(f\"- Image URI: {model_build.image_uri}\")\nprint(f\"- Hyperparameters: {model_build.hyperparameters}\")\nprint(\"\\nThis 'model_build' object can now be used as a step within a SageMaker Pipeline.\")","lang":"python","description":"This quickstart demonstrates how to instantiate a `ModelBuild` component. This component defines the configuration for a SageMaker training job, which is typically used as a step within a larger SageMaker Pipeline. It initializes a SageMaker session and retrieves an execution role, showing how to configure it for local testing or within an AWS SageMaker environment. Placeholder values are used for AWS account ID and role ARN, which must be replaced with actual, valid credentials for execution."},"warnings":[{"fix":"Ensure the IAM role ARN provided to `ModelBuild` or `Pipeline` has the necessary `AmazonSageMakerFullAccess` (or more granular custom policies), S3 read/write on relevant buckets, and ECR access. Validate policies using the IAM Policy Simulator.","message":"Insufficient AWS IAM permissions are a common source of errors. The SageMaker execution role used by MLOps pipelines needs permissions for SageMaker, S3 (read/write to specified buckets), ECR (pulling images), and potentially other services like KMS, CloudWatch, or Step Functions depending on the pipeline complexity.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always install `sagemaker-mlops` in a clean environment or ensure your `sagemaker` SDK version meets the minimum requirement specified in `sagemaker-mlops`'s `install_requires`. Upgrade `sagemaker` to the latest compatible version: `pip install --upgrade sagemaker`.","message":"Strict dependency on specific `sagemaker` SDK versions. `sagemaker-mlops` typically depends on a recent `sagemaker` SDK version (e.g., `>=2.176.0`). Installing an older or incompatible version of `sagemaker` can lead to runtime errors or unexpected behavior due to API changes.","severity":"breaking","affected_versions":"All versions, specifically when upgrading `sagemaker` SDK."},{"fix":"Ensure your `sagemaker.Session` is initialized with the correct region, and all S3 URIs reference buckets in that same region. Use account-specific prefixes for S3 buckets to aid global uniqueness, e.g., `sagemaker-youraccountid-yourregion-data`.","message":"AWS region and S3 bucket consistency is crucial. All SageMaker resources (pipelines, training jobs, models) and S3 buckets used for input/output data or artifacts must reside in the same AWS region. S3 bucket names also need to be globally unique.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If running locally, explicitly pass the full SageMaker execution role ARN string (e.g., `arn:aws:iam::123456789012:role/SageMakerExecutionRole`) to the `role` parameter of `ModelBuild` or `Pipeline` components. Ensure your local AWS credentials are configured (e.g., via `~/.aws/credentials` or environment variables) for `boto3` to initialize the session correctly.","message":"The `get_execution_role()` utility function is designed to work within a SageMaker execution environment (e.g., SageMaker Notebook Instances or Studio). When running scripts locally or outside SageMaker, it may fail, requiring explicit role ARN provision.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}