A thousand times the mysteries unfold like galaxies in my head.

Follow me

Disabling automatic kernel starts in SageMaker Studio

The default behaviour in SageMaker Studio is for it to start a kernel whenever you open an existing notebook that has been assigned a kernel before.

In my discussions with customers, I have been asked several times how one could modify this behaviour. Automatic kernel starts cannot be cancelled as the kernel is being spun up, hence the SageMaker Studio user has to wait for the kernel to be provisioned. This can be time consuming and cost ineffective for very big instance types.

In the following post, I will explain how this behaviour can be tweaked.

Diving deep into the Jupyter Lab configuration for a solution

Looking at the Jupyter Lab 2.0 API documentation, there should be a notebook_starts_kernel flag that can be set to false in order to disable automatic starts. However, SageMaker Studio uses version 3.0 of Jupyter Lab.

According to this GitHub issue, the configuration flag is now in the page config. This change has been merged into JupyterLab 3.3.x in March 2022.

The use of page_config.json for setting options is documented here, so let us try implementing it on a SageMaker Studio environment.

Testing the solution manually

1. Log into your SageMaker Studio user and start a new Terminal session:

2. Check your Jupyter Lab version. As mentioned, this will only work for version 3.3.x and up. Below is my terminal output as an example:

You can see that I have version 3.6.0a1 of Jupyter Lab. If you find yourself stuck on an old version of SageMaker Studio with Jupyter Lab version 1.x.x, please upgrade to version 3.x.x by following this blog post.

3. Check where Jupyter Lab reads its configuration from:

The configuration paths are listed in order of priority. Which means that Jupyter Lab will first look for configuration files in the hidden .jupyter folder in the home directory of the SageMaker Studio user. The final configuration is a merge of all configuration files encountered in the paths.

4. Create the page_config.json file:

5. Shut down the Jupyter Lab Server by going to File>Shut Down. Then launch the server again.

SageMaker Studio would now ask for a kernel every time you open a notebook, instead of automatically starting the kernel:

Automating the solution

Because the configuration resides on the EFS shared drive, it will be picked up every time you stop and restart the Jupyter Lab server.

In case you want to have this applied automatically to all users in a SageMaker domain, you can also create a lifecycle configuration script as shown below:

1. On your SageMaker domain page, access the Lifecycle configurations tab and click Attach.

2. Create a new configuration.

For Configuration Type choose Jupyter server app. Give the configuration a suitable name and then copy-paste the script in the Scripts section.

Here is the script for easy copy-pasting:

The script is also available on GitHub. I have made a pull request to the official aws-samples repository for lifecycle config examples:

3. Select the created lifecycle configuration script and click Set as default to have it run on the creation of new users.

Note: the lifecycle configuration can also be enabled on individual users, instead of the whole domain.

Reverting back to the original behaviour

If you want to revert back to the original behaviour of the notebooks, you should do the following:

  • if you have enabled the automation, detach the lifecycle configuration script from the SageMaker Studio domain
  • delete the configuration file from the home directories of the existing users and restart their Jupyter Lab servers

In this post you have seen how to disable automatic kernel starts on notebook opening inside SageMaker Studio. I have shown both a manual and automated way to do it, as well as explained how to revert back to the original behaviour. Let me know in the comments section if this solution was useful to you.

No Comments

Post A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.