Skip to content

Instantly share code, notes, and snippets.

@tuliocasagrande
Last active May 4, 2022 13:21
Show Gist options
  • Save tuliocasagrande/61cd55ed72bb22bb50ea75bc5719419f to your computer and use it in GitHub Desktop.
Save tuliocasagrande/61cd55ed72bb22bb50ea75bc5719419f to your computer and use it in GitHub Desktop.

Install auto-shutdown on SageMaker Studio domain

This script installs the auto-shutdown Lifecycle Configuration to the SageMaker Domain, such that all users get the extension enabled by default.

The same installation can be made following the blog post Customize Amazon SageMaker Studio using Lifecycle Configurations, but I needed a "1-click" installation to run faster in multiple environments.

Prerequisites

  • Bash (or other compatible Unix shell)
  • AWS CLI
  • Programmatic access (access key ID, secret access key)
  • Permissions to:
    • Create SageMaker Studio Lifecycle Config
    • List SageMaker domains
    • Update SageMaker domain

Instructions

  1. Download the install_autoshutdown_sm_domain.sh shell script

  2. Execute ./install_autoshutdown_sm_domain.sh

#!/bin/bash
# -e Exit immediately if a command exits with a non-zero status.
# -u Treat unset variables as an error when substituting.
# -x Print commands and their arguments as they are executed.
set -eux
# Check other lifecycle config scripts here:
# https://github.com/aws-samples/sagemaker-studio-lifecycle-config-examples
wget -qO lcc-script.sh https://raw.githubusercontent.com/aws-samples/sagemaker-studio-lifecycle-config-examples/main/scripts/install-autoshutdown-extension/on-jupyter-server-start.sh
lcc_content=$(openssl base64 -A -in lcc-script.sh)
rm lcc-script.sh
lcc_arn=$(aws sagemaker create-studio-lifecycle-config \
--studio-lifecycle-config-name autoshutdown \
--studio-lifecycle-config-content $lcc_content \
--studio-lifecycle-config-app-type JupyterServer \
--query 'StudioLifecycleConfigArn' --output text)
domain_id=$(aws sagemaker list-domains --query 'Domains[0].DomainId' --output text)
aws sagemaker update-domain \
--domain-id $domain_id \
--default-user-settings '{
"JupyterServerAppSettings": {
"DefaultResourceSpec": {
"LifecycleConfigArn": "'$lcc_arn'",
"InstanceType": "system"
},
"LifecycleConfigArns": ["'$lcc_arn'"]
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment