Skip to content

how to show Instance status for users inside the SageMaker Studio domain?

1

Is there a way to check with the AWS SDK or CLI whether instances are created at a specific time for users inside the SageMaker Studio domain?

3 Answers
2

I made a script for myself in python to output running instances, maybe it is helpful for your usecase.

import boto3
import csv

client = boto3.client('sagemaker')


response_apps = client.list_apps(
    #NextToken='string',
    MaxResults=10,
    SortOrder='Ascending',
    SortBy='CreationTime',
)

running_instances = []
while True:
    for app in response_apps["Apps"]:
        if app["AppType"] != "JupyterServer" and app["Status"] != "Deleted":
            response = client.describe_app(
                DomainId=app["DomainId"],
                UserProfileName=app["UserProfileName"],
                AppType=app["AppType"],
                AppName=app["AppName"]
            )
            #print(response)
            running_instances.append(
                {
                    "AppArn": response["AppArn"],
                    "DomainId": response["DomainId"],
                    "UserProfileName": response["UserProfileName"],
                    "Status": response["Status"],
                    "InstanceType": response["ResourceSpec"]["InstanceType"],
                    "CreationTime": str(response["CreationTime"]),
                    "LastUserActivityTimestamp": str(response["LastUserActivityTimestamp"]),
                    "SageMakerImageArn": response["ResourceSpec"]["SageMakerImageArn"],

                }
            )
    if "NextToken" in response_apps:
        response_apps = client.list_apps(
            NextToken=response_apps["NextToken"],
            MaxResults=10,
            SortOrder='Ascending',
            SortBy='CreationTime'
        )
    else:
        break

print(running_instances)
keys = running_instances[0].keys()

with open('running_instances.csv', 'w', newline='') as output_file:
    dict_writer = csv.DictWriter(output_file, keys)
    dict_writer.writeheader()
    dict_writer.writerows(running_instances)
answered 2 years ago
0

You cannot view instance status since these are managed by SageMaker service. You can, however, use the list-apps and describe-app CLI calls to see the apps in service for the user, and work backwards to find the instances that are being used by the user.

AWS
answered 2 years ago
0

I have updated my script because it was breaking because of the introduction of Jupyther Lab instances.

import boto3
import csv

account_id =  boto3.client("sts").get_caller_identity()["Account"]
client = boto3.client('sagemaker')


response_apps = client.list_apps(
    MaxResults=10,
    SortOrder='Ascending',
    SortBy='CreationTime',
)

running_instances = []
while True:
    for app in response_apps["Apps"]:
        if app["AppType"] == "JupyterLab" and app["Status"] != "Deleted":
            response = client.describe_app(
                DomainId=app["DomainId"],
                SpaceName=app["SpaceName"],
                AppType=app["AppType"],
                AppName=app["AppName"]
            )
        elif app["AppType"] == "KernelGateway" and app["Status"] != "Deleted":
            response = client.describe_app(
                DomainId=app["DomainId"],
                UserProfileName=app["UserProfileName"],
                AppType=app["AppType"],
                AppName=app["AppName"]
            )
        else:
            continue
        #print(response)
        resource_spec = response.get("ResourceSpec", {})
        running_instances.append(
            {
                "AppArn": response.get("AppArn", "N/A"),
                "DomainId": response.get("DomainId", "N/A"),
                "SpaceName": response.get("SpaceName", "N/A"),
                "UserProfileName": response.get("UserProfileName", "N/A"),
                "Status": response.get("Status", "N/A"),
                "InstanceType": resource_spec.get("InstanceType", "N/A"),
                "CreationTime": str(response.get("CreationTime", "N/A")),
                "SageMakerImageArn": resource_spec.get("SageMakerImageArn", "N/A"),
            }
        )
    if "NextToken" in response_apps:
        response_apps = client.list_apps(
            NextToken=response_apps["NextToken"],
            MaxResults=10,
            SortOrder='Ascending',
            SortBy='CreationTime'
        )
    else:
        break

#print(running_instances)
if(len(running_instances)>0):
    keys = running_instances[0].keys()

    with open(f'aws-sagemaker-studio-instances_{account_id}.csv', 'w', newline='') as output_file:
        dict_writer = csv.DictWriter(output_file, keys)
        dict_writer.writeheader()
        dict_writer.writerows(running_instances)
answered 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.