How to include lifecycle configuration in Sagemaker studio's user notebook I want to use lifecycle configuration in Sagemaker studio so that on start of user's notebook it runs the given lifecycle configuration.
My lifecycle configuration will have shell script which will launch cronjob having python script to send attached notebook's running duration.
#!/bin/bash
set -e

# PARAMETERS
IDLE_TIME=120

echo &quot;Fetching the autostop script&quot;
aws s3 cp s3://testing-west2/duration-check.py .
aws s3 cp s3://testing-west2/on-start.sh .

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

echo &quot;Changing cloudwatch configuration&quot;
cat $DIR/on-start.sh | sudo bash -s auto-stop-idle /home/ec2-user/SageMaker/auto-stop-idle.log

Referred lifecycle configuration from here:
https://github.com/aws-samples/amazon-sagemaker-notebook-instance-lifecycle-config-samples/tree/master/scripts/auto-stop-idle
Original source
You can follow this tutorial:
https://modelpredict.com/sagemaker-stop-your-instances-when-idle/
Summary
Basically, you have to go to the notebook instance where you want to apply the Lifecycle Configuration, stop it, and go to: Additional configuration &gt; Lifecycle Configuration &gt; [Select your script].
After this, you have to configure the proper permissions for the Lifecycle Configuration script to stop your notebook instance. To do so, create a new policy, and paste the following code:
{
  &quot;Version&quot;: &quot;2012-10-17&quot;,
  &quot;Statement&quot;: [
      {
          &quot;Sid&quot;: &quot;VisualEditor0&quot;,
          &quot;Effect&quot;: &quot;Allow&quot;,
          &quot;Action&quot;: [
              &quot;sagemaker:StopNotebookInstance&quot;,
              &quot;sagemaker:DescribeNotebookInstance&quot;
          ],
          &quot;Resource&quot;: &quot;*&quot;
      }
  ]
}

After this, attach it to your Notebook instance and it's done!