0

我正在尝试在我的本地笔记本电脑上从https://github.com/aws-samples/aws-deepracer-workshops/blob/master/log-analysis/DeepRacer%20Log%20Analysis.ipynb运行 Deepracer 日志分析工具。但是,在尝试运行步骤 [5]“创建 IAM 角色”时出现以下错误。

try:
    sagemaker_role = sagemaker.get_execution_role()
except:
    sagemaker_role = get_execution_role('sagemaker')

print("Using Sagemaker IAM role arn: \n{}".format(sagemaker_role))

Couldn't call 'get_role' to get Role ARN from role name arn:aws:iam::26********:root to get Role path.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-3bea8175b8c7> in <module>
      1 try:
----> 2     sagemaker_role = sagemaker.get_execution_role()
      3 except:

/opt/conda/lib/python3.7/site-packages/sagemaker/session.py in get_execution_role(sagemaker_session)
   3302     )
-> 3303     raise ValueError(message.format(arn))
   3304 

ValueError: The current AWS identity is not a role: arn:aws:iam::26********:root, therefore it cannot be used as a SageMaker execution role

During handling of the above exception, another exception occurred:

NameError                                 Traceback (most recent call last)
<ipython-input-5-3bea8175b8c7> in <module>
      2     sagemaker_role = sagemaker.get_execution_role()
      3 except:
----> 4     sagemaker_role = get_execution_role('sagemaker')
      5 
      6 print("Using Sagemaker IAM role arn: \n{}".format(sagemaker_role))

NameError: name 'get_execution_role' is not defined

有谁知道要执行以上代码而没有错误需要做什么?

4

1 回答 1

1

AWS 支持推荐以下解决方案:

在本地执行代码时,这似乎是一个已知问题,如以下 Github 问题 [3] 中所述。该问题 [3] 中还定义了解决该问题的解决方法,可以使用以下链接进行参考:aws/sagemaker-python-sdk#300(评论)

上述链接中给出的解决方法中的步骤是:

  1. 登录 AWS 控制台 -> IAM -> 角色 -> 创建角色

  2. 创建 IAM 角色并选择“SageMaker”服务

  3. 授予角色“AmazonSageMakerFullAccess”权限

  4. 查看并创建角色

  5. 接下来,还将“AWSRoboMakerFullAccess”权限策略附加到上述创建的角色(如 Github notebook [1] 中所要求的)。

  6. 然后需要修改原始代码以在本地机器上执行代码时直接获取 IAM 角色。下面给出了要使用的代码片段:

try:
   sagemaker_role = sagemaker.get_execution_role()
 except ValueError:
   iam = boto3.client('iam')
   sagemaker_role = iam.get_role(RoleName='<sagemaker-IAM-role-name>')['Role']['Arn']

在上面的代码段中,将“”文本替换为在步骤 4 中创建的 IAM 角色名称。

参考:

[1] https://github.com/aws-samples/aws-deepracer-workshops/blob/master/log-analysis/DeepRacer%20Log%20Analysis.ipynb

[2] https://docs.aws.amazon.com/sagemaker/latest/dg/automatic-model-tuning-ex-role.html

[3] aws/sagemaker-python-sdk#300

于 2020-04-01T17:00:37.230 回答