Skip to main content
Xenors Xenors
Home › Data Science › Deploying ML Models on AWS SageMaker

Module 11: MLOps & Model DeploymentBy Ashok Kumar Yadav4 min read

Deploying ML Models on AWS SageMaker

SageMaker manages training machines, model hosting and scaling so you do not run servers yourself. You pay for what runs, so cleaning up matters as much as deploying.

AWS SageMaker workflow from training job to real-time inference endpoint
Quick answer: Write a training script, run it as a SageMaker training job with the SKLearn estimator, call estimator.deploy() to create a real-time endpoint, invoke it with predict(), and delete the endpoint when finished to stop charges.

SageMaker building blocks

A training job runs your script on managed instances and writes model.tar.gz to S3. A model points to that artifact and a container. An endpoint hosts the model behind an HTTPS API. Alternatives include batch transform for offline scoring, serverless inference for spiky traffic and asynchronous inference for long requests.

NeedOption
Low latency, steady trafficReal-time endpoint
Spiky or rare trafficServerless inference
Large offline scoringBatch transform

Training script

SageMaker passes data and output locations through environment variables.

Training and model_fn in one file
# train.py
import argparse, os, joblib, pandas as pd
from sklearn.ensemble import RandomForestClassifier

if __name__ == "__main__":
    p = argparse.ArgumentParser()
    p.add_argument("--n-estimators", type=int, default=100)
    a = p.parse_args()
    df = pd.read_csv(os.path.join(os.environ["SM_CHANNEL_TRAIN"], "train.csv"))
    X, y = df.drop(columns="target"), df["target"]
    model = RandomForestClassifier(n_estimators=a.n_estimators, random_state=0).fit(X, y)
    joblib.dump(model, os.path.join(os.environ["SM_MODEL_DIR"], "model.joblib"))

def model_fn(model_dir):                       # used at inference time
    return joblib.load(os.path.join(model_dir, "model.joblib"))

Train and deploy with the Python SDK

Run this from a SageMaker notebook or any machine with AWS credentials and an execution role. Check current framework versions in the AWS documentation.

Training job and real-time endpoint
import sagemaker
from sagemaker.sklearn.estimator import SKLearn

session = sagemaker.Session()
role = sagemaker.get_execution_role()          # or an IAM role ARN
train_uri = session.upload_data("train.csv", key_prefix="demo/train")

est = SKLearn(entry_point="train.py", role=role, instance_type="ml.m5.large",
              framework_version="1.2-1", hyperparameters={"n-estimators": 200})
est.fit({"train": train_uri})

predictor = est.deploy(initial_instance_count=1, instance_type="ml.m5.large")
print(predictor.predict([[5.1, 3.5, 1.4, 0.2]]))

Invoke from an application

Applications call the endpoint through the runtime API, never the training SDK.

Call the endpoint with boto3
import boto3, json

rt = boto3.client("sagemaker-runtime")
resp = rt.invoke_endpoint(EndpointName=predictor.endpoint_name,
                          ContentType="application/json",
                          Body=json.dumps([[5.1, 3.5, 1.4, 0.2]]))
print(json.loads(resp["Body"].read()))

Control cost and security

Endpoints bill while they run, so delete them when you finish a lab.

Practical tipUse least-privilege IAM roles, keep data in encrypted S3 buckets, set CloudWatch alarms on latency and errors, and add autoscaling policies for production endpoints.
Clean up to stop charges
predictor.delete_endpoint()
predictor.delete_model()

Hands-on practice

Run the examples in the downloadable Jupyter notebook. The extra checks below focus on mistakes that appear in real projects, not just syntax.

Keep inference payloads small

Practice example
import json
payload = json.dumps({'features':[0.2,1.4,3.1]})
print(len(payload.encode()), payload)
31 {"features": [0.2, 1.4, 3.1]}

Large payloads raise latency and can hit endpoint limits.

Delete test endpoints after validation

Practice example
# boto3 example
import boto3
sm=boto3.client('sagemaker')
# sm.delete_endpoint(EndpointName='demo-endpoint')
print('cleanup command prepared')
cleanup command prepared

Cloud tutorial resources should have an explicit cleanup step to prevent idle cost.

Self-check

  • Can you explain why the first practice result is trustworthy rather than just plausible?
  • What would you monitor or test before using this AWS SageMaker workflow in production?
  • Which assumption in the example would break first on a larger or messier dataset?

Frequently asked questions

Is SageMaker free?

There is a limited free tier for new accounts, but real-time endpoints and training instances are billed by usage. Check current AWS pricing.

Real-time endpoint or serverless inference?

Use real-time for steady low-latency traffic and serverless for infrequent or spiky requests where cold starts are acceptable.

How do I update a deployed model?

Create a new model version and update the endpoint configuration, ideally with a canary or blue-green rollout.

Primary references

Use these official sources when exact syntax, defaults or definitions matter beyond the examples here.

Written by Ashok Kumar Yadav
Ashok Kumar Yadav writes Xenors tutorials on applied AI, programming and data workflows. Every lesson is built around code you can run and check yourself.
GitHub · LinkedIn

Continue exploring