Introduction
Amazon SageMaker from AWS is a useful platform for deploying and managing machine learning models. However, real-time configuration updates of SageMaker AI endpoints can lead to various issues. Specifically, when deploying through AWS CloudFormation or AWS Cloud Development Kit (AWS CDK), changing the instance type or environment variables of the endpoint can cause deployment failures. This article presents a method for managing automated scaling using AWS CDK to address these issues.
Causes of the Problem
The failure of configuration updates for SageMaker AI endpoints occurs when attempting to change the endpoint configuration while the scaling target is registered. AWS CloudFormation attempts to update the endpoint while the scaling target is registered, but SageMaker AI produces the following error: > "SageMaker AI blocks the endpoint update before CloudFormation reaches the auto-scaling resources." In this situation, manually deregistering the scaling target and changing the endpoint configuration results in a successful deployment, but CloudFormation will not register the scaling.
Utilizing CDK Constructs
To resolve these issues using AWS CDK, we introduce a reusable CDK construct that automatically registers and deregisters scaling. This construct uses custom resources from AWS CloudFormation to automatically manage scaling during endpoint updates. Specifically, the process proceeds as follows:
- Deregister (Custom Resource): Deregisters the scaling target before the endpoint update begins.
- Endpoint Update (CDK Management): CDK successfully updates the endpoint resources.
- Register Scalable Target (Custom Resource): Registers the scaling target after the endpoint reaches the InService state.
- Attach Scaling Policy (Custom Resource): Applies the target tracking policy once the scaling target becomes queryable.
This process is clearly managed through the dependency order of CDK, and each custom resource is supported by AWS Lambda functions. This creates an environment where endpoint configurations and scaling parameters can be updated simultaneously.
Deployment Process
The process of deploying the SageMaker AI endpoint with automated scaling using AWS CDK is as follows: 1. Clone Repository and Install Dependencies: Clone the repository using the command `git clone https://github.com/aws-samples/sample-sagemaker-endpoint-autoscaling.git` and install dependencies with `uv sync`. 2. Configure Endpoint and Scaling Settings: Set the endpoint and scaling parameters in the `config/config.yaml` file. 3. Bootstrap AWS Environment: Bootstrap the AWS environment with the command `uv run cdk bootstrap`. 4. Deploy Stack: Deploy the stack using the command `uv run cdk deploy`.
In this process, the endpoint parameters are mapped to the CreateModel and CreateEndpointConfig APIs of SageMaker AI, while the scaling parameters are mapped to the RegisterScalableTarget and PutScalingPolicy APIs of Application Auto Scaling.
Conclusion
The method of managing automated scaling of SageMaker AI endpoints using AWS CDK effectively resolves various issues that may arise during endpoint configuration updates. This approach helps developers manage endpoints reliably without manual intervention. Additionally, it enhances efficiency in the deployment and management processes of machine learning models through integration with various AWS services.