如何在 Sagemaker Studio 的用户笔记本中包含生命周期配置

数据挖掘 数据科学模型 aws 贤者
2022-03-03 22:34:37

我想在 Sagemaker Studio 中使用生命周期配置,以便在用户笔记本启动时运行给定的生命周期配置。

我的生命周期配置将具有 shell 脚本,该脚本将启动具有 python 脚本的 cronjob 以发送附加笔记本的运行持续时间。

#!/bin/bash
set -e

# PARAMETERS
IDLE_TIME=120

echo "Fetching the autostop script"
aws s3 cp s3://testing-west2/duration-check.py .
aws s3 cp s3://testing-west2/on-start.sh .

echo "Starting the SageMaker autostop script in cron"
(crontab -l 2>/dev/null; echo "*/1 * * * * /bin/bash -c '/usr/bin/python3 $DIR/duration-check.py --time ${IDLE_TIME} | tee -a /home/ec2-user/SageMaker/auto-stop-idle.log'") | crontab -

echo "Changing cloudwatch configuration"
cat $DIR/on-start.sh | sudo bash -s auto-stop-idle /home/ec2-user/SageMaker/auto-stop-idle.log

从这里引用生命周期配置: https ://github.com/aws-samples/amazon-sagemaker-notebook-instance-lifecycle-config-samples/tree/master/scripts/auto-stop-idle

1个回答

原始来源

您可以按照本教程进行操作:

https://modelpredict.com/sagemaker-stop-your-instances-when-idle/

概括

基本上,您必须转到要应用生命周期配置的笔记本实例,将其停止,然后转到:附加配置 > 生命周期配置 > [选择您的脚本]。

在此之后,您必须为生命周期配置脚本配置适当的权限以停止您的笔记本实例。为此,请创建一个新策略,然后粘贴以下代码:

{
  "Version": "2012-10-17",
  "Statement": [
      {
          "Sid": "VisualEditor0",
          "Effect": "Allow",
          "Action": [
              "sagemaker:StopNotebookInstance",
              "sagemaker:DescribeNotebookInstance"
          ],
          "Resource": "*"
      }
  ]
}

在此之后,将其附加到您的 Notebook 实例就完成了!