CloudBrum
CloudBrum · Cloud decision-support platform
AWS AI and machine learning

Amazon SageMakerLast reviewedReading time~12 minLevelBeginner to advanced

Amazon Web Services · AI and machine learning · Model training and hosting

A workshop for machine learning, billed by the hour. You prepare data, train a model on rented servers, register it and put it behind an endpoint, all inside one service. The part to learn first is the bill: training jobs stop charging when they finish, but a real-time endpoint and a forgotten notebook keep charging until you stop them. AWS now calls the machine learning service SageMaker AI, and uses SageMaker for the wider data-and-AI platform around it.

On this page (17 sections)

    Mind map: Amazon SageMaker at a glance

    Amazon SageMaker mind map Amazon SageMaker at a glance: Build: Studio and JupyterLab, Canvas (no code), Unified Studio; Train: Training jobs, Managed spot training, HyperPod clusters; Tune & track: Automatic tuning, Experiments (MLflow), Checkpoints in S3; Serve: Real-time endpoints, Serverless, async, batch, Inference components; MLOps: Pipelines, Model Registry, Deployment guardrails; Models: JumpStart catalogue, Your own container, Fine-tune foundation models; Govern: Clarify (bias, explain), Model Monitor (drift), Model Cards; Pricing: Instance time, Endpoints bill while up, Savings Plans. Amazon SageMaker at a glance Building, training and serving models on AWS: the main pieces and decisions CloudBrum Cloud decision-support platform CloudBrum Studio and JupyterLab Canvas (no code) Unified Studio Build Training jobs Managed spot training HyperPod clusters Train Automatic tuning Experiments (MLflow) Checkpoints in S3 Tune & track Real-time endpoints Serverless, async, batch Inference components Serve Pipelines Model Registry Deployment guardrails MLOps JumpStart catalogue Your own container Fine-tune foundation models Models Clarify (bias, explain) Model Monitor (drift) Model Cards Govern Instance time Endpoints bill while up Savings Plans Pricing SageMaker17-point analysis CloudBrum· 17-point cloud service analysis cloudbrum.com
    CloudBrum SageMaker at a glance · 17-point analysis Build Studio and JupyterLab Canvas (no code) Unified Studio Train Training jobs Managed spot training HyperPod clusters Tune & track Automatic tuning Experiments (MLflow) Checkpoints in S3 Serve Real-time endpoints Serverless, async, batch Inference components MLOps Pipelines Model Registry Deployment guardrails Models JumpStart catalogue Your own container Fine-tune foundation models Govern Clarify (bias, explain) Model Monitor (drift) Model Cards Pricing Instance time Endpoints bill while up Savings Plans CloudBrum · cloudbrum.com

    Free to share. The downloaded image includes the CloudBrum link.

    Key features (summary)

    • Notebooks and a studioSageMaker Studio gives you JupyterLab, a code editor and a place to see jobs, experiments and endpoints in one window. The older notebook instances still work, but they bill while they run and have no idle shutdown of their own.
    • Training on servers you don’t manageYou point a job at data in S3, pick an instance type and a container, and SageMaker starts the machines, runs the code, saves the model to S3 and shuts everything down. A single job can run for up to 28 days.
    • Built-in algorithms and prebuilt containersXGBoost, linear learners and others come ready to use, and there are maintained containers for PyTorch, TensorFlow, Hugging Face and scikit-learn. You can also bring any container that follows the training and serving contract.
    • Automatic model tuningGive it a range for each hyperparameter and a metric to improve, and it runs many training jobs and learns which settings work best. It costs a bunch of jobs, so set a limit.
    • Four ways to serve a modelReal-time endpoints for steady traffic, serverless endpoints for gaps in traffic, asynchronous endpoints for big payloads and long runs, and batch transform for scoring a whole file at once.
    • Pipelines and a model registryChain data prep, training, evaluation and approval into a repeatable pipeline. The registry keeps model versions with their metrics and an approval status that a deployment can wait for.
    • JumpStartA catalogue of pretrained models and sample solutions, including open foundation models. You can deploy one in a few clicks or fine-tune it on your own data first.
    • Watching models in productionModel Monitor compares live inputs and predictions with a baseline to catch drift, and Clarify reports bias and which features drove a prediction.

    Basic concepts

    Training job
    One run of your training code on managed instances. It reads from S3, writes a model artifact back to S3 and stops billing when it ends.
    Model artifact
    The output of training, usually a model.tar.gz file in S3. SageMaker doesn’t keep the model anywhere else.
    Model (SageMaker object)
    A record that links a container image to a model artifact and an execution role. Creating it costs nothing, and an endpoint needs one.
    Endpoint configuration
    The recipe for an endpoint: which models, which instance type, how many instances and what share of traffic goes where.
    Endpoint
    The running, billed HTTPS service created from an endpoint configuration. You call it with the runtime API and a signed request.
    Execution role
    The IAM role SageMaker assumes to read your data and pull your container. Its permissions decide what your training code can touch.
    Instance type
    A name such as ml.m5.xlarge or ml.g5.xlarge. The ml. prefix marks SageMaker instances, and GPU types usually start with a quota of zero.
    Managed spot training
    Training on spare capacity at a discount. AWS can take the machine back, so the job has to write checkpoints and resume.
    Inference component
    A way to put several models on one endpoint and give each its own share of CPU, memory and accelerators, with its own scaling.
    Multi-model endpoint
    One fleet of instances that loads many small models from S3 on demand, so you don’t pay for an endpoint per model.
    Pipeline
    A directed graph of steps (process, train, evaluate, register) that you define in code and run on demand or on a schedule.
    Model Registry
    A catalogue of model versions in model groups, each with metrics, lineage and an approval status.
    Feature Store
    A managed store for features, with an online side for low-latency lookups and an offline side in S3 for training.
    HyperPod
    A cluster of accelerated instances for very large training runs, kept healthy and resumable by SageMaker.

    How to create & configure it

    Train and serve your first model from the AWS console

    1. Pick the Region and check quotasChoose a Region close to your data and users (Mumbai or Hyderabad for India). Open Service Quotas and look at the instance types you plan to use. GPU types often start at zero and a request can take a day or two.
    2. Create a bucket and an execution rolePut your training data in an S3 bucket in the same Region. Create an IAM role that SageMaker can assume, and give it access to that bucket only. Don’t attach the broad full-access policy except in a sandbox.
    3. Open StudioCreate a domain (the quick setup is fine for a first look), then open JupyterLab. Set an idle timeout on the space so it shuts itself down. A notebook left running over a weekend is the classic surprise bill.
    4. Prepare the dataClean the file in a notebook and write a training set and a validation set back to S3. Keep the column order the algorithm expects, and keep the label in the first column for the built-in XGBoost.
    5. Run a training jobUnder Training jobs choose a built-in algorithm or your own container, point the input channel at S3, and pick a small instance such as ml.m5.xlarge. Turn on managed spot training and set a checkpoint location.
    6. Check the resultWhen the job completes, read its metrics in the console or in CloudWatch. The model artifact lands in the output path you gave. If the job failed, the failure reason and the CloudWatch log stream tell you which.
    7. Register the modelAdd the model to a Model Registry group with its metrics, and set the status to pending approval. Even for a first project this habit makes later rollbacks and audits easy.
    8. Deploy to an endpointCreate a real-time endpoint on one small instance, or a serverless endpoint if traffic will be occasional. Wait for InService, then send a test request.
    9. Add scaling, alarms and a stop ruleAttach target tracking on invocations per instance, set a CloudWatch alarm on 5xx errors and latency, and delete test endpoints the same day. Set a budget alert for SageMaker in Billing.

    Tip: once the console flow makes sense, write the same steps as code (the SDK, CloudFormation, the CDK or Terraform, see the code references) so every environment is built the same way and a change shows up in review.

    Common issues after configuration

    SymptomLikely causeWhat to check / fix
    ResourceLimitExceeded when you start a job or endpointThe account’s quota for that instance type is zero or too lowOpen Service Quotas, find the line for that exact use (training job, endpoint or notebook) and instance type, and request an increase. A different instance type in the same family is often available straight away.
    AccessDeniedException mentioning iam:PassRoleThe person or pipeline starting the job can’t pass the execution role to SageMakerAllow iam:PassRole for that one role, with a condition that the service is sagemaker.amazonaws.com, and check the role’s trust policy lists SageMaker.
    Training job fails right after it startsThe container can’t read the data, can’t pull the image or hits a bug in the scriptRead the failure reason, then the log stream in CloudWatch. For S3 problems check the role and, in a VPC, that there is an S3 gateway endpoint. For image problems check the ECR permissions.
    Endpoint stuck in Creating, then FailedThe container didn’t pass the health check on /ping, or couldn’t load the model in timeLook at the endpoint’s CloudWatch logs. The container must listen on port 8080 and answer /ping and /invocations. For big models raise the startup timeout and use a larger instance.
    ModelError or a 5xx when you call the endpointThe model code threw an exception, or the input wasn’t in the format the container expectsTest the same payload locally against the container, check the content type, and read the log line printed just before the error.
    A 413 error or a timeout on the callThe payload or the processing time is over the limit for that endpoint typeReal-time endpoints accept payloads up to 25 MB and answer within 60 seconds. Use an asynchronous endpoint for bigger inputs or slower models.
    The bill is high but nothing seems to be runningAn endpoint, a notebook or a Studio app is still upList endpoints and apps in every Region you’ve used. Delete what you don’t need, add idle shutdown to Studio spaces, and use Cost Explorer filtered to SageMaker usage types.
    Spot training job keeps restartingInterruptions with no checkpoint, so it starts over each timeWrite checkpoints to the S3 location in the job config and load the latest one at start-up. Set the maximum wait time higher than the runtime.
    ThrottlingException on the runtime APIMore requests than the endpoint can serve right nowScale out, add retries with backoff on the client, and check that auto scaling is attached and its target value is realistic.
    Model works in the notebook but gives different results on the endpointA different library version or preprocessing stepPin the framework version in the container, ship the preprocessing code with the model, and compare a sample of outputs before cutting over.
    Pipeline step can’t find the output of the previous stepA wrong S3 path or step property referencePass outputs through step properties rather than hard-coded paths, and run the failing step alone with the same inputs.

    Use cases: when and where to use it

    Good fit

    • Teams that train models regularly and want the servers started and stopped for them
    • Putting a trained model behind an API without building the serving stack
    • Experiments you want tracked, compared and repeatable, instead of notebooks on someone’s laptop
    • Scheduled retraining with an approval step before anything reaches production
    • Fine-tuning and hosting an open foundation model inside your own AWS account
    • Scoring a large file overnight with batch transform and no always-on servers

    Consider something else

    • Calling a hosted foundation model through one API with no servers of your own → Amazon Bedrock
    • Ready-made vision, text or speech APIs when you don’t need a custom model → Amazon Rekognition, Comprehend or Transcribe
    • Analytics and SQL on the data before it becomes features → Athena or Redshift, then feed SageMaker
    • A tiny model that fits in a function → package it in AWS Lambda and skip the endpoint
    • The same job on other clouds → compare Azure Machine Learning and Gemini Enterprise Agent Platform (Vertex AI)

    Pros: why consider it?

    Why teams choose it

    • Covers the whole path from notebook to endpoint without gluing separate tools together
    • You pay for compute only while jobs and endpoints run, and training machines go away by themselves
    • Spot training, multi-model endpoints and scale-to-zero give real levers on cost
    • Deep AWS integration: S3, IAM, VPC, KMS, CloudWatch and EventBridge work the way you already know
    • Choice of serving styles, from always-on to serverless to batch
    • Works with open source (PyTorch, XGBoost, MLflow) rather than locking you to a proprietary format

    Trade-offs to weigh

    • It’s a big surface area, and the names changed (SageMaker AI, Unified Studio, the older Studio Classic), so old tutorials mislead
    • Real-time endpoints bill around the clock, even with no traffic
    • New accounts hit instance quotas, especially for GPUs
    • Debugging a failing container means reading CloudWatch logs, and some failures are vague
    • The Python SDK changed a lot in version 3, so v2 examples can break
    • Cost gets hard to follow when many people share one account and don’t tag their jobs

    Common threats, vulnerabilities & risks

    RiskSeverityWhy it mattersMitigation
    An idle endpoint or notebook running for weeksHighEndpoints on GPU instances cost real money every hour and nothing warns you.Budgets and anomaly alerts for SageMaker, idle shutdown on Studio, and a rule that test endpoints are deleted the same day.
    An over-broad execution roleHighTraining code runs with that role. A full-access role plus a compromised or careless script can read every bucket in the account.One role per project, scoped to the buckets and keys it needs, with a permissions boundary.
    Training data and models left unencrypted or publicHighThe data is often the most sensitive thing in the account, and model artifacts can leak it.KMS keys on S3, volumes and endpoints, Block Public Access on, and private buckets only.
    Notebooks with direct internet accessMediumA notebook with open egress can pull in unvetted packages or send data out.Run Studio in a VPC with no direct internet, and use a private package mirror or VPC endpoints.
    Model drift nobody noticesMediumAccuracy decays as the world changes, and the endpoint still answers with confidence.Capture inputs and outputs, schedule Model Monitor, and alert on drift.
    Unreviewed model goes to productionMediumA notebook-trained model gets deployed because it’s quicker than the process.Use the Model Registry with an approval status and let only the pipeline role deploy.
    Biased or unexplainable predictionsMediumNobody sets out to build an unfair model, but it happens when the training data is lopsided.Run Clarify before release and on a schedule, and record the findings in a model card.
    Endpoint exposed to too many callersLow–MedInvocation permission is just IAM, so a broad policy lets anyone in the account call it.Allow sagemaker:InvokeEndpoint on named endpoints only, and put an API in front for outside callers.
    Supply chain risk in containers and packagesLow–MedCustom images and pip installs bring in whatever the registry serves that day.Build images in a pipeline from pinned versions, scan them in ECR, and avoid latest tags.

    Performance optimisation

    • Pick the instance type from measurements. Inference Recommender can load-test a model across candidates, and a cheaper CPU instance is often enough for tree models.
    • Keep training data in the same Region and use Pipe mode or FastFile mode for large datasets so training starts before the whole download finishes.
    • Use warm pools when you run many short jobs back to back, so the instance and its container survive between runs.
    • Spread large training runs across instances with the distributed training libraries, and use HyperPod when a run is big and long enough that hardware failures are a given.
    • Cut cold start on serverless endpoints with Provisioned Concurrency, or keep a real-time endpoint warm when latency matters.
    • Batch requests where the model allows it. One call with 32 rows is usually far cheaper than 32 calls.
    • Compile or quantise big models, and try Inferentia (ml.inf2) or Trainium instances where your framework supports them.
    • Put a multi-model endpoint or inference components in front of many small models instead of one endpoint each.

    Reduce monthly cost

    FinOps is the habit of managing cloud spend together with engineering. These are the FinOps checks that matter most for Amazon SageMaker.

    • Use managed spot training with checkpoints for jobs that can restart. AWS quotes savings of up to 90% against on-demand.
    • Delete test endpoints and stop idle apps. This is the single biggest saving for most teams.
    • Use serverless or asynchronous (scale to zero) endpoints for traffic that comes in bursts, and batch transform for jobs you can run on a schedule.
    • Host many small models on a multi-model endpoint or with inference components rather than one instance per model.
    • Right-size instances. Check CPU, memory and GPU use in CloudWatch, and move to a smaller type when the graphs are flat.
    • Add target-tracking auto scaling to every real-time endpoint, and set the minimum no higher than you need overnight.
    • Look at SageMaker AI Savings Plans for steady usage once you know your baseline.
    • Cap automatic tuning with a maximum number of jobs and early stopping, so a sweep can’t run away.
    • Keep data compact (Parquet, compressed CSV) and delete old training outputs, checkpoints and model versions you won’t use again.
    • Tag every job, endpoint and domain with team and project, and give each a budget alert.

    The figures and free-tier limits on this page are from AWS's public pages as we read them. Region and instance type move them, so confirm the live numbers before you plan around them (see Pricing).

    Security optimisation

    • Give each project its own execution role, scoped to the buckets, keys and ECR repositories it needs, and limit who can pass it with iam:PassRole.
    • Run Studio, training jobs and endpoints inside a VPC, with private subnets, VPC endpoints for S3, ECR and CloudWatch, and no direct internet where you can avoid it.
    • Encrypt with KMS keys you control: S3 data, training volumes, model artifacts and endpoint storage volumes.
    • Turn on network isolation for training and inference containers that don’t need to call out.
    • Use inter-container traffic encryption for distributed training that handles sensitive data.
    • Allow sagemaker:InvokeEndpoint on named endpoints only, and put an API layer in front of any model that outside callers use.
    • Block root access and public notebook links where possible, and apply a service control policy that denies creating resources outside a VPC.
    • Log with CloudTrail, keep CloudWatch logs for jobs and endpoints, and review who started which job.
    • Scan container images in ECR, build them in a pipeline and pin package versions.
    • Use Clarify and Model Cards to document how a model was built and tested, and keep that record with the registry entry.

    Governance & compliance

    Guardrails

    • Tagging rules for domains, jobs and endpoints (owner, project, data class, cost centre) checked when they are created
    • A service control policy that denies jobs and endpoints created without a VPC configuration or a KMS key
    • One shared pipeline template, so each model passes through training, evaluation, registry and approval in the same order
    • Approved instance types and Regions, with a request step for anything bigger

    Continuous checks (AWS Config)

    • “sagemaker-notebook-no-direct-internet-access”
    • “sagemaker-endpoint-configuration-kms-key-configured”
    • “sagemaker-notebook-instance-kms-key-configured”
    • “sagemaker-notebook-instance-inside-vpc”
    • A custom rule for endpoints that have run longer than a set time with no traffic

    Use AWS CloudTrail to see who started jobs and changed endpoints, AWS Config for configuration history and rules, SageMaker Model Cards and the Model Registry to record what a model is and who approved it, and AWS Artifact for compliance reports. Know who owns what: AWS secures the platform, while you answer for the data, how the model behaves, who can reach it and how releases are reviewed.

    Reduce carbon footprint

    • Train less often. Retrain on a schedule that matches how fast the data really changes, not on a habit.
    • Start small. A fine-tuned small model or a gradient-boosted tree often does the job of a huge network at a fraction of the energy.
    • Use spot capacity and warm pools so fewer machines sit idle between runs.
    • Switch off notebooks, Studio apps and test endpoints when you stop work, because an idle accelerator still draws power.
    • Pick a Region with a cleaner grid for training jobs that nobody is waiting on, as long as data rules allow it.
    • Use efficient hardware (Inferentia, Trainium or Graviton-based instances) and quantised models where they fit.
    • The AWS Customer Carbon Footprint Tool gives account-level estimates. Look at what it counts for SageMaker before you quote a number from it.

    Pricing

    Purchase options

    Notebooks and Studio Per instance-hour

    Studio apps and notebook instances bill for the instance while they run, plus storage. A stopped app doesn’t bill for compute.

    Training Per instance-time

    A training job bills for the instances it uses for as long as it runs. Spot capacity is cheaper, and the job costs nothing after it ends.

    Real-time endpoints Per instance-hour

    An endpoint bills for every instance for every hour it exists, traffic or not. Auto scaling changes how many instances there are.

    Serverless, async and batch Per use

    Serverless inference bills by the millisecond of compute plus data processed. Asynchronous endpoints can scale to zero. Batch transform bills for the job’s instances only.

    What you actually pay for

    • Instance time for notebooks, Studio apps, processing jobs, training jobs and endpoints, by instance type
    • Storage for notebooks and volumes, plus S3 for data and model artifacts
    • Serverless inference: compute time and data processed, plus any provisioned concurrency
    • Data transfer out and NAT gateway charges if traffic leaves the VPC
    • Optional features: Feature Store, Data Wrangler, Canvas sessions, HyperPod clusters and monitoring jobs

    Rates move with the Region and instance type. AWS lists a free tier for new SageMaker AI users that runs for the first two months and includes 50 hours of training and 125 hours of real-time inference on small instance types, plus 150,000 seconds of serverless inference. Check the official Amazon SageMaker AI pricing page and the AWS Pricing Calculator for current figures, and read the AWS Free Tier page for what new accounts get.

    Important code references

    The example predicts which customers will leave: train a small classifier on a CSV in S3, serve it from a real-time endpoint and send it one row. The CLI, Python and C# tabs do the work step by step; the Terraform tab builds the serving side (model, endpoint and auto scaling). The built-in XGBoost image has a different address in each Region, so look yours up first. Tear the endpoint down at the end, because billing carries on until you do.

    # Placeholders: your account ID, bucket and role. Mumbai is the example Region.
    REGION=ap-south-1
    BUCKET=acme-ml-123456789012
    ROLE=arn:aws:iam::123456789012:role/SageMakerExecutionRole
    # Built-in XGBoost image for your Region: see "Docker registry paths" in the SageMaker docs
    IMAGE=123456789012.dkr.ecr.ap-south-1.amazonaws.com/sagemaker-xgboost:1.7-1
    
    # 1. Train on spot capacity; checkpoints let an interrupted job pick up where it stopped
    aws sagemaker create-training-job --region $REGION \
      --training-job-name churn-001 \
      --role-arn $ROLE \
      --algorithm-specification TrainingImage=$IMAGE,TrainingInputMode=File \
      --hyper-parameters objective=binary:logistic,num_round=100 \
      --input-data-config '[{"ChannelName":"train","ContentType":"text/csv","DataSource":{"S3DataSource":{"S3DataType":"S3Prefix","S3Uri":"s3://'$BUCKET'/churn/train/","S3DataDistributionType":"FullyReplicated"}}}]' \
      --output-data-config S3OutputPath=s3://$BUCKET/churn/output/ \
      --resource-config InstanceType=ml.m5.xlarge,InstanceCount=1,VolumeSizeInGB=20 \
      --stopping-condition MaxRuntimeInSeconds=3600,MaxWaitTimeInSeconds=7200 \
      --enable-managed-spot-training \
      --checkpoint-config S3Uri=s3://$BUCKET/churn/checkpoints/
    
    aws sagemaker wait training-job-completed-or-stopped --region $REGION --training-job-name churn-001
    
    # 2. Turn the artifact into a model, then an endpoint configuration, then the endpoint
    aws sagemaker create-model --region $REGION --model-name churn-001 --execution-role-arn $ROLE \
      --primary-container Image=$IMAGE,ModelDataUrl=s3://$BUCKET/churn/output/churn-001/output/model.tar.gz
    
    aws sagemaker create-endpoint-config --region $REGION --endpoint-config-name churn-001 \
      --production-variants VariantName=AllTraffic,ModelName=churn-001,InstanceType=ml.m5.large,InitialInstanceCount=1
    
    aws sagemaker create-endpoint --region $REGION --endpoint-name churn --endpoint-config-name churn-001
    aws sagemaker wait endpoint-in-service --region $REGION --endpoint-name churn
    
    # 3. Score one customer (tenure, support calls, monthly charge, contract type)
    aws sagemaker-runtime invoke-endpoint --region $REGION --endpoint-name churn \
      --content-type text/csv --cli-binary-format raw-in-base64-out --body "34,2,79.5,1" prediction.json
    cat prediction.json
    
    # 4. Stop the meter
    aws sagemaker delete-endpoint --region $REGION --endpoint-name churn
    # Serving side only: assumes the model.tar.gz already exists in S3 and AWS provider version 4 or later
    variable "bucket" { type = string }
    variable "image"  { type = string }   # the XGBoost (or your own) image URI for this Region
    
    data "aws_iam_policy_document" "trust" {
      statement {
        actions = ["sts:AssumeRole"]
        principals {
          type        = "Service"
          identifiers = ["sagemaker.amazonaws.com"]
        }
      }
    }
    
    resource "aws_iam_role" "sagemaker" {
      name               = "churn-sagemaker-exec"
      assume_role_policy = data.aws_iam_policy_document.trust.json
    }
    
    # Read the model artifact only, not the whole account
    data "aws_iam_policy_document" "model_read" {
      statement {
        actions   = ["s3:GetObject"]
        resources = ["arn:aws:s3:::${var.bucket}/churn/output/*"]
      }
    }
    
    resource "aws_iam_role_policy" "model_read" {
      role   = aws_iam_role.sagemaker.id
      policy = data.aws_iam_policy_document.model_read.json
    }
    
    resource "aws_sagemaker_model" "churn" {
      name               = "churn-001"
      execution_role_arn = aws_iam_role.sagemaker.arn
    
      primary_container {
        image          = var.image
        model_data_url = "s3://${var.bucket}/churn/output/churn-001/output/model.tar.gz"
      }
    }
    
    resource "aws_sagemaker_endpoint_configuration" "churn" {
      name = "churn-001"
    
      production_variants {
        variant_name           = "AllTraffic"
        model_name             = aws_sagemaker_model.churn.name
        initial_instance_count = 1
        instance_type          = "ml.m5.large"
      }
    }
    
    resource "aws_sagemaker_endpoint" "churn" {
      name                 = "churn"
      endpoint_config_name = aws_sagemaker_endpoint_configuration.churn.name
    }
    
    # One to four instances, aiming for about 100 requests a minute on each
    resource "aws_appautoscaling_target" "churn" {
      service_namespace  = "sagemaker"
      resource_id        = "endpoint/${aws_sagemaker_endpoint.churn.name}/variant/AllTraffic"
      scalable_dimension = "sagemaker:variant:DesiredInstanceCount"
      min_capacity       = 1
      max_capacity       = 4
    }
    
    resource "aws_appautoscaling_policy" "churn" {
      name               = "invocations-per-instance"
      policy_type        = "TargetTrackingScaling"
      service_namespace  = aws_appautoscaling_target.churn.service_namespace
      resource_id        = aws_appautoscaling_target.churn.resource_id
      scalable_dimension = aws_appautoscaling_target.churn.scalable_dimension
    
      target_tracking_scaling_policy_configuration {
        target_value = 100
        predefined_metric_specification {
          predefined_metric_type = "SageMakerVariantInvocationsPerInstance"
        }
      }
    }
    # pip install boto3
    import boto3
    
    REGION = "ap-south-1"
    BUCKET = "acme-ml-123456789012"
    ROLE = "arn:aws:iam::123456789012:role/SageMakerExecutionRole"
    IMAGE = "123456789012.dkr.ecr.ap-south-1.amazonaws.com/sagemaker-xgboost:1.7-1"  # look up the one for your Region
    
    sm = boto3.client("sagemaker", region_name=REGION)
    runtime = boto3.client("sagemaker-runtime", region_name=REGION)
    
    
    def train(job_name):
        """Start a spot training job on a CSV in S3 and wait for it to finish. Returns the model artifact's S3 URL."""
        sm.create_training_job(
            TrainingJobName=job_name,
            RoleArn=ROLE,
            AlgorithmSpecification={"TrainingImage": IMAGE, "TrainingInputMode": "File"},
            HyperParameters={"objective": "binary:logistic", "num_round": "100"},
            InputDataConfig=[{
                "ChannelName": "train",
                "ContentType": "text/csv",
                "DataSource": {"S3DataSource": {
                    "S3DataType": "S3Prefix",
                    "S3Uri": f"s3://{BUCKET}/churn/train/",
                    "S3DataDistributionType": "FullyReplicated",
                }},
            }],
            OutputDataConfig={"S3OutputPath": f"s3://{BUCKET}/churn/output/"},
            ResourceConfig={"InstanceType": "ml.m5.xlarge", "InstanceCount": 1, "VolumeSizeInGB": 20},
            StoppingCondition={"MaxRuntimeInSeconds": 3600, "MaxWaitTimeInSeconds": 7200},
            EnableManagedSpotTraining=True,
            CheckpointConfig={"S3Uri": f"s3://{BUCKET}/churn/checkpoints/"},
        )
        sm.get_waiter("training_job_completed_or_stopped").wait(TrainingJobName=job_name)
        job = sm.describe_training_job(TrainingJobName=job_name)
        if job["TrainingJobStatus"] != "Completed":
            raise RuntimeError(job.get("FailureReason", job["TrainingJobStatus"]))
        return job["ModelArtifacts"]["S3ModelArtifacts"]
    
    
    def deploy(name, model_url):
        """Model, endpoint configuration and endpoint, in that order. Returns when the endpoint is InService."""
        sm.create_model(ModelName=name, ExecutionRoleArn=ROLE,
                        PrimaryContainer={"Image": IMAGE, "ModelDataUrl": model_url})
        sm.create_endpoint_config(EndpointConfigName=name, ProductionVariants=[{
            "VariantName": "AllTraffic", "ModelName": name,
            "InstanceType": "ml.m5.large", "InitialInstanceCount": 1,
        }])
        sm.create_endpoint(EndpointName=name, EndpointConfigName=name)
        sm.get_waiter("endpoint_in_service").wait(EndpointName=name)
    
    
    def score(endpoint, row):
        """Send one CSV row and return the model's answer as text."""
        reply = runtime.invoke_endpoint(EndpointName=endpoint, ContentType="text/csv", Body=row)
        return reply["Body"].read().decode("utf-8")
    
    
    def tear_down(name):
        """Delete the endpoint (this is what stops the billing), then its configuration and model."""
        sm.delete_endpoint(EndpointName=name)
        sm.delete_endpoint_config(EndpointConfigName=name)
        sm.delete_model(ModelName=name)
    
    
    if __name__ == "__main__":
        url = train("churn-001")
        deploy("churn", url)
        try:
            print(score("churn", "34,2,79.5,1"))
        finally:
            tear_down("churn")
    // Program.cs: create the endpoint for an existing model, wait for it, score one row, then delete it
    // NuGet packages: AWSSDK.SageMaker, AWSSDK.SageMakerRuntime
    using System.Text;
    using Amazon;
    using Amazon.SageMaker;
    using Amazon.SageMaker.Model;
    using Amazon.SageMakerRuntime;
    using Amazon.SageMakerRuntime.Model;
    
    const string name = "churn";
    
    using var sm = new AmazonSageMakerClient(RegionEndpoint.APSouth1);
    using var runtime = new AmazonSageMakerRuntimeClient(RegionEndpoint.APSouth1);
    
    // The model "churn-001" must already exist (created by the training steps or by Terraform)
    await sm.CreateEndpointConfigAsync(new CreateEndpointConfigRequest
    {
        EndpointConfigName = name,
        ProductionVariants = new List<ProductionVariant>
        {
            new ProductionVariant
            {
                VariantName = "AllTraffic",
                ModelName = "churn-001",
                InstanceType = ProductionVariantInstanceType.MlM5Large,
                InitialInstanceCount = 1
            }
        }
    });
    await sm.CreateEndpointAsync(new CreateEndpointRequest { EndpointName = name, EndpointConfigName = name });
    
    // Poll until InService (the SDK has no built-in waiter here)
    while (true)
    {
        var status = (await sm.DescribeEndpointAsync(new DescribeEndpointRequest { EndpointName = name })).EndpointStatus;
        Console.WriteLine($"Endpoint status: {status}");
        if (status == EndpointStatus.InService) break;
        if (status == EndpointStatus.Failed) throw new InvalidOperationException("Endpoint creation failed; check its CloudWatch logs.");
        await Task.Delay(TimeSpan.FromSeconds(30));
    }
    
    try
    {
        var reply = await runtime.InvokeEndpointAsync(new InvokeEndpointRequest
        {
            EndpointName = name,
            ContentType = "text/csv",
            Body = new MemoryStream(Encoding.UTF8.GetBytes("34,2,79.5,1"))
        });
        using var reader = new StreamReader(reply.Body);
        Console.WriteLine($"Prediction: {await reader.ReadToEndAsync()}");
    }
    finally
    {
        // Deleting the endpoint is what stops the billing
        await sm.DeleteEndpointAsync(new DeleteEndpointRequest { EndpointName = name });
    }

    Best videos to explore

    Short walkthrough videos for this service are being prepared. They will appear here and load only when you press play. Until then, follow CloudBrum on YouTube for new videos.

    Introduction to Amazon SageMakerVideo coming soon · Beginner
    Training and tuning a model in SageMakerVideo coming soon · Intermediate
    Choosing a SageMaker inference optionVideo coming soon · Intermediate
    Cutting SageMaker costs: spot, scaling and idle shutdownVideo coming soon · Advanced

    Interview questions

    BasicWhat is Amazon SageMaker?

    It’s AWS’s managed service for building, training and deploying machine learning models. You get notebooks and a studio to work in, training jobs that run on instances SageMaker starts and stops for you, and endpoints that serve the finished model over HTTPS. AWS now names the machine learning service SageMaker AI and uses SageMaker for the larger data and AI platform around it.

    BasicWhat is the difference between a training job and an endpoint?

    A training job is a one-off run: it reads data from S3, writes a model artifact back to S3 and stops. An endpoint is a long-running service that loads that artifact and answers prediction requests. You pay for the job while it runs, and for the endpoint every hour it exists.

    BasicName the ways SageMaker can serve a model.

    Real-time endpoints for steady, low-latency traffic. Serverless endpoints for traffic that comes and goes. Asynchronous endpoints for large payloads or slow models, which can also scale to zero. Batch transform for scoring a whole dataset in one job with no endpoint at all.

    IntermediateHow does managed spot training work, and what do you need to do for it?

    SageMaker runs the job on spare capacity at a discount that AWS puts at up to 90%. If AWS needs the machines back, the job is interrupted. To survive that, the training code writes checkpoints to the S3 location in the job configuration and loads the latest one when it starts. Set the maximum wait time above the maximum run time.

    IntermediateAn endpoint is stuck in Creating and then fails. Where do you look?

    The endpoint’s CloudWatch log group, first. The usual cause is a container that doesn’t answer the health check on /ping on port 8080 in time, either because of a bug or because a large model takes longer to load than the startup timeout. Fix the container, raise the timeout or pick a bigger instance.

    IntermediateHow do you keep training data and models private?

    Run jobs and Studio in a VPC with private subnets and VPC endpoints for S3 and ECR. Encrypt S3, volumes and endpoint storage with KMS keys, switch on network isolation for containers that don’t need outbound access, and scope the execution role to the specific buckets. Allow InvokeEndpoint only on the endpoints a caller needs.

    AdvancedYour team hosts 200 small models, one per customer, and the endpoint bill is huge. What do you change?

    One endpoint per model wastes instances. Use a multi-model endpoint, which loads models from S3 on demand into a shared fleet, or inference components that give each model its own slice of an endpoint and scale independently. Accept a slower first request for models that aren’t in memory, and watch the model cache hit rate.

    AdvancedHow would you roll out a new model version with no downtime and a way back?

    Register the new version in the Model Registry and approve it. Deploy through the endpoint’s deployment guardrails using canary or linear traffic shifting, with CloudWatch alarms that trigger an automatic rollback. For extra safety, run a shadow variant first so the new model sees real traffic without answering it.

    AdvancedPredictions have got worse over three months and nobody changed the model. How do you diagnose it?

    Suspect drift. Compare current input statistics with the training baseline using Model Monitor, check whether an upstream data source changed format or meaning, and compare predictions with the labels as they arrive. If the data really moved, retrain through the pipeline on fresh data, evaluate against the current model and promote it through the registry.

    AdvancedA training run needs 64 GPUs for two weeks. What do you think about before you start?

    Quota first, since large GPU counts need a request well ahead of time. Then resilience: HyperPod or a job with frequent checkpoints so one failed node doesn’t cost days. Then data throughput from S3 or FSx for Lustre, networking between nodes, and a budget alarm. Run a short trial at small scale to check the code and the cost per step before committing.

    Similar or alternative services on other clouds

    CapabilityAWSAzureGoogle Cloud
    ServiceAmazon SageMaker (SageMaker AI)Azure Machine LearningVertex AI, now part of the Gemini Enterprise Agent Platform
    Where you workStudio with JupyterLab, plus Canvas for no-codeAzure ML studio, notebooks and VS CodeWorkbench and Colab Enterprise notebooks
    What starts the training machinesA training job on ml.* instancesA command job on a compute clusterA custom training job on managed machines
    Cheap, interruptible capacityManaged spot training with checkpointsLow-priority or spot nodes in a clusterSpot VMs, or Flex-start to queue for scarce GPUs
    Keeping model versionsModel Registry with approval statusModel registry, shareable across workspaces through registriesModel Registry
    Repeatable workflowsSageMaker PipelinesPipelines built from componentsVertex AI Pipelines (Kubeflow Pipelines based)
    Serving that scales to zeroAsynchronous endpoints, and inference componentsBatch endpoints; managed online endpoints keep a running instanceEndpoints can be set to scale to zero
    Catalogue of ready-made modelsJumpStartModel catalogModel Garden

    Not every model needs this much machinery. If you only want to call a hosted language model, Amazon Bedrock does that with no instances to manage, and ready-made vision or text APIs are quicker still. Small models can live inside AWS Lambda, and the training data usually starts life in Amazon S3.

    Did you find this article helpful?

    Your answer helps us improve this page for the next reader.

    Sources and further reading

    CloudBrum wrote this page in its own words, using the provider’s public documentation listed below as the reference. Features, limits, names and prices change, so confirm anything that matters on the provider’s own pages before you decide.