Cloud & AI Application Engineer: Deploying Multi-Model AWS SageMaker Endpoints with Custom ECR Containers
Provisioning a dedicated GPU or compute instance for every tenant-specific or regional machine learning model quickly bankrupts cloud budgets when 80% of those models receive sporadic traffic. By combining custom inference containers in Amazon Elastic Container Registry (ECR) with AWS SageMaker Multi-Model Endpoints (MME), dozens or hundreds of PyTorch/ONNX models share a single autoscaled fleet of instances, dynamically loading and unloading model artifacts from S3 into GPU/CPU memory on demand. As a Cloud Engineer and AI Application Engineer, I architect low-latency MME inference pipelines.
1. Implementing the SageMaker Multi-Model Server (MMS) Handler Contract
A SageMaker Multi-Model container exposes HTTP port `8080` and handles dynamic model lifecycle actions (`LOAD`, `INVOKE`, `UNLOAD`) routed by the `X-Amzn-SageMaker-Target-Model` request header. Our handler keeps hot models pinned in memory and pre-allocates tensor buffers to minimize cold-load jitter:
import io
import json
from typing import Any
import onnxruntime as ort
class MultiModelONNXHandler:
def __init__(self) -> None:
self._sessions: dict[str, ort.InferenceSession] = {}
def load_model(self, model_name: str, model_dir: str) -> None:
if model_name not in self._sessions:
opts = ort.SessionOptions()
opts.intra_op_num_threads = 2
self._sessions[model_name] = ort.InferenceSession(
f"{model_dir}/model.onnx",
sess_options=opts,
providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
)
def predict(self, model_name: str, input_tensor: Any) -> list[float]:
session = self._sessions[model_name]
input_name = session.get_inputs()[0].name
outputs = session.run(None, {input_name: input_tensor})
return outputs[0].tolist()
2. Optimizing ECR Layer Caching & S3 Tarball Model Artifacts
To keep model cold-loads from S3 under 300ms, we package quantized ONNX or TorchScript weights as uncompressed or `zstd`-streamed `.tar.gz` archives in a regional S3 bucket co-located with the SageMaker VPC endpoint, and deploy infrastructure declaratively via our Cloud Engineer AWS & GCP Terraform modules and AI Application Engineer inference workflows.
Need a Cloud Engineer or AI Application Engineer to cut your AWS SageMaker or Vertex AI inference bill with multi-model serving? Book a 1-Day Engineering Sprint.