Screenshot from the article

Introduction

AWS SageMaker is a fully managed service that allows developers and data scientists to build, train, and deploy machine learning (ML) models at scale. With built-in Jupyter notebooks, seamless model deployment, and integration with popular ML frameworks, SageMaker simplifies the entire ML workflow.

In this guide, we’ll:

✅ Set up AWS SageMaker Studio and Notebook Instances✅ Train and deploy a sample ML model using SageMaker✅ Use Amazon ECR for containerized ML models✅ Automate training & deployment with AWS Lambda and GitHub Actions

🚀 Architecture Overview

Workflow Overview:

1️⃣ SageMaker Studio provides an interactive development environment.2️⃣ SageMaker Notebook Instances allow data preprocessing and model training.3️⃣ SageMaker Training Jobs handle distributed training on scalable instances.4️⃣ SageMaker Endpoints serve trained models via a REST API.5️⃣ Amazon ECR stores custom containerized ML models.6️⃣ GitHub Actions & AWS Lambda automate model training and deployment.

1️⃣ Setting Up AWS SageMaker Studio

Step 1: Create SageMaker Studio Domain

  1. Go to the AWS ConsoleSageMakerSageMaker Studio.
  2. Click Create domain and select IAM Role (Choose an existing role or create a new one).
  3. Configure VPC, storage, and permissions as required.
  4. Click Submit and wait for the domain setup to complete.

Step 2: Launch SageMaker Studio

  1. Open SageMaker Studio from the AWS Console.
  2. Select a JupyterLab environment.
  3. Create a Notebook and choose a kernel (e.g., TensorFlow, PyTorch, Scikit-learn).
  4. Verify installation by running:
  • import tensorflow as tf print(tf.__version__)

2️⃣ Training a Model in SageMaker Notebooks

Step 1: Load Data

import boto3
import pandas as pd
from sklearn.model_selection import train_test_split
s3 = boto3.client('s3')
bucket = "your-data-bucket"
file_name = "dataset.csv"
s3.download_file(bucket, file_name, file_name)
data = pd.read_csv(file_name)
train, test = train_test_split(data, test_size=0.2)

Step 2: Train Model with SageMaker SDK

import sagemaker
from sagemaker.sklearn.estimator import SKLearn
session = sagemaker.Session()
role = "arn:aws:iam::account-id:role/SageMakerExecutionRole"
estimator = SKLearn(entry_point="train.py",
                     framework_version="0.23-1",
                     role=role,
                     instance_count=1,
                     instance_type="ml.m5.large")
estimator.fit({"train": "s3://your-data-bucket/train"})

Step 3: Deploy the Model

predictor = estimator.deploy(instance_type="ml.m5.large", initial_instance_count=1)
predictor.predict([[5.1, 3.5, 1.4, 0.2]])  # Example prediction

3️⃣ Deploying ML Models with SageMaker Endpoints

Step 1: Create SageMaker Model

from sagemaker.model import Model
model = Model(image_uri="your-ecr-image",
              model_data="s3://your-bucket/model.tar.gz",
              role=role)

Step 2: Deploy Endpoint

predictor = model.deploy(instance_type="ml.m5.large", initial_instance_count=1)

4️⃣ Automating Training & Deployment with GitHub Actions

Step 1: Define GitHub Actions Workflow

Create .github/workflows/deploy.yml:

name: Train and Deploy Model
on:
  push:
    branches:
      - main
jobs:
  train:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v2
      
      - name: Train Model
        run: |
          aws sagemaker create-training-job \
            --training-job-name my-training-job \
            --role-arn ${{ secrets.AWS_ROLE }} \
            --algorithm-specification TrainingImage="your-ecr-image" \
            --input-data-config "[{S3DataSource: {S3Uri: s3://your-data-bucket/train}}]"

🚀 Best Practices

✅ Use Amazon S3 for large dataset storage.✅ Implement IAM roles & policies for security.✅ Use CloudWatch Logs for real-time monitoring.✅ Automate workflows with GitHub Actions & Lambda Functions.✅ Deploy models with AWS SageMaker Pipelines for production workloads.

❓ FAQs

1️⃣ How do I update my trained model?

  • Retrain using the latest dataset and redeploy using predictor.update_endpoint().

2️⃣ Can I use custom ML frameworks?

  • Yes! Build a custom Docker image and push it to Amazon ECR.

3️⃣ What is the difference between SageMaker Studio & Notebook Instances?

  • Studio provides an integrated development environment, while Notebook Instances are managed Jupyter environments.

4️⃣ How do I monitor my deployed model?

  • Use Amazon CloudWatch and SageMaker Model Monitor.

🚀 Conclusion

We successfully set up AWS SageMaker, trained a model, deployed it as an API, and automated the process using GitHub Actions. Mastering SageMaker enables scalable, production-ready machine learning workflows.

💬 Are you using AWS SageMaker for your ML projects? Share your experience below! 👇

📢 Have questions or feedback? Drop a comment below or connect with me on Twitter/X@spysood!

Originally published on Medium.