Skill

Deploy SageMaker Endpoints with Confidence

A SageMaker endpoint deployment skill covering model registration, blue/green rollout, auto-scaling, and inference optimization patterns.

Works with boto3sagemakergithubcloudwatch

Maintainer of this project? Claim this page to edit the listing.


91
Spark score
out of 100
Updated 7 months ago
Version 1.0.0
Models

Add to Favorites

Why it matters

Automate the deployment of machine learning models to Amazon SageMaker endpoints, ensuring robust, scalable, and optimized inference infrastructure.

Outcomes

What it gets done

01

Register and version models in SageMaker Model Registry.

02

Configure endpoints for production with data capture.

03

Implement Blue/Green and Multi-Model deployment strategies.

04

Set up auto-scaling and monitor endpoint performance.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/vb-sagemaker-endpoint-deploy | bash

Overview

SageMaker Endpoint Deployment Expert

A SageMaker endpoint deployment skill covering model registration, blue/green and multi-model deployment patterns, auto-scaling, and custom inference handlers. It also covers cold-start, memory, timeout, and scaling troubleshooting. Use it when deploying or scaling a production SageMaker endpoint, or diagnosing latency, memory, timeout, or auto-scaling problems on one.

What it does

This skill covers the full lifecycle of Amazon SageMaker endpoint deployment: model registration and versioning through the SageMaker Model Registry before deployment, endpoint configuration strategy using separate, timestamped endpoint configs per deployment stage with data capture enabled, and two deployment patterns - blue/green deployment with gradual traffic-weight shifting between variants, and multi-model endpoints that share infrastructure across multiple models registered under one endpoint.

It covers auto-scaling configuration (registering a scalable target and a target-tracking scaling policy based on invocations-per-instance, with configurable scale-out and scale-in cooldowns), inference optimization through custom inference handlers (a model_fn/input_fn/predict_fn/output_fn class structure for loading a model, parsing JSON input, running inference, and formatting the response), and monitoring through CloudWatch metric alarms (for example alerting when model latency exceeds a threshold).

Best-practice guidance spans instance selection (compute-optimized C5/M5 for CPU inference, P3/G4 for GPU workloads), using batch transform instead of real-time endpoints for large-scale batch inference, keeping model artifacts under 10GB for faster cold starts, securing endpoints with VPC endpoints, encryption at rest and in transit, and least-privilege IAM, and cost optimization via Serverless Inference for sporadic traffic versus Reserved Instances for predictable workloads. It also covers troubleshooting: cold-start latency (model warming, provisioned concurrency), memory issues (CloudWatch metrics, instance sizing, quantization), timeout errors (raising SAGEMAKER_MODEL_SERVER_TIMEOUT), and scaling issues (reviewing auto-scaling policies and CloudWatch thresholds).

import boto3
from sagemaker import Model, get_execution_role
from sagemaker.model_package import ModelPackage

When to use - and when NOT to

Use this skill when you need to move a registered model into production on SageMaker and keep it there reliably - picking between a gradual blue/green traffic shift or a shared-infrastructure multi-model endpoint, sizing an auto-scaling policy around invocations-per-instance, or working through a specific failure mode like cold starts, out-of-memory errors, request timeouts, or scaling lag.

It is not a fit for large-scale batch scoring, where the skill itself recommends batch transform over a real-time endpoint, or for model training and artifact packaging - the guidance assumes a model package is already registered and starts from there.

Inputs and outputs

Inputs are a registered SageMaker model package, target instance type and count, deployment-pattern choice (blue/green or multi-model), and any custom inference environment variables such as SAGEMAKER_MODEL_SERVER_WORKERS. Outputs are working boto3/SageMaker SDK code for endpoint creation with data capture enabled (a configurable sampling percentage and a destination S3 URI for captured input and output payloads), traffic-shifting logic between variants, an auto-scaling policy, a custom inference handler module, and CloudWatch alarm definitions for latency monitoring.

Integrations

The SageMaker Model Registry supplies the versioned model package deployment starts from; AWS Application Auto Scaling watches the SageMakerVariantInvocationsPerInstance metric to drive scale-out and scale-in; CloudWatch alarms watch ModelLatency and other metrics keyed to the endpoint name and variant; IAM execution roles gate what the endpoint can access; and VPC endpoints keep inference traffic off the public internet.

Who it's for

ML engineers and platform teams deploying models to production SageMaker endpoints who need concrete patterns for zero-downtime rollouts and cost-efficient multi-model hosting, and who test with production-like data volumes and request patterns before going live, rather than assembling endpoint configuration, scaling policies, and inference handlers from the SageMaker documentation alone.

Source README

SageMaker Endpoint Deployment Expert

You are an expert in Amazon SageMaker endpoint deployment, with deep knowledge of model hosting, inference optimization, auto-scaling, and production ML deployment patterns. You understand the complete lifecycle from model registration to endpoint monitoring and troubleshooting.

Core Deployment Principles

Model Registration and Versioning

Always register models in the SageMaker Model Registry before deployment:

import boto3
from sagemaker import Model, get_execution_role
from sagemaker.model_package import ModelPackage

### Register model package
model_package = ModelPackage(
    role=get_execution_role(),
    model_package_arn="arn:aws:sagemaker:region:account:model-package/package-name/version",
    sagemaker_session=sagemaker_session
)

### Create model from package
model = model_package.create_model(
    instance_type="ml.m5.xlarge",
    role=get_execution_role(),
    env={
        "SAGEMAKER_MODEL_SERVER_TIMEOUT": "3600",
        "SAGEMAKER_MODEL_SERVER_WORKERS": "2"
    }
)

Endpoint Configuration Strategy

Use separate endpoint configurations for different deployment stages:

from sagemaker.predictor import Predictor
from datetime import datetime

### Production-ready endpoint configuration
endpoint_config_name = f"model-config-{datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}"

model.create_endpoint_config(
    name=endpoint_config_name,
    initial_instance_count=2,
    instance_type="ml.m5.xlarge",
    data_capture_config={
        "EnableCapture": True,
        "InitialSamplingPercentage": 100,
        "DestinationS3Uri": "s3://bucket/data-capture",
        "CaptureOptions": [
            {"CaptureMode": "Input"},
            {"CaptureMode": "Output"}
        ]
    }
)

Deployment Patterns

Blue/Green Deployment

Implement zero-downtime deployments using traffic shifting:

def blue_green_deployment(endpoint_name, new_model, current_weight=100):
    predictor = Predictor(endpoint_name=endpoint_name)
    
    # Create new endpoint configuration with both variants
    new_config = predictor.update_endpoint(
        model_name=new_model.name,
        initial_instance_count=1,
        instance_type="ml.m5.xlarge",
        variant_name="green-variant",
        wait=False
    )
    
    # Gradually shift traffic
    traffic_configs = [
        {"VariantName": "blue-variant", "DesiredWeight": 90},
        {"VariantName": "green-variant", "DesiredWeight": 10}
    ]
    
    predictor.update_endpoint_weights_and_capacities(
        desired_weights_and_capacity=traffic_configs,
        wait=True
    )
    
    return predictor

Multi-Model Endpoints

Optimize costs for multiple models with shared infrastructure:

from sagemaker.multidatamodel import MultiDataModel

### Setup multi-model endpoint
multi_model = MultiDataModel(
    name="multi-model-endpoint",
    model_data_prefix="s3://bucket/multi-model-artifacts/",
    model=model,
    sagemaker_session=sagemaker_session
)

predictor = multi_model.deploy(
    initial_instance_count=1,
    instance_type="ml.m5.xlarge",
    endpoint_name="multi-model-endpoint"
)

### Add models dynamically
multi_model.add_model(model_data_source="s3://bucket/model1.tar.gz", model_data_path="model1")
multi_model.add_model(model_data_source="s3://bucket/model2.tar.gz", model_data_path="model2")

Auto-Scaling Configuration

Instance-Based Auto-Scaling

Configure responsive scaling based on invocation metrics:

import boto3

autoscaling_client = boto3.client('application-autoscaling')
cloudwatch = boto3.client('cloudwatch')

### Register scalable target
autoscaling_client.register_scalable_target(
    ServiceNamespace='sagemaker',
    ResourceId=f'endpoint/{endpoint_name}/variant/AllTraffic',
    ScalableDimension='sagemaker:variant:DesiredInstanceCount',
    MinCapacity=1,
    MaxCapacity=10
)

### Create scaling policy
autoscaling_client.put_scaling_policy(
    PolicyName=f'{endpoint_name}-scaling-policy',
    ServiceNamespace='sagemaker',
    ResourceId=f'endpoint/{endpoint_name}/variant/AllTraffic',
    ScalableDimension='sagemaker:variant:DesiredInstanceCount',
    PolicyType='TargetTrackingScaling',
    TargetTrackingScalingPolicyConfiguration={
        'TargetValue': 70.0,
        'PredefinedMetricSpecification': {
            'PredefinedMetricType': 'SageMakerVariantInvocationsPerInstance'
        },
        'ScaleOutCooldown': 300,
        'ScaleInCooldown': 300
    }
)

Inference Optimization

Custom Inference Code

Optimize inference performance with custom handlers:

### inference.py
import json
import torch
import numpy as np
from transformers import AutoTokenizer, AutoModel

class ModelHandler:
    def __init__(self):
        self.model = None
        self.tokenizer = None
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    
    def model_fn(self, model_dir):
        """Load model for inference"""
        self.tokenizer = AutoTokenizer.from_pretrained(model_dir)
        self.model = AutoModel.from_pretrained(model_dir).to(self.device)
        self.model.eval()
        return self.model
    
    def input_fn(self, request_body, content_type):
        """Parse input data"""
        if content_type == 'application/json':
            data = json.loads(request_body)
            return data['instances']
        raise ValueError(f"Unsupported content type: {content_type}")
    
    def predict_fn(self, input_data, model):
        """Run inference"""
        with torch.no_grad():
            inputs = self.tokenizer(input_data, return_tensors="pt", 
                                  padding=True, truncation=True, max_length=512)
            inputs = {k: v.to(self.device) for k, v in inputs.items()}
            
            outputs = model(**inputs)
            predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
            return predictions.cpu().numpy().tolist()
    
    def output_fn(self, prediction, accept):
        """Format output"""
        if accept == 'application/json':
            return json.dumps({'predictions': prediction})
        raise ValueError(f"Unsupported accept type: {accept}")

Monitoring and Observability

CloudWatch Metrics and Alarms

Set up comprehensive monitoring:

### Create custom metrics and alarms
cloudwatch.put_metric_alarm(
    AlarmName=f'{endpoint_name}-high-latency',
    ComparisonOperator='GreaterThanThreshold',
    EvaluationPeriods=2,
    MetricName='ModelLatency',
    Namespace='AWS/SageMaker',
    Period=300,
    Statistic='Average',
    Threshold=5000.0,  # 5 seconds
    ActionsEnabled=True,
    AlarmActions=[
        'arn:aws:sns:region:account:alert-topic'
    ],
    AlarmDescription='Alert when model latency exceeds 5 seconds',
    Dimensions=[
        {
            'Name': 'EndpointName',
            'Value': endpoint_name
        },
        {
            'Name': 'VariantName',
            'Value': 'AllTraffic'
        }
    ]
)

Best Practices

  1. Instance Selection: Use compute-optimized instances (C5/M5) for CPU inference, P3/G4 for GPU workloads
  2. Batch Transform: Use batch transform for large-scale batch inference instead of real-time endpoints
  3. Model Artifacts: Keep model artifacts under 10GB for faster cold starts; use model compilation for edge deployment
  4. Security: Always use VPC endpoints, enable encryption at rest and in transit, implement proper IAM policies
  5. Cost Optimization: Use Serverless Inference for sporadic traffic, Reserved Instances for predictable workloads
  6. Testing: Always test with production-like data volumes and request patterns before deployment

Troubleshooting Common Issues

  • Cold Start Latency: Implement model warming strategies and use provisioned concurrency
  • Memory Issues: Monitor CloudWatch metrics and adjust instance types or implement model quantization
  • Timeout Errors: Increase SAGEMAKER_MODEL_SERVER_TIMEOUT environment variable
  • Scaling Issues: Review auto-scaling policies and CloudWatch metrics for proper threshold configuration

FAQ

Common questions

Discussion

Questions & comments ยท 0

Sign In Sign in to leave a comment.