Home > OS >  How to automatically stop Sagemaker notebook instances if it is idle?
How to automatically stop Sagemaker notebook instances if it is idle?

Time:05-13

I have been looking for a script to automatically close Sagemaker Notebook Instances that have been forgotten to be closed or that are idle. A few scripts I found don't work very well (eg: link , it is only checking if ipynb file is live, Im not using .ipynb, or taking the last updated info which never changes until you shut down or open the instance) Is there a resource or script you can recommend?

CodePudding user response:

You will find several options that works in most of the cases for SageMaker Studio Notebooks at the following link: https://github.com/aws-samples/sagemaker-studio-auto-shutdown-extension

If still does not work for you, please provide more information. Thanks

CodePudding user response:

You can use the following script to find idle instances. You can modify the script to stop the instance if idle for more than 5 minutes or have a cron job to stop the instance.

import boto3

last_modified_threshold = 5 * 60
sm_client = boto3.client('sagemaker')
response = sm_client.list_notebook_instances()

for item in response['NotebookInstances']:
    last_modified_seconds = item['LastModifiedTime'].timestamp()
    last_modified_minutes = last_modified_seconds/60
    print(last_modified_minutes)
    if last_modified_minutes > last_modified_threshold:
        print('Notebook {0} has been idle for more than {1} minutes'.format(item['NotebookInstanceName'], last_modified_threshold/60))
  • Related