Mastering the mystical art of model deployment, part 2: deploying Amazon SageMaker endpoints with…
Tags: devops, ai, machinelearning, aws
Published: 2019-05-20 09:01:38

---
title: Mastering the mystical art of model deployment, part 2: deploying Amazon SageMaker endpoints with…
published: true
tags: devops, artificialintellige, machinelearning, aws
canonical_url: https://medium.com/@julsimon/mastering-the-mystical-art-of-model-deployment-part-2-deploying-amazon-sagemaker-endpoints-with-cf9539dc2579
---

### Mastering the mystical art of model deployment, part 2: deploying Amazon SageMaker endpoints with AWS CloudFormation

In a previous [article](https://dev.to/juliensimon/mastering-the-mystical-art-of-model-deployment-2afi), I discussed several deployment strategies for machine learning models, and I showed you how to use the [SageMaker](http://aws.amazon.com/sagemaker) [SDK](https://github.com/aws/sagemaker-python-sdk) to deploy several models on the same prediction endpoint.

This is all fine and dandy, but in a production setting, I’d bet that most of you would prefer **automating** this properly with [CloudFormation](http://aws.amazon.com/cloudformation). One-click deployment, automatic clean-up and more: what’s not to like?

In this post, I’ll show you how to:

- **deploy a SageMaker endpoint backed by a single model** ,
- **scale the endpoint by adding more instances,**
- **add an extra production variant to the endpoint** , i.e. add a second model to the endpoint and set up weighted round-robin,
- **do things The-Right-Way** , with nice YAML [templates](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/gettingstarted.templatebasics.html) and [change sets](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-changesets.html).

> As usual, all code is available on [Gitlab](https://gitlab.com/juliensimon/sagemaker-automation/tree/master/cloudformation).

Alright, magic time!

![](https://cdn-images-1.medium.com/max/540/0*bbaw5i7z11hQ9YZS.gif)<figcaption>And this, my friend, is how you deploy two production variants.</figcaption>

### Deploying a SageMaker endpoint

First, let’s write a template that lets us deploy a single model. We need to create Cloudformation resources for the [model](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sagemaker-model.html), the [endpoint configuration](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sagemaker-endpointconfig.html) and the [endpoint](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sagemaker-endpoint.html) itself. I’ve tried to keep the template as straightforward as possible, and it should work with any single-container model

> For [inference pipelines](https://docs.aws.amazon.com/sagemaker/latest/dg/inference-pipelines.html), you’ll have to list all containers in the Model resource.

{% gist https://gist.github.com/juliensimon/bebdcb1f9755ec46411213fd1b239d59 %}

As you can see, we’ll need to pass the container name, the location of the model artefact, and the role as parameters. These strings are not user-friendly, and it would be nicer to work with the training job name.

A bit of Python will help us with that. Thanks to the SageMaker SDK, we can easily describe a training job, extract the values we need and use them to create our stack.

{% gist https://gist.github.com/juliensimon/cff850677c3d8f43cb1979e902da1ae4 %}

All right, let’s run this.

```
$ python create-stack.py
xgboost-2019-05-09-15-20-51-276
{u'StackId': 'arn:aws:cloudformation:eu-west-1:123456789012:stack/endpoint-one-model/06661d80-78b5-11e9-9878-066324f10f26', 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '065e0749-78b5-11e9-a5d5-af6ed726348b', 'HTTPHeaders': {'x-amzn-requestid': '065e0749-78b5-11e9-a5d5-af6ed726348b', 'date': 'Fri, 17 May 2019 15:04:09 GMT', 'content-length': '388', 'content-type': 'text/xml'}}}
```

Meh. Let’s take a look at the Cloudformation console.

![](https://cdn-images-1.medium.com/max/839/1*LPk3FaOkC0HS_tpSh2TdZg.png)

Pretty much what we expected: first create the model (i.e. “register” the model artifact in S3 as something that can be deployed), then the endpoint configuration, and finally the endpoint.

After a few minutes, the endpoint is in service and visible in the SageMaker console. Woohoo.

![](https://cdn-images-1.medium.com/max/1024/1*paEZzrXNM5MnuFhMdRnykQ.png)

#### Updating the endpoint configuration

Now, let’s say we’d like to add a second instance to this endpoint. Now, don’t you start clicking in the console, ya hear me? This would introduce drift in the stack we just created, and we don’t like that around here.

Instead, let’s define a **change set** , and check its impact before executing it... which is how battle-scarred veterans manage production systems ;) I’m using the CLI (because), feel free to use your favorite language SDK if that works better for you.

```
$ aws cloudformation create-change-set \
--stack-name endpoint-one-model \
--change-set-name add-instances \
--use-previous-template \
--parameters ParameterKey="InstanceCount",ParameterValue="2" \
             ParameterKey=ModelName,UsePreviousValue=true \
             ParameterKey="ModelDataUrl",UsePreviousValue=true \
             ParameterKey="TrainingImage",UsePreviousValue=true \
             ParameterKey="RoleArn",UsePreviousValue=true
```

Basically, we’re telling Cloudformation: “_hey, I’d like to use the same template with the same parameters as before, except that I want two instances now. Tell me what’s going to happen_”.

No change is applied at this stage. We could call ‘_aws cloudformation describe-change-set_’ to see what’s going to happen, or we can look at a console for a nicer experience.

![](https://cdn-images-1.medium.com/max/1024/1*Hz4xlSREBkwvhzlxv2XPmw.png)

The instance count is defined in the endpoint configuration, which is itself associated to the endpoint, so it makes sense that both resources need to be updated. The harder question is: do they need to be replaced, and will the endpoint go down? ‘_Conditional_’ basically means ‘It depends’ (or ‘Roll a dice’ if you feel sarcastic). At this point, you would need to look at the Cloudformation documentation for these resources, and probably at the SageMaker documentation as well: **in a production environment, getting this right is the difference between an invisible change and downtime, so RTFM**.

I’ll save you the trouble (this time): the endpoint will not be taken down. A new endpoint configuration will be created, and applied to the endpoint without any downtime.

OK, now that we feel good about this change, let’s apply it.

```
$ aws cloudformation execute-change-set \
--change-set-name add-instances \
--stack-name endpoint-one-model
```

Done. Pretty quickly, we see the endpoint going into the ‘_Updating_’ status in the SageMaker console.

![](https://cdn-images-1.medium.com/max/1024/1*uusIW6_2x9jxieKCO7SAzg.png)

A few minutes later, the change set has been fully applied, and the Cloudformation console tells us the full story: a new endpoint configuration was created and applied to the endpoint.

![](https://cdn-images-1.medium.com/max/971/1*Ns5Z6zKoqVkjtC_3ok7dbQ.png)

Let’s take a quick look at the SageMaker console. Yup: the endpoint is ‘_Active_’ again, and it’s backed by two instances.

![](https://cdn-images-1.medium.com/max/852/1*HgLFFGItJPtgkgF0ch3wzA.png)

Pretty cool. Now let’s take things up a notch, and add a second model to the endpoint.

### Adding a second model to the endpoint

Let’s say we’d like to use **canary deployment** to test a new model on the endpoint, starting with a fraction of live traffic and gradually increasing it to 100% (or rolling back…). In SageMaker terms, we need to create a second **production variant** running the new model.

Here’s the updated template. We’re simply adding a **second _Model_ resource** , as well as a **second production variant in the _EndpointConfig_ resource**. This second variant will get a third of the traffic (0.5/(1+0.5), meaning that the first one will get two thirds (1/1+0.5).

> In a real production setting, we would certainly start lower (say, 10%) and gradually increase that number.

{% gist https://gist.github.com/juliensimon/a51de96cdfc0d9571fb66d2e6a17a78b %}

Here, I’d like to deploy another XGBoost model trained with the same container. If you want to use a model trained with another container, simply add another parameter, e.g. _TrainingImage2_.

Let’s create a change set. This time, we’re using an updated template, so we need to pass its location.

```
$ export MODEL2\_NAME="xgboost-190509-1528-016-39860b48"

$ export MODEL2\_ARTEFACT=""s3://sagemaker-eu-west-1-123456789012/sagemaker/DEMO-hpo-xgboost-dm/output/xgboost-190509-1528-016-39860b48/output/model.tar.gz"

$ aws cloudformation create-change-set \
--stack-name endpoint-one-model \
--change-set-name add-production-variant \
--template-body "file://endpoint-two-models.yml" \
--parameters \
   ParameterKey=ModelName2,ParameterValue=$MODEL2\_NAME \
   ParameterKey="ModelDataUrl2",ParameterValue=$MODEL2\_ARTEFACT \
   ParameterKey=ModelName,UsePreviousValue=true \
   ParameterKey="ModelDataUrl",UsePreviousValue=true \
   ParameterKey="TrainingImage",UsePreviousValue=true \
   ParameterKey="RoleArn",UsePreviousValue=true
```

Let’s check the impact in the console.

![](https://cdn-images-1.medium.com/max/1024/1*hTJ5J8h5ddwvmt4iKn40rQ.png)

Cloudformation will create a new model, and then a new endpoint configuration which will be applied to the endpoint. Here too, the endpoint will stay up during the whole process.

Let’s execute the change set.

```
$ aws cloudformation execute-change-set --change-set-name add-production-variant --stack-name endpoint-one-model
```

Once again, the Cloudformation console displays the updates to the stack

![](https://cdn-images-1.medium.com/max/839/1*teWsWat0tFvHDvISPLmxcA.png)

After a few minutes, the update is complete. The SageMaker console confirms that the endpoint is ‘_Active_’, and that it’s now running two production variants. Note that _variant-1_ uses a single instance again, as defined in our updated template.

![](https://cdn-images-1.medium.com/max/850/1*sCwaoHDJRV_4TPOFpv_bwg.png)

![](https://cdn-images-1.medium.com/max/1024/1*35C9CoHRUNoow2lhpPVwiQ.png)

Once we’re satisfied that the model works as intended, we can update the stack again to shift all trafic to _variant-2._

### Cleaning up

What if we want to take this endpoint down? Super easy: just delete the stack, and Cloudformation will clean everything up automatically.

> Careful with the ‘Delete Stack’ API: no confirmation will be asked for! Many DevOps engineers have learned this a bit too late… Needless to say, the AWS world breathed a sigh of relief when [stack termination protection](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-protect-stacks.html) was introduced :)

```
$ aws cloudformation delete-stack --stack-name endpoint-one-model
```

### Conclusion

As you can see, you can easily build safe and automated deployment workflows for SageMaker endpoints. Sure, the SageMaker SDK is great for development and testing, but when it comes to production, I don’t think you can beat the predictability and robustness of Cloudformation.

As always, thank you very much for reading. Happy to answer questions here or on [Twitter](https://twitter.com/julsimon).

_Come, come to the Sabbath, let’s deploy Machine Learning models ;)_

{% youtube ou7skBJ8rno %}