Search code examples
jupyter-notebookinstanceshutdownamazon-sagemaker

Is there a Jupyter code that I can use "to stop" a Notebook Instances on Sagemaker, after running any code?


I use a Jupyter Notebook instance on Sagemaker to run a code that took around 3 hours to complete. Since I pay for hour use, I would like to automatically "Close and Halt" the Notebook as well as stop the "Notebook instance" after running that code. Is this possible?


Solution

  • Jupyter Notebook are predominantly designed for exploration and development. If you want to launch long-running or scheduled jobs on ephemeral hardware, it will be a much better experience to use the training API, such as the create_training_job in boto3 or the estimator.fit() of the Python SDK. The code passed to training jobs can be completely arbitrary - not necessarily ML code - so whatever you write in jupyter could likely be scheduled and ran in those training jobs. See the random forest sklearn demo here for an example. That being said, if you still want to programmatically shut down a SageMaker notebook instance, you can use that boto3 call:

    import boto3
    
    sm = boto3.client('sagemaker')
    sm.stop_notebook_instance(NotebookInstanceName='string')