Best Practices for Deploying Models on SageMaker Hosting Services with Recommended AWS CDK Project Structure

tfc

Hatched by tfc

Sep 01, 2023

4 min read

0

Best Practices for Deploying Models on SageMaker Hosting Services with Recommended AWS CDK Project Structure

Introduction:
Deploying machine learning models on SageMaker Hosting Services requires careful consideration to ensure optimal performance and flexibility. Additionally, organizing your project directory structure in a logical manner using the AWS CDK can greatly simplify future changes and maintenance. In this article, we will explore the best practices for deploying models on SageMaker Hosting Services and recommended project structure using the AWS CDK.

Deploying Models on SageMaker Hosting Services:
When hosting models using SageMaker hosting services, there are a few important points to keep in mind. Firstly, client applications can send requests to the SageMaker HTTPS endpoint to obtain inferences from a deployed model. This endpoint can also be accessed from a Jupyter notebook during testing, allowing for easy debugging and validation.

Secondly, it is possible to deploy a model trained with SageMaker to your own deployment target. To achieve this, you need to understand the algorithm-specific format of the model artifacts generated during model training. Familiarizing yourself with the output formats corresponding to the algorithm you are using is crucial for successful deployment.

Furthermore, deploying multiple variants of a model to the same SageMaker HTTPS endpoint can be highly beneficial, especially for testing purposes. Suppose you have a model deployed in production and want to test a variation of the model by directing a small amount of traffic, such as 5%, to the new model. To accomplish this, create an endpoint configuration that describes both variants of the model and specify the ProductionVariant in your request to the CreateEndPointConfig.

It is worth noting that you can configure a ProductionVariant to use Application Auto Scaling, which allows for automatic scaling of your SageMaker models. This feature is particularly useful when dealing with fluctuating workloads and ensures optimal resource allocation.

Modifying an endpoint without disrupting models already deployed in production is another essential practice. You can add new model variants, update the ML Compute instance configurations of existing model variants, or change the distribution of traffic among model variants without any downtime. This is achieved by providing a new endpoint configuration, and SageMaker implements the changes seamlessly.

However, it is crucial to avoid changing or deleting model artifacts or inference code after deploying a model, as this can lead to unpredictable results. If modifications are necessary, it is recommended to modify the endpoint by providing a new endpoint configuration. Once the new configuration is provided, the old model artifacts corresponding to the previous endpoint configuration can be changed or deleted safely.

For scenarios where inferences on entire datasets are required, batch transform can be a more suitable alternative to hosting services. Batch transform provides a convenient way to process large volumes of data in parallel, saving time and resources. It is important to consider this option when dealing with extensive data processing requirements.

Recommended AWS CDK Project Structure:
To ensure maintainability and ease of development, it is crucial to organize your AWS CDK project directory structure based on logical units. Each logical unit should have its own directory, encompassing the related infrastructure, runtime, and configuration code.

For example, consider the following directory structure:

|-- backend  
|   |-- api  
|   |   |-- runtime  
|   |   |   |-- lambda_function.py  
|   |   |   `-- requirements.txt  
|   |   `-- infrastructure.py  
|-- database  
|   `-- infrastructure.py  

By organizing your project structure in this manner, it becomes much easier to locate and modify code related to specific logical units. If you need to make changes to the API, you can easily find the code within the backend/api directory. This approach also allows for separating logical units into self-contained entities, enabling better ownership and refactoring capabilities.

It is important to implement logical units as constructs rather than stacks. Constructs are the building blocks of AWS CDK applications, while stacks serve as deployment units. By implementing logical units as constructs, you gain flexibility in supporting different deployment layouts and the ability to reuse them as construct libraries in the future.

When refactoring constructs, it is crucial to consider logical ID stability to avoid unexpected infrastructure changes. Logical ID stability ensures that changes to the infrastructure code do not inadvertently impact the deployed infrastructure.

Conclusion:
Deploying models on SageMaker Hosting Services and organizing your AWS CDK project structure are critical components of successful machine learning development. By following the best practices outlined in this article, you can ensure optimal performance, flexibility, and maintainability.

Actionable Advice:

  1. Familiarize yourself with the output formats specific to the algorithm you are using in SageMaker. This knowledge is essential for successful model deployment.
  2. Utilize multiple variants of a model on a single SageMaker HTTPS endpoint for testing and experimentation purposes. This allows for easy comparison and validation of different models.
  3. Organize your AWS CDK project directory structure based on logical units and implement them as constructs. This approach improves maintainability and facilitates future changes and refactoring.

Remember, deploying models on SageMaker Hosting Services and structuring your AWS CDK projects are ongoing processes that require continuous refinement and improvement. By incorporating these best practices into your workflow, you can streamline your machine learning deployments and development processes.

Sources

← Back to Library

Hatch New Ideas with Glasp AI 🐣

Glasp AI allows you to hatch new ideas based on your curated content. Let's curate and create with Glasp AI :)

Start Hatching 🐣