0

Bit of an edge case ask, basically I'm trying to write a general purpose script that can be set up as a cronjob on AWS Sagemaker that will notify users if an instance is still active. If possible, I'd like to be able to call name of the AWS Sagemaker instance (different from the name of the active jupyter notebook) within the cronjob.

Haven't been able to find anything in the boto3 or sagemaker documentation specific to this. I know that the instance name is contained within the URL path, but similarly haven't found a way to reference back to this. I'm expecting this isn't an intended functionality so will probably just go ahead with setting values manually in a config file, but if anyone has any creative solutions I'd appreciate the input!

Ben Saunders
  • 929
  • 10
  • 20
  • 1
    This is more of a Jupyter question. This might help: https://stackoverflow.com/questions/12544056/how-do-i-get-the-current-ipython-jupyter-notebook-name – Julien Simon Jan 29 '20 at 10:17
  • Thanks @JulienSimon, unfortunately that post is about the name of the active Jupyter notebook whereas I'm looking for the name of the AWS Sagemaker notebook instance it's being hosted in. I'll update my question to be a bit clearer here – Ben Saunders Jan 29 '20 at 17:53

1 Answers1

3

Here's an example of how to get the instance name

def get_notebook_name():
    log_path = '/opt/ml/metadata/resource-metadata.json'
    with open(log_path, 'r') as logs:
        _logs = json.load(logs)
    return _logs['ResourceName']

From:

https://github.com/aws-samples/amazon-sagemaker-notebook-instance-lifecycle-config-samples/blob/62c44aa5e69f4266955476f24647b99d9b597aaf/scripts/auto-stop-idle/autostop.py#L79

user3363678
  • 178
  • 6
  • This isn't working for me. For a notebook named "garbage.ipynb" I get instance details about the image version: `datascience-1-0-ml-t3-medium-fbbacbd136ea35c00e5ce9203df8`. – jmatsen Mar 01 '23 at 00:12