AWS SageMaker支持按实例度量自动扩展功能。要使用此功能,您需要执行以下步骤:
import sagemaker
sagemaker_session = sagemaker.Session()
role = sagemaker.get_execution_role()
sagemaker_session.create_notebook_instance(
notebook_instance_name='my-notebook-instance',
instance_type='ml.t2.medium',
role_arn=role
)
import boto3
autoscaling_client = boto3.client('application-autoscaling')
autoscaling_client.register_scalable_target(
ServiceNamespace='sagemaker',
ResourceId='endpoint/your-endpoint-name/variant/your-variant-name',
ScalableDimension='sagemaker:variant:DesiredInstanceCount',
MinCapacity=1,
MaxCapacity=10
)
autoscaling_client.put_scaling_policy(
PolicyName='your-scaling-policy-name',
ServiceNamespace='sagemaker',
ResourceId='endpoint/your-endpoint-name/variant/your-variant-name',
ScalableDimension='sagemaker:variant:DesiredInstanceCount',
PolicyType='TargetTrackingScaling',
TargetTrackingScalingPolicyConfiguration={
'TargetValue': 1000,
'PredefinedMetricSpecification': {
'PredefinedMetricType': 'SageMakerVariantInvocationsPerInstance'
},
'ScaleInCooldown': 60,
'ScaleOutCooldown': 60,
'DisableScaleIn': False
}
)
import time
sagemaker_client = boto3.client('sagemaker')
sagemaker_client.update_endpoint_weights_and_capacities(
EndpointName='your-endpoint-name',
DesiredWeightsAndCapacities=[
{
'VariantName': 'your-variant-name',
'DesiredWeight': 1,
'DesiredInstanceCount': 1
}
]
)
# 等待自动扩展生效
time.sleep(300)
# 获取SageMaker实例状态
response = sagemaker_client.describe_endpoint(
EndpointName='your-endpoint-name'
)
# 打印实例状态
print(response['EndpointStatus'])
以上代码示例了如何使用AWS SDK for Python(Boto3)在SageMaker中创建自动扩展策略。您需要根据自己的实际情况进行适当的更改,如替换your-endpoint-name和your-variant-name等参数。请确保您已正确配置AWS访问凭证,并具有执行这些操作所需的权限。