0

I have set up a sagemaker domain and added some user profile. I log into the sagemaker studio, select a user profile and it starts the studio for me. i open a new notebook. inside the notebook, i install sagemaker and import additional libraries. how can use sagemaker api to print the name of the user/userprofile , that is currently using the studio? in the example below, i have created a userprofile by the name of "chuck". and i'm logged into studio via that profile , but i would like go get and print that name in the notebook, i'm running.

!pip install --upgrade sagemaker
!pip install torch==1.9.1
!pip install torchvision==0.10.1
import boto3
import numpy as np
import os
import sagemaker
import torch
import torchvision
from sagemaker.pytorch.model import PyTorchModel

cloudformation code to create sagemaker domain and userprofiles

StudioDomain:
    Type: Custom::StudioDomain
    Properties:
      ServiceToken: !GetAtt StudioDomainFunction.Arn
      VPC: !Ref VPCId
      SubnetIds: !Ref SubnetIds
      DomainName: "MyDomainName"
      DefaultUserSettings:
        ExecutionRole: !GetAtt SageMakerExecutionRole.Arn

UserProfile:
    Type: Custom::UserProfile
    Properties:
      ServiceToken: !GetAtt UserProfileFunction.Arn
      DomainId: !GetAtt StudioDomain.DomainId
      UserProfileName: 'chuck'
      UserSettings:
        ExecutionRole: !GetAtt SageMakerExecutionRole.Arn
haju
  • 95
  • 6

1 Answers1

0

We can retrieve user profile name from the studio notebook using the below code:

with open("/opt/ml/metadata/resource-metadata.json", "r") as f:
    app_metadata = json.loads(f.read())
    print(json.dumps(app_metadata, indent=2))
    sm_user_profile_name = app_metadata["UserProfileName"]

File content looks something like:

{
  "AppType": "KernelGateway",
  "DomainId": "d-a0987654321z",
  "UserProfileName": "myuserprofile",
  "ResourceArn": "arn:aws:sagemaker:",
  "ResourceName": "datascience-1-0-ml-t3-medium-81187bd2ae843298bc309cb256a3",
  "AppImageVersion": ""
}
Arun Lokanatha
  • 290
  • 1
  • 4