Using sagemaker in jupyter notebook

Hi everyone,

I am trying to train a huggingface model on jupyternotebook on my local machine.

can i get some advice on: how do i pass my IAM role access key ?
i have installed sagemaker on my jupyternotebook, however, how do i connect to my AWS account? i found a line of code:

Blockquote

gets role for executing training job

iam_client = boto3.client(‘iam’)
role = iam_client.get_role(RoleName=‘{IAM_ROLE_WITH_SAGEMAKER_PERMISSIONS}’)[‘Role’][‘Arn’]
hyperparameters = {
‘model_name_or_path’:‘finiteautomata/beto-sentiment-analysis’,
‘output_dir’:‘/opt/ml/model’
# add your remaining hyperparameters
# more info here https://github.com/huggingface/transformers/tree/v4.6.1/examples/pytorch/text-classification
}

Blockquote

i have set up a new user in my AWS account and have my access keys. just don’t understand how do it pass it on to my local machine?

everytime i only get errors like:

Blockquote
ValueError: Must setup local AWS configuration with a region supported by SageMaker.

NoCredentialsError: Unable to locate credentials

Blockquote

please advise!

thank you in advance.
meng

Hey @tumon,

You need to create an IAM Role for sagemaker with permission to access all required resources. You can find instructions on how to do this here: SageMaker Roles - Amazon SageMaker

To create a new role

  1. Open the IAM console at https://console.aws.amazon.com/iam/.
  2. Select Roles and then select Create role .
  3. Select SageMaker .
  4. Select Next: Permissions .
  5. The IAM managed policy, AmazonSageMakerFullAccess is automatically attached to this role. To see the permissions included in this policy, select the sideways arrow next to the policy name. Select Next: Tags .
  6. (Optional) Add tags and select Next: Review .
  7. Give the role a name in the text field under Role name and select Create role .
  8. On the Roles section of the IAM console, select the role you just created. If needed, use the text box to search for the role using the role name you entered in step 7.
  9. On the role summary page, make note of the ARN.

Hi @philschmid ,

Thank you so much for the quick reply!

I have had an AWS IAM role and have my unique ARN. My following problem is in my jupyternotebook, no matter how i apply my ARN to “{IAM_ROLE_WITH_SAGEMAKER_PERMISSIONS}” into the below code.

iam_client = boto3.client(‘iam’)
role = iam_client.get_role(RoleName=’{IAM_ROLE_WITH_SAGEMAKER_PERMISSIONS}’)[‘Role’][‘Arn’]

i always get an error :

Blockquote
NoCredentialsError: Unable to locate credentials

could you please kindly advise what might i be missing here?

thank you!

meng

Hey,

Yes let me explain this, it is a bit complicated to get started. So the IAM ROLE, which you have created will be used inside the SageMaker Training Job/Inference. Meaning this ROLE is used to, e.g. download your data from s3 or is needed to start the underlying machine.

The error NoCredentialsError: Unable to locate credentials is shown when you don’t have credentials configured on your machine. You need to run aws configure so set up IAM Credentials (User) on your machine. You need these credentials to start your training job.

Hi @philschmid

Thank you so much for my silly question!

I configured aws on my machine and now it worked!!

meng

1 Like

So I had the same problem, but I didn’t want to use the secret access key to connect to AWS account, I was using SSO login. My goal was to deploy huggingface model from my local machine using HuggingFaceModel interface. The solution:

sg_role = "{SAGEMAKER-ROLE}"
s3_location = "{ARTIFACT-S3-URI}"
sg_session = sagemaker.Session(boto_session=boto3.session.Session(profile_name="{PROFILE-NAME"}))

hg_model = HuggingFaceModel(
    model_data=s3_location,
    role=sg_role,
    transformers_version=...,
    pytorch_version=...,
    py_version=...,
    sagemaker_session=sg_session
)

predictor = hg_model.deploy(
    initial_instance_count=1,
    instance_type="ml.g4dn.xlarge",
    )