Skip to main content

Command Palette

Search for a command to run...

AWS SageMaker to Create and Save ML Models

Updated
β€’5 min read
AWS SageMaker to Create and Save ML Models
P

πŸ‘‹ Hello! I'm passionate about DevOps and have over 1+ years of experience in the field. I'm proficient in a variety of cutting-edge technologies and always motivated to expand my knowledge and skills. Let's connect and grow together!

SKILLS:

πŸ”Ή Languages & Runtimes: Python, Shell Scripting, HCL, YAML πŸ”Ή Cloud Technologies: AWS, Microsoft Azure, GCP πŸ”Ή Infrastructure Tools: Docker, Terraform, AWS CloudFormation πŸ”Ή Other Tools: Linux, Git and GitHub Actions, Jenkins, Jira, GitLab (beginner), Docker, AWS DevOps πŸ”Ή Web Development: HTML, CSS, Bootstrap, Python, SQL

Job & Responsibilities:

πŸš€ Improved development efficiency by implementing CI/CD pipelines, resulting in a 30% reduction in deployment time on the test server. πŸ”’ Strengthened deployment and testing reliability by utilizing Docker containers and optimizing Dockerfile, reducing development issues on the test server by 20%. βš™οΈ Automated S3 bucket log creation with Shell scripting, eliminating 100% of manual search and saving 2 hours per week. πŸ“… Scheduled EC2 instance start/stop using Lambda functions and Event Bridge, leading to a 25% decrease in infrastructure costs. πŸ”§ Utilized AWS, Linux, Python, Docker, Shell scripting, Terraform, Jenkins Pipelines, and automation to streamline workflows and improve overall system performance.

I'm very detail-oriented and possess strong written and verbal communication skills. As a high performer with a possibility mindset, I strive to solve problems using efficient approaches.

Let's Connect & Grow:

If you find my profile suitable for the role you are searching for, please feel free to reach out to me at sumanprasad9766@gmail.com.


From Training a Model to Storing It in S3 for Production Use

In the previous sections, we:

  • set up SageMaker infrastructure

  • configured IAM roles and Studio

  • understood how SageMaker simplifies MLOps

Now we move to the next important step:

Creating a machine learning model and storing it in a location that SageMaker can use for deployment.


What We Are Building

We will:

  • train a simple ML model (Iris classifier)

  • save it as an artifact

  • upload it to S3

  • verify it

This is theΒ foundation step before deployment.


Why This Step Matters

In real-world MLOps systems:

  • models are not directly deployed from notebooks

  • they are stored asΒ versioned artifacts

  • deployments always pull models fromΒ central storage (S3)

This ensures:

  • reproducibility

  • traceability

  • version control


Architecture Overview

Notebook β†’ Train Model β†’ Save Artifact β†’ Upload to S3 β†’ Use for Deployment

Step 1 β€” Create an S3 Bucket

aws s3 mb s3://my-sagemaker-demo-bucket-abhishek

What this does

  • Creates a storage location in AWS

  • This bucket will store model artifacts

Think of S3 as:

The central storage for all ML models


Step 2 β€” Create IAM Execution Role

SageMaker needs permissions to:

  • read/write S3

  • run training jobs

  • deploy models


Create Trust Policy

Save asΒ trust.json

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": { "Service": "sagemaker.amazonaws.com" },
      "Action": "sts:AssumeRole"
    }
  ]
}

Create Role

aws iam create-role \
  --role-name SageMakerDemoExecutionRole \
  --assume-role-policy-document file://trust.json

Attach Permissions

aws iam attach-role-policy \
  --role-name SageMakerDemoExecutionRole \
  --policy-arn arn:aws:iam::aws:policy/AmazonSageMakerFullAccess
aws iam attach-role-policy \
  --role-name SageMakerDemoExecutionRole \
  --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess

What this does

This role allows SageMaker to:

  • access S3

  • run jobs

  • manage models


Step 3 β€” Find Default VPC and Subnets


Get Default VPC

aws ec2 describe-vpcs --filters Name=isDefault,Values=true \
  --query "Vpcs[0].VpcId" --output text

Get Subnets

aws ec2 describe-subnets --filters Name=vpc-id,Values=<VPC-ID> \
  --query "Subnets[*].SubnetId" --output text

Pick anyΒ two subnets.


What this does

  • defines network where SageMaker runs

  • ensures high availability


Step 4 β€” Create SageMaker Domain (Studio)

aws sagemaker create-domain \
  --domain-name demo-domain \
  --auth-mode IAM \
  --default-user-settings "ExecutionRole=arn:aws:iam::<ACCOUNT-ID>:role/SageMakerDemoExecutionRole" \
  --vpc-id <VPC-ID> \
  --subnet-ids "<SUBNET-1>" "<SUBNET-2>"

What this does

Creates:

  • SageMaker Studio environment

  • networking setup

  • execution permissions


Save Domain ID

You will need it later.


Step 5 β€” Create User Profile

aws sagemaker create-user-profile \
  --domain-id <DOMAIN-ID> \
  --user-profile-name demo-user

What this does

  • creates a workspace inside SageMaker

  • each user gets isolated environment


Step 6 β€” Open SageMaker Studio

Go to:

AWS Console β†’ SageMaker β†’ Domains β†’ demo-domain β†’ Launch Studio

Select:

demo-user

What you will see

  • JupyterLab interface

  • ready-to-use ML environment

  • no setup required


Step 7 β€” Train a Simple Model

Inside Studio, open a Python notebook.


Import Libraries

import boto3
import joblib
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
import os

Train Model

iris = load_iris()
X, y = iris.data, iris.target

model = DecisionTreeClassifier()
model.fit(X, y)

joblib.dump(model, "iris-model.pkl")

print("Model saved as iris-model.pkl")

What this does

  • loads sample dataset

  • trains a simple model

  • saves model as file


Step 8 β€” Upload Model to S3

import boto3

s3 = boto3.client("s3")
bucket = "my-sagemaker-demo-bucket-abhishek"

s3.upload_file("iris-model.pkl", bucket, "model-artifacts/iris-model.pkl")

print("Uploaded to S3:", f"s3://{bucket}/model-artifacts/iris-model.pkl")

What this does

  • uploads model artifact to S3

  • creates a structured path

s3://bucket/model-artifacts/iris-model.pkl

Step 9 β€” Verify Upload

aws s3 ls s3://my-sagemaker-demo-bucket-abhishek/model-artifacts/

Expected output:

iris-model.pkl

What You Achieved

You successfully:

  • trained a machine learning model

  • saved it as an artifact

  • uploaded it to S3

  • verified its availability


Why This Is Important

This step is critical because:

  • deployments always use stored artifacts

  • models must be reproducible

  • S3 acts as the source of truth


Real-World Flow

In production systems:

Training Pipeline β†’ Model Artifact β†’ S3 β†’ Model Registry β†’ Deployment β†’ Endpoint

Key Takeaways

  • Model artifacts must be stored centrally

  • S3 is the default storage for SageMaker

  • IAM roles control access

  • Studio simplifies development

  • This is the first step toward deploy


More from this blog

D

DeployToCloud

405 posts

πŸ‘‹ Welcome to my Hashnode blog! I'm a DevOps Engineer with 2+ years of experience. Join ~5k followers and explore 320+ blogs on Python, AWS, Docker, Jenkins, Linux, and more. Let's connect & grow πŸš€