# Using sagemaker in jupyter notebook

**URL:** <https://discuss.huggingface.co/t/using-sagemaker-in-jupyter-notebook/9970>\
**Category:** Amazon SageMaker\
**Created:** [September 14, 2021, 7:55am UTC](https://discuss.huggingface.co/t/using-sagemaker-in-jupyter-notebook/9970 "2021-09-14T07:55:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tumon](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@tumon](https://discuss.huggingface.co/u/tumon)\
**Post date:** [September 14, 2021, 7:55am UTC](https://discuss.huggingface.co/t/using-sagemaker-in-jupyter-notebook/9970/1 "2021-09-14T07:55:49Z")

</div>

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 [transformers/examples/pytorch/text-classification at v4.6.1 · huggingface/transformers · GitHub](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

---

<div class="post-metadata">

**Author:** ![philschmid](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/philschmid/32/38022_2.png) [@philschmid](https://discuss.huggingface.co/u/philschmid)\
**Post date:** [September 14, 2021, 12:40pm UTC](https://discuss.huggingface.co/t/using-sagemaker-in-jupyter-notebook/9970/2 "2021-09-14T12:40:47Z")

</div>

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](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-roles.html)

> **To create a new role**
> 
> 1. Open the IAM console at [https://console.aws.amazon.com/iam/](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.

---

<div class="post-metadata">

**Author:** ![tumon](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@tumon](https://discuss.huggingface.co/u/tumon)\
**Post date:** [September 14, 2021, 5:32pm UTC](https://discuss.huggingface.co/t/using-sagemaker-in-jupyter-notebook/9970/3 "2021-09-14T17:32:44Z")

</div>

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.

```auto
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

---

<div class="post-metadata">

**Author:** ![philschmid](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/philschmid/32/38022_2.png) [@philschmid](https://discuss.huggingface.co/u/philschmid)\
**Post date:** [September 15, 2021, 7:12am UTC](https://discuss.huggingface.co/t/using-sagemaker-in-jupyter-notebook/9970/4 "2021-09-15T07:12:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tumon](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@tumon](https://discuss.huggingface.co/u/tumon)\
**Post date:** [September 15, 2021, 9:23am UTC](https://discuss.huggingface.co/t/using-sagemaker-in-jupyter-notebook/9970/5 "2021-09-15T09:23:59Z")

</div>

Hi @philschmid

Thank you so much for my silly question!

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

meng

---

<div class="post-metadata">

**Author:** ![Khaliladib](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/khaliladib/32/18445_2.png) [@Khaliladib](https://discuss.huggingface.co/u/Khaliladib)\
**Post date:** [August 10, 2023, 7:41pm UTC](https://discuss.huggingface.co/t/using-sagemaker-in-jupyter-notebook/9970/6 "2023-08-10T19:41:10Z")

</div>

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:

```auto
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",
    )

```
