Sm Deployment Guardrails Update Inference Endpoint With With Canary Traffic Shifting
Leverage deployment guardrails to update a SageMaker Inference endpoint using canary traffic shifting
This notebook's CI test result for us-west-2 is as follows. CI test results in other regions can be found at the end of the notebook.
SageMaker Studio Kernel: Data Science
Introduction
Deployment guardrails are a set of model deployment options in Amazon SageMaker Inference to update your machine learning models in production. Using the fully managed deployment guardrails, you can control the switch from the current model in production to a new one. Traffic shifting modes, such as canary and linear, give you granular control over the traffic shifting process from your current model to the new one during the course of the update. There are also built-in safeguards such as auto-rollbacks that help you catch issues early and take corrective action before they impact production.
We support blue-green deployment with multiple traffic shifting modes. A traffic shifting mode is a configuration that specifies how endpoint traffic is routed to a new fleet containing your updates. The following traffic shifting modes provide you with different levels of control over the endpoint update process:
- All-At-Once Traffic Shifting : shifts all of your endpoint traffic from the blue fleet to the green fleet. Once the traffic has shifted to the green fleet, your pre-specified Amazon CloudWatch alarms begin monitoring the green fleet for a set amount of time (the “baking period”). If no alarms are triggered during the baking period, then the blue fleet is terminated.
- Canary Traffic Shifting : lets you shift one small portion of your traffic (a “canary”) to the green fleet and monitor it for a baking period. If the canary succeeds on the green fleet, then the rest of the traffic is shifted from the blue fleet to the green fleet before terminating the blue fleet.
- Linear Traffic Shifting : provides even more customization over how many traffic-shifting steps to make and what percentage of traffic to shift for each step. While canary shifting lets you shift traffic in two steps, linear shifting extends this to n number of linearly spaced steps.
The Deployment guardrails for Amazon SageMaker Inference endpoints feature also allows customers to specify conditions/alarms based on Endpoint invocation metrics from CloudWatch to detect model performance regressions and trigger automatic rollback.
In this notebook we'll update endpoint with following deployment configurations:
- Blue/Green update policy with Canary traffic shifting option
- Configure CloudWatch alarms to monitor model performance and trigger auto-rollback action.
To demonstrate Canary deployments and the auto-rollback feature, we will update an Endpoint with an incompatible model version and deploy it as a Canary fleet, taking a small percentage of the traffic. Requests sent to this Canary fleet will result in errors, which will be used to trigger a rollback using pre-specified CloudWatch alarms. Finally, we will also demonstrate a success scenario where no alarms are tripped and the update succeeds.
This notebook is organized in 4 steps -
- Step 1 creates the models and Endpoint Configurations required for the 3 scenarios - the baseline, the update containing the incompatible model version and the update containing the correct model version.
- Step 2 invokes the baseline Endpoint prior to the update.
- Step 3 specifies the CloudWatch alarms used to trigger the rollbacks.
- Finally in step 4, we update the endpoint to trigger a rollback and demonstrate a successful update.
Setup some required imports and basic initial variables:
Download the Input files and pre-trained model from S3 bucket
Step 1: Create and deploy the models
First, we upload our pre-trained models to Amazon S3
This code uploads two pre-trained XGBoost models that are ready for you to deploy. These models were trained using the XGB Churn Prediction Notebook in SageMaker. You can also use your own pre-trained models in this step. If you already have a pretrained model in Amazon S3, you can add it by specifying the s3_key.
The models in this example are used to predict the probability of a mobile customer leaving their current mobile operator. The dataset we use is publicly available and was mentioned in the book Discovering Knowledge in Data by Daniel T. Larose. It is attributed by the author to the University of California Irvine Repository of Machine Learning Datasets.
Next, we create our model definitions
Start with deploying the pre-trained churn prediction models. Here, you create the model objects with the image and model data. The three URIs correspond to the baseline version, the update containing the incompatible version, and the update containing the correct model version.
Create Endpoint Configs
We now create three EndpointConfigs, corresponding to the three Models we created in the previous step.
Create Endpoint
Deploy the baseline model to a new SageMaker endpoint:
Wait for the endpoint creation to complete.
Invocations Metrics
Amazon SageMaker emits metrics such as Latency and Invocations per variant/Endpoint Config (full list of metrics here) in Amazon CloudWatch.
Query CloudWatch to get number of Invocations and latency metrics per variant and endpoint configuration.
Plot endpoint invocation metrics:
Below, we are going to plot graphs to show the Invocations,Invocation4XXErrors,Invocation5XXErrors,ModelLatency and OverheadLatency against the Endpoint.
You will observe that there should be a flat line for Invocation4XXErrors and Invocation5XXErrors as we are using the correct model version and configs. Additionally, ModelLatency and OverheadLatency will start decreasing over time.
BlueGreen update policy with Canary traffic shifting
We define the following deployment configuration to perform Blue/Green update strategy with Canary traffic shifting from old to new stack. The Canary traffic shifting option can reduce the blast ratio of a regressive update to the endpoint. In contrast, for the All-At-Once traffic shifting option, the invocation requests start failing at 100% after flipping the traffic. In the Canary mode, invocation requests are shifted to the new version of model gradually, preventing errors from impacting 100% of your traffic. Additionally, the auto-rollback alarms monitor the metrics during the canary stage.
Rollback Case

Update the Endpoint with an incompatible model version to simulate errors and trigger a rollback.
We invoke the endpoint during the update operation is in progress.
Note : Invoke endpoint in this notebook is in single thread mode, to stop the invoke requests please stop the cell execution
The E's denote the errors generated from the incompatible model version in the canary fleet.
The purpose of the below cell is to simulate errors in the canary fleet. Since the nature of traffic shifting to the canary fleet is probabilistic, you should wait until you start seeing errors. Then, you may proceed to stop the execution of the below cell. If not aborted, cell will run for 600 invocations.
Wait for the update operation to complete and verify the automatic rollback.
Collect the endpoint metrics during the deployment:
Below, we are going to plot graphs to show the Invocations,Invocation5XXErrors and ModelLatency against the Endpoint.
You can expect to see as the new endpoint config-2 (erroneous due to model version) starts getting deployed, it encounters failure and leads to the rollback to endpoint config-1. This can be seen in the graphs below as the Invocation5XXErrors and ModelLatency increases during this rollback phase
Let's take a look at the Success case where we use the same Canary deployment configuration but a valid endpoint configuration.
Success Case

Now we show the success case where the Endpoint Configuration is updated to a valid version (using the same Canary deployment config as the rollback case).
Update the endpoint with the same Canary deployment configuration:
Invoke the endpoint during the update operation is in progress:
Wait for the update operation to complete:
Collect the endpoint metrics during the deployment:
Below, we are going to plot graphs to show the Invocations,Invocation5XXErrors and ModelLatency against the Endpoint.
You can expect to see that, as the new endpoint config-3 (correct model version) starts getting deployed, it takes over endpoint config-2 (incompatible due to model version) without any errors. This can be seen in the graphs below as the Invocation5XXErrors and ModelLatency decreases during this transition phase
The Amazon CloudWatch metrics for the total invocations for each endpoint config shows how invocation requests are shifted from the old version to the new version during deployment.
You can now safely update your endpoint and monitor model regressions during deployment and trigger auto-rollback action.
Notebook CI Test Results
This notebook was tested in multiple regions. The test results are as follows, except for us-west-2 which is shown at the top of the notebook.