Automatically "stop" Sagemaker notebook instance after inactivity?

You could also use CloudWatch + Lambda to monitor Sagemaker and stop when your utilization hits a minimum. Here is a list of what's available in CW for SM: https://docs.aws.amazon.com/sagemaker/latest/dg/monitoring-cloudwatch.html.

For example, you could set a CW alarm to trigger when CPU utilization falls below ~5% for 30 minutes and have that trigger a Lambda which would shut down the notebook.


Unfortunately, automatically stopping the Notebook Instance when there is no activity is not possible in SageMaker today. To avoid leaving them overnight, you can write a cron job to check if there's any running Notebook Instance at night and stop them if needed.


You can use Lifecycle configurations to set up an automatic job that will stop your instance after inactivity.

There's a GitHub repository which has samples that you can use. In the repository, there's a auto-stop-idle script which will shutdown your instance once it's idle for more than 1 hour.

What you need to do is

  1. to create a Lifecycle configuration using the script and
  2. associate the configuration with the instance. You can do this when you edit or create a Notebook instance.

If you think 1 hour is too long you can tweak the script. This line has the value.


After we've burned quite a lot of money by forgetting to turn off these machines, I've decided to create a script. It's based on AWS' script, but provides an explanation why the machine was or was not killed. It's pretty lightweight because it does not use any additional infrastructure like Lambda.

Here is the script and the guide on installing it! It's just a simple lifecycle configuration!