Search code examples
amazon-web-servicesamazon-sagemaker

AWS Sagemaker Life Cycle Configuration - AutoStop


I have created 4 instances in my AWS sagemaker NOTEBOOKS tab.

I want to create a life cycle configuration where the instance should stop every day at 9:00 PM.

I have seen some examples but it is with IDLE TIME but not with the specific time

#!/bin/bash
set -e

# PARAMETERS
IDLE_TIME=3600

echo "Fetching the autostop script"
wget -O autostop.py https://raw.githubusercontent.com/mariokostelac/sagemaker-setup/master/scripts/auto-stop-idle/autostop.py

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

echo "Changing cloudwatch configuration"
curl https://raw.githubusercontent.com/mariokostelac/sagemaker-setup/master/scripts/publish-logs-to-cloudwatch/on-start.sh | sudo bash -s auto-stop-idle /home/ec2-user/SageMaker/auto-stop-idle.log

Can anyone help me out on this one?


Solution

  • Change the crontab syntax to 0 21 * * * shutdown.py

    Then create a shutdown.py which is reduced version of the autostop.py and contains mainly:

    ...
    print('Closing notebook')
    client = boto3.client('sagemaker')
    client.stop_notebook_instance(NotebookInstanceName=get_notebook_name())
    

    BTW: triggering shutdown now directly from the crontab command didn't work for me, therefore calling the SageMaker API instead.