aws-samples / sagemaker-studio-lifecycle-config-examples

MIT No Attribution
78 stars 51 forks source link

Lifecycle Configurations in Shared Spaces #29

Open jracabado opened 1 year ago

jracabado commented 1 year ago

Hi,

I've configured my user profile with a default KernelGateway lifecycle configuration and I was expecting it to run when launching a notebook from a Shared Space that uses that user profile.

I can see it runs and it's available when launching a Studio instance directly from the user-profile but when launching a Shared Space the "Start-up scripts" dropdown is empty.

Is this by design?

athewsey commented 1 year ago

Hi, the settings (including LCC) are separate for shared spaces vs user profiles so I believe this is by design.

Currently in the AWS Console I see the Domain > Environment tab has a "Lifecycle configurations for personal Studio apps" section where personal LCCs can be configured across the domain; and from each user profile's detail page we can click Edit to configure LCCs for the specific user. However, I don't see any equivalent for setting LCCs for specific spaces or across all shared spaces on the console.

Therefore I believe you'll need to follow a similar process to what we had to for personal user LCCs before the console UI became available: Use the UpdateDomain and/or UpdateSpace APIs to include your LCC in the LifecycleConfigArns of the domain (DefaultSpaceSettings) or space (SpaceSettings) - and configure it to run by default by setting it under DefaultResourceSpec.

Because this Update* process involves merging the old and new configurations (to avoid losing any settings), I usually preferred to do it in Python rather than shell script... With a process something like (untested example for all spaces at domain level):

import boto3
smclient = sess.client("sagemaker")

DOMAIN_ID = ...
SCRIPT_ARN = ...

domain_desc = smclient.describe_domain(DomainId=DOMAIN_ID)
default_settings = domain_desc["DefaultSpaceSettings"]

# The sub-objects may not exist if no LCCs were set up previously:
if not default_settings.get("JupyterServerAppSettings"):
    default_settings["JupyterServerAppSettings"] = {}
if not default_settings["JupyterServerAppSettings"].get("LifecycleConfigArns"):
    default_settings["JupyterServerAppSettings"]["LifecycleConfigArns"] = []

default_scripts = default_settings["JupyterServerAppSettings"]["LifecycleConfigArns"]
if SCRIPT_ARN not in default_scripts:
    print(f"Adding script:\n{SCRIPT_ARN}")
    default_scripts.append(SCRIPT_ARN)
    smclient.update_domain(
        DomainId=DOMAIN_ID,
        DefaultUserSettings=default_settings,
    )
else:
    print("Script already default on domain:\n{SCRIPT_ARN}")

I was previously thinking it might be a good idea to get a proper tested example of this kind of helper script in this repository... But hadn't prioritised it because the Console UI made it easier. Seems like it might be worth revisiting if we still don't have a UI experience for attaching Spaces scripts yet