
FROM MODEL TO API
A Beginner’s Guide to Deploying Machine Learning on AWS AWS Builder Community | Machine Learning | Cloud Computing Turn a locally trained machine-learning model into a cloud-hosted prediction service using Amazon SageMaker AI, Amazon S3, and Python.
Introduction
Building a machine-learning model is an exciting first step, but a model sitting on a laptop cannot automatically serve predictions to users around the world. To make it useful, we need to deploy it somewhere accessible, reliable, and scalable.
This is where cloud computing comes in.
Amazon Web Services (AWS) provides Amazon SageMaker AI, a managed service that helps developers build, train, and deploy machine-learning models. Instead of manually configuring servers and maintaining inference infrastructure, developers can create an endpoint that accepts input data and returns predictions.
In this tutorial, we will deploy a simple Scikit-learn classification model using Amazon SageMaker AI. You will learn how to prepare a model artifact, store it in Amazon S3, deploy it, and request predictions through Python.
No previous AWS deployment experience is required.
1. Understanding the Architecture
Our deployment pipeline consists of four main components:
- Scikit-learn: Trains a machine-learning model.
- Amazon S3: Stores the serialized model artifact.
- Amazon SageMaker AI: Hosts the model and manages its inference endpoint.
- Python with Boto3: Sends input data to the endpoint and receives predictions.
The workflow looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Training Data
|
v
Scikit-learn Model
|
v
model.joblib
|
v
Amazon S3
|
v
Amazon SageMaker AI
|
v
HTTPS Inference Endpoint
|
v
Prediction Returned to PythonSageMaker AI supports several deployment approaches, including real-time, serverless, asynchronous, and batch inference. We will focus on real-time inference because it is straightforward to understand and useful for interactive applications.
2. Prerequisites
Before starting, prepare the following:
- An AWS account with access to Amazon S3 and SageMaker AI.
- Python 3 and the Scikit-learn, Joblib, and Boto3 libraries.
- An AWS execution role with the permissions needed for model deployment.
- A suitable SageMaker AI inference instance quota in your chosen AWS Region.
Install the local Python dependencies:
1
pip install scikit-learn joblib boto3Configure AWS credentials using an approved method, such as an AWS CLI profile or an IAM role. Avoid placing access keys directly in source code.
3. Train and Save a Machine-Learning Model
For this demonstration, we will use the Iris dataset, a classic dataset for classifying flowers into three species based on four numerical measurements.
Create a file named
train_model.py:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
import joblib
import os
# Load the sample dataset
iris = load_iris()
X, y = iris.data, iris.target
# Train a classification model
model = RandomForestClassifier(
n_estimators=100,
random_state=42
)
model.fit(X, y)
# Save the trained model
os.makedirs("model", exist_ok=True)
joblib.dump(model, "model/model.joblib")
print("Model trained and saved successfully!")Run the script:
1
python train_model.pyThe trained model is now saved as
model/model.joblib.This file contains the learned parameters required to make predictions. In a real project, you should also preserve the training configuration, preprocessing steps, dependency versions, and evaluation metrics so that the model can be reproduced and maintained.
4. Package the Model and Upload It to Amazon S3
SageMaker AI needs access to the model artifact. We will package it into a compressed archive and upload it to an S3 bucket.
Create the archive:
1
2
3
4
5
import tarfile
with tarfile.open("model.tar.gz", "w:gz") as archive:
archive.add("model/model.joblib",
arcname="model.joblib")Next, upload it using Boto3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import boto3
bucket = "YOUR-UNIQUE-S3-BUCKET-NAME"
key = "models/iris/model.tar.gz"
s3 = boto3.client("s3")
s3.upload_file(
"model.tar.gz",
bucket,
key
)
print("Model uploaded to Amazon S3.")Replace the bucket name with your own bucket, created in your selected Region. Ensure that the identity running this script has permission to upload objects.
Amazon S3 provides durable object storage, making it a convenient location for model artifacts. Keep the bucket private and grant access only to the necessary AWS roles.
5. Create the Inference Script
The model needs instructions for loading its saved artifact and processing incoming requests.
Create a file named
inference.py:1
2
3
4
5
6
7
8
9
import os
import joblib
def model_fn(model_dir):
path = os.path.join(model_dir, "model.joblib")
return joblib.load(path)
def predict_fn(input_data, model):
return model.predict(input_data)The
model_fn function loads the trained model when the inference container starts. The predict_fn function uses it to generate predictions.SageMaker's Scikit-learn inference container provides the surrounding request-handling infrastructure. The exact framework version and input/output serialization must match the container and SDK version you select.
6. Deploy the Model with SageMaker AI
Install the SageMaker Python SDK:
1
pip install sagemakerRun the following code from a SageMaker notebook or another configured Python environment:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import sagemaker
from sagemaker.sklearn.model import SKLearnModel
role = sagemaker.get_execution_role()
model = SKLearnModel(
model_data="s3://YOUR-UNIQUE-S3-BUCKET-NAME/models/iris/model.tar.gz",
role=role,
entry_point="inference.py",
framework_version="1.2-1",
py_version="py3"
)
predictor = model.deploy(
initial_instance_count=1,
instance_type="ml.m5.large"
)
print("Endpoint:", predictor.endpoint_name)Important: This example uses the SageMaker Python SDK's Scikit-learn model interface and a specific framework version as an illustration. Check the current SDK documentation and supported framework images before running it; available versions and instance types can change.
The deployment process provisions the hosting infrastructure and registers an endpoint. Wait for deployment to finish before making predictions. Once the endpoint is ready, it can receive inference requests.
7. Request a Prediction
Our model expects four measurements in the same order used during training.
1
2
3
4
5
sample = [[5.1, 3.5, 1.4, 0.2]]
prediction = predictor.predict(sample)
print("Predicted class:", prediction)The returned value represents the predicted class index for the supplied measurements.
A web application could send similar measurements to a backend service, which would invoke the SageMaker endpoint and return the prediction to the user.
For production applications, validate the request format, handle errors and timeouts, and avoid exposing a SageMaker endpoint directly to untrusted clients. The application backend should authenticate users and invoke the endpoint through authorized AWS credentials.
8. Security and Cost Optimization
A successful deployment is not just about getting a prediction. It also means protecting data and managing infrastructure responsibly.
Security best practices
- Follow the principle of least privilege when configuring IAM roles.
- Keep S3 buckets private and enable encryption.
- Use secure credential management rather than hardcoding AWS access keys.
- Restrict endpoint invocation to authorized identities and approved application components.
- Monitor errors and unusual request patterns using AWS monitoring tools.
Cost considerations
A real-time endpoint generally incurs hosting charges while its instances are provisioned, even when request traffic is low. Training jobs, storage, data transfer, and other resources may incur additional charges.
For occasional workloads, investigate SageMaker Serverless Inference or batch inference where appropriate. For continuous traffic, select an instance size based on measured latency, throughput, and memory requirements. Always review current regional pricing before deployment. [2][4]
After testing, delete the endpoint to stop ongoing endpoint hosting charges:
1
2
3
predictor.delete_endpoint(
delete_endpoint_config=True
)Keep any S3 model artifacts you still need, or remove them separately if they are no longer required.
Conclusion
Deploying a machine-learning model is the bridge between experimentation and real-world applications. With Scikit-learn, Amazon S3, and Amazon SageMaker AI, beginners can learn the essential workflow without building their own model-serving infrastructure from scratch.
The Iris example is intentionally small, but the same core concepts apply to more meaningful projects, including fraud detection, recommendation systems, demand forecasting, and image classification.
Once the basic deployment works, the next steps are to add automated testing, model versioning, monitoring, controlled rollouts, and an application interface.
The key takeaway: a model becomes useful when people and applications can reliably access its predictions. AWS provides the infrastructure to make that possible.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article