Recently one of the AWS accounts that I manage showed a spike in the bill. On detailed analysis, I found so many active SageMaker instances in the account.

I thought of exporting the details as a csv file and sharing it with the team to understand the usage and delete the unwanted instances. I have developed the following program to generate a csv file with the basic details of all the SageMaker instances present in the AWS account.

The program is very simple. I have used python boto3 to get the complete details of AWS SageMaker instances.

import csv
import boto3
client = boto3.client('sagemaker', region_name='us-east-1')
response = client.list_notebook_instances(MaxResults=100)
notebooks = response['NotebookInstances']
print("Total Number of Notebook Instances —–>", len(notebooks))
notebook_list = []
for notebook in notebooks:
notebook_dict = dict()
notebook_dict['NotebookInstanceName'] = notebook['NotebookInstanceName']
notebook_dict['NotebookInstanceArn'] = notebook['NotebookInstanceArn']
notebook_dict['NotebookInstanceStatus'] = notebook['NotebookInstanceStatus']
notebook_dict['InstanceType'] = notebook['InstanceType']
notebook_list.append(notebook_dict)
print(notebook_list)
with open('notebook_instances.csv', 'w', encoding='utf8', newline='') as output_file:
fc = csv.DictWriter(output_file,
fieldnames=notebook_list[0].keys())
fc.writeheader()
fc.writerows(notebook_list)