🦔

SageMaker Studio:KernelGateway を停止するCLIスクリプト

2024/10/20に公開

Studio:KernelGateway でコストが発生してる!というアナタへ送る AWS CLI です。
起動中の SageMaker Studio のカーネルを探し、すべて停止します。

#!/bin/bash

# Function to get running apps
get_running_apps() {
    aws sagemaker list-apps --query "Apps[?Status=='InService']"
}

# Function to stop an app
stop_app() {
    local domain_id=$1
    local user_profile_name=$2
    local app_type=$3
    local app_name=$4

    echo "Stopping $app_type app: $app_name"
    aws sagemaker delete-app \
        --domain-id "$domain_id" \
        --user-profile-name "$user_profile_name" \
        --app-type "$app_type" \
        --app-name "$app_name"
}

# Main script
echo "Checking for running SageMaker Studio instances..."

running_apps=$(get_running_apps)

if [ -z "$running_apps" ] || [ "$running_apps" == "[]" ]; then
    echo "No running instances found. All instances are stopped."
else
    echo "Found running instance(s). Stopping them..."

    echo "$running_apps" | jq -c '.[]' | while read -r app; do
        domain_id=$(echo $app | jq -r '.DomainId')
        user_profile_name=$(echo $app | jq -r '.UserProfileName')
        app_type=$(echo $app | jq -r '.AppType')
        app_name=$(echo $app | jq -r '.AppName')

        stop_app "$domain_id" "$user_profile_name" "$app_type" "$app_name"
    done

    echo "All instances have been requested to stop. They may take a few minutes to fully terminate."
fi

echo "Script completed."

Discussion