Reputation: 73
My user regularly forget to shutdown their Sagemaker Studio instances. And I needed a script to list all running instances inside the sagemaker studio domains.
Upvotes: 1
Views: 1015
Reputation: 73
I hope its okay that I answer my own question here, but I made the following script for this problem and wanted to share so that other can find it too.
To run this your terminal needs to have an active aws session either via exporting the following variables before running the python script.
export AWS_ACCESS_KEY_ID=""
export AWS_SECRET_ACCESS_KEY=""
export AWS_SESSION_TOKEN=""
Or by logging in via AWS Cli: https://docs.aws.amazon.com/signin/latest/userguide/command-line-sign-in.html
import boto3
import csv
account_id = boto3.client("sts").get_caller_identity()["Account"]
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"]),
"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(f'sagemaker_running_instances_{account_id}.csv', 'w', newline='') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(running_instances)
The script will output the data to the console and create a file 'sagemaker_running_instances_{account_id}.csv' in the working dir.
Upvotes: 3