Deepracer log_analysis 工具 - sagemaker 角色错误

Deepracer log_analysis tool - sagemaker role errors

我正在尝试在我的本地笔记本电脑上 运行 来自 https://github.com/aws-samples/aws-deepracer-workshops/blob/master/log-analysis/DeepRacer%20Log%20Analysis.ipynb 的 Deepracer 日志分析工具。但是,我在尝试 运行 步骤 [5] "Create an IAM role" 时遇到以下错误。

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

有谁知道需要做什么才能不出错地执行上面的代码?

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

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

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

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

  2. 创建 IAM 角色和 select "SageMaker" 服务

  3. 赋予角色"AmazonSageMakerFullAccess"权限

  4. 查看并创建角色

  5. 接下来,还要将"AWSRoboMakerFullAccess"权限策略附加到上面创建的角色(根据Github笔记本[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