You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何中断AWS SageMaker端点部署或设置2分钟部署超时?

AWS SageMaker部署终止与超时设置方案

问题场景

使用脚本模式部署AWS SageMaker端点时,自定义Python代码的低级错误会导致部署长时间重试,无法及时终止;同时本地调试兼容性问题耗时,倾向直接部署但需要控制超时时间。


一、终止正在运行的部署

控制台操作

  • 进入SageMaker控制台,打开「端点」页面
  • 找到状态为「创建中」或「更新中」的目标端点
  • 选中后点击「删除」,确认即可终止部署流程

代码主动终止

在另一个终端或独立脚本中调用SageMaker API终止:

import boto3

sagemaker_client = boto3.client('sagemaker')
# 删除端点以终止部署
sagemaker_client.delete_endpoint(EndpointName='opportunity-win-score')
# 可选:同步删除关联的端点配置
sagemaker_client.delete_endpoint_config(EndpointConfigName='opportunity-win-score')

二、设置2分钟部署超时

方案1:自定义脚本超时控制

修改部署脚本,通过线程+超时判断实现自动终止:

import threading
import boto3
from sagemaker.xgboost import XGBoostModel

def run_deployment():
    xgb_inference_model = XGBoostModel(
        model_data=model_url,
        role=role,
        entry_point="inference1.py",
        source_dir="./code",
        framework_version="1.7-1",
        sagemaker_session=sagemaker_session
    )
    # 设置wait=False,不阻塞主线程
    xgb_inference_model.deploy(
        initial_instance_count=1,
        endpoint_name='opportunity-win-score',
        instance_type="ml.t2.medium",
        wait=False
    )

# 启动部署线程
deploy_thread = threading.Thread(target=run_deployment)
deploy_thread.start()

# 等待120秒(2分钟)
deploy_thread.join(timeout=120)

# 线程仍存活则说明超时,终止部署
if deploy_thread.is_alive():
    sagemaker_client = boto3.client('sagemaker')
    sagemaker_client.delete_endpoint(EndpointName='opportunity-win-score')
    print("部署超时,已自动终止")

方案2:容器启动超时配置

通过SageMaker生命周期配置设置容器启动超时(部分框架容器适用):

  1. 创建Shell脚本startup-timeout.sh:
#!/bin/bash
# 设置容器启动超时为120秒
echo "export SAGEMAKER_CONTAINER_STARTUP_TIMEOUT=120" >> /etc/profile.d/sagemaker-env.sh
  1. 将脚本上传至S3,创建生命周期配置:
sagemaker_client = boto3.client('sagemaker')
sagemaker_client.create_notebook_instance_lifecycle_config(
    NotebookInstanceLifecycleConfigName='container-startup-timeout',
    OnCreate=[{'Content': {'S3Uri': 's3://your-bucket/path/startup-timeout.sh'}}]
)
  1. 部署时关联该配置:
predictor = xgb_inference_model.deploy(
    initial_instance_count=1,
    endpoint_name='opportunity-win-score',
    instance_type="ml.t2.medium",
    lifecycle_config_name='container-startup-timeout'
)

内容的提问来源于stack exchange,提问作者Stanislav Nevyazhsky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 01:07:32