Lesson 4 of 4 Model Deployment and Inference

Deploying and Invoking Models Programmatically

This lesson explains the resources that the SageMaker Python SDK, the AWS CLI and Boto3 create when models are deployed, since production deployments are automated, not clicked.

Learning outcomes

After completing this lesson, you will be able to:

  • Create the model, endpoint configuration and endpoint resources.
  • Deploy with the SageMaker Python SDK.
  • Invoke endpoints synchronously and asynchronously.
  • Update an endpoint without downtime.

The three hosting resources

SageMaker AI hosting uses three resources. A model combines an inference container image, the S3 location of model artefacts and an execution role. An endpoint configuration defines one or more production variants (model, instance type, initial instance count, traffic weight) and options such as data capture, asynchronous or serverless settings and KMS keys. An endpoint is created from an endpoint configuration. To change instance type, model or variants, create a new endpoint configuration and call UpdateEndpoint; SageMaker AI performs a blue/green update without downtime.

aws sagemaker create-model --model-name churn-v7 \
  --primary-container Image=$IMAGE,ModelDataUrl=s3://ml/churn/v7/model.tar.gz \
  --execution-role-arn $ROLE
aws sagemaker create-endpoint-config --endpoint-config-name churn-v7-cfg \
  --production-variants VariantName=AllTraffic,ModelName=churn-v7,InstanceType=ml.m5.large,InitialInstanceCount=2
aws sagemaker update-endpoint --endpoint-name churn --endpoint-config-name churn-v7-cfg

The following diagram shows the resources created to upgrade a live SageMaker AI endpoint and the blue/green update that avoids an outage.

Upgrading the live endpoint without an outage. The team creates a new model and a new endpoint configuration and calls UpdateEndpoint; SageMaker AI provisions the new fleet, shifts traffic to it, optionally under deployment guardrails and alarms, and removes the old fleet. The endpoint name stays the same, so callers change nothing.
The scenario is illustrative.
Put in order

Create an endpoint

Exam skills 3.2.5

Arrange the API calls that deploy a model to a real-time endpoint.

Log in to try this activity Interactive activities are free with an account.

SageMaker Python SDK

The SageMaker Python SDK wraps the underlying API calls in a few high-level methods. estimator.deploy() deploys a model directly after training; Model(image_uri, model_data, role).deploy() deploys existing model artefacts with a chosen container; ModelBuilder packages models from many frameworks and selects suitable containers automatically; and a Predictor invokes the endpoint, with serializers and deserializers that convert data to and from the request format.

Serverless and asynchronous options are selected by passing a ServerlessInferenceConfig or AsyncInferenceConfig when deploying, so the same code deploys to any of the inference options.

Spot the bug

A deployment script

Exam skills 3.2.5

Find the three lines that make this SageMaker Python SDK deployment fail or misbehave.

Log in to try this activity Interactive activities are free with an account.

Invoking endpoints

Clients use the sagemaker-runtime API: InvokeEndpoint for synchronous requests, InvokeEndpointAsync with an S3 input location for asynchronous endpoints and InvokeEndpointWithResponseStream to stream tokens from LLMs. Infrastructure as code with CloudFormation or CDK can define the same resources declaratively. The following table summarises the operations.

TaskAPI / SDK
Register a modelCreateModel / Model()
Define variants and optionsCreateEndpointConfig
Create or update an endpointCreateEndpoint / UpdateEndpoint
Change traffic weights without redeployingUpdateEndpointWeightsAndCapacities
Synchronous requestInvokeEndpoint
Queued requestInvokeEndpointAsync
Streamed responseInvokeEndpointWithResponseStream
Match the pairs

Invocation options

Exam skills 3.2.5

Match each invocation need with the runtime call or parameter.

Log in to try this activity Interactive activities are free with an account.
Worked example

A zero-downtime upgrade

A team must move a live endpoint from ml.m5.xlarge to ml.c6i.xlarge and to a new model version without interrupting callers. A colleague suggests deleting and recreating the endpoint with the same name.

Deleting the endpoint would cause an outage. Instead, create the new model and a new endpoint configuration, then call UpdateEndpoint. SageMaker AI provisions the new fleet, shifts traffic (optionally with deployment guardrails and alarms) and removes the old fleet. The endpoint name, and therefore client configuration, stays the same.

Check yourself

How are only the traffic weights between variants changed?

Show answer

With UpdateEndpointWeightsAndCapacities, without a new endpoint configuration.

Exam Tips

Remember for the exam

  • Hosting resources: model, endpoint configuration, endpoint.
  • Change instance type or model: new endpoint configuration, then UpdateEndpoint.
  • Change traffic weights only: UpdateEndpointWeightsAndCapacities.
  • Stream LLM tokens: InvokeEndpointWithResponseStream.
  • Never delete and recreate a live endpoint to upgrade it.