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

如何监控Amazon SageMaker Notebook实例的启停次数及使用时长

监控Amazon SageMaker Notebook实例的启停次数与状态时长

方法1:利用CloudTrail + CloudWatch Logs Insights

CloudTrail会捕获SageMaker Notebook实例的所有API操作事件,结合CloudWatch Logs Insights可直接查询统计数据。

操作步骤:

  1. 确保已开启CloudTrail,并将日志交付到CloudWatch Logs日志组。
  2. 打开CloudWatch控制台,进入Logs Insights,选择对应CloudTrail日志组。
  3. 使用以下查询语句提取所需数据:

统计启停次数

fields @timestamp, eventName, requestParameters.notebookInstanceName
| filter eventName in ["StartNotebookInstance", "StopNotebookInstance"]
| stats count(*) as operationCount by eventName, requestParameters.notebookInstanceName
| sort requestParameters.notebookInstanceName, eventName asc

计算实例使用(InService)时长

该查询会匹配每一对启动/停止事件,计算单次使用时长并汇总总时长:

fields @timestamp, eventName, requestParameters.notebookInstanceName
| filter eventName in ["StartNotebookInstance", "StopNotebookInstance"]
| sort requestParameters.notebookInstanceName, @timestamp asc
| lag(@timestamp) as prevTimestamp, lag(eventName) as prevEvent by requestParameters.notebookInstanceName
| filter eventName == "StopNotebookInstance" and prevEvent == "StartNotebookInstance"
| parse duration(@timestamp - prevTimestamp) as usageDuration
| stats sum(@timestamp - prevTimestamp) as totalUsageSeconds, count(*) as usageTimes by requestParameters.notebookInstanceName
| sort requestParameters.notebookInstanceName asc

注:如果实例当前处于运行状态,需单独补充计算从最后一次启动到当前的时长

方法2:利用CloudWatch Metrics

SageMaker提供原生指标跟踪实例状态,可通过CloudWatch Metrics直接计算状态时长。

操作步骤:

  1. 打开CloudWatch控制台,进入Metrics,选择AWS/SageMaker命名空间。
  2. 筛选NotebookInstanceStatus指标,按实例名称维度拆分。该指标值为1表示实例处于InService状态,0表示其他状态(如Stopped、Pending等)。
  3. 使用Metric Math计算总使用时长:
    • 公式示例:SUM(METRICS('AWS/SageMaker', 'NotebookInstanceStatus', 'NotebookInstanceName', 'your-instance-name')) * 60
    • 说明:指标每分钟采样一次,乘以60将采样次数转换为秒数,得到总运行时长。
  4. 如需统计启停次数,可结合状态变化的告警事件,或通过指标的Datapoint数量间接统计(每次状态切换会产生新的数据点)。

方法3:自定义跟踪(Lambda + EventBridge + DynamoDB)

如果需要更灵活的自定义统计和持久化存储,可通过事件驱动方式实现:

操作步骤:

  1. 创建EventBridge规则,匹配SageMaker实例状态变化事件:
    • 事件模式:
      {
        "source": ["aws.sagemaker"],
        "detail-type": ["SageMaker Notebook Instance State Change"],
        "detail": {
          "CurrentStatus": ["InService", "Stopped"]
        }
      }
      
  2. 创建Lambda函数,作为EventBridge规则的目标:
    • 函数逻辑:接收状态变化事件,提取实例名称、当前状态、事件时间,写入DynamoDB表(表结构示例:instanceName、status、timestamp)。
  3. 分析DynamoDB数据:
    • 通过查询同一实例的状态记录,匹配InService和Stopped的时间对,计算单次使用时长并汇总。
    • 统计InService状态的记录次数即为启动次数,Stopped次数即为停止次数。

内容的提问来源于stack exchange,提问作者Ramya T

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 18:05:26