

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 如何使用 SageMaker AI 執行角色
<a name="sagemaker-roles"></a>

Amazon SageMaker AI 會使用其他服務代表您執行操作 AWS 。您必須授予 SageMaker AI 許可，才能使用這些服務及其所處理的資源。您可以使用 AWS Identity and Access Management (IAM) 執行角色授予 SageMaker AI 這些許可。如需 IAM 角色的更多資訊，請參閱 [IAM 角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles.html)。

若要建立和使用執行角色，您可以使用下列程序。

## 建立執行角色
<a name="sagemaker-roles-create-execution-role"></a>

使用以下程序，建立 IAM 受管政策`AmazonSageMakerFullAccess`的執行角色。如果您的使用案例需要更細緻的許可，請使用此頁面上的其他區段來建立符合您業務需求的執行角色。您可以使用 SageMaker AI 主控台或 AWS CLI建立執行角色。

**重要**  
下列程序中使用的 IAM 受管政策`AmazonSageMakerFullAccess`僅授予執行角色許可，可對具有名為`SageMaker`、`Sagemaker`、`sagemaker`或`aws-glue`的儲存貯體或物件執行特定 Amazon S3 動作。要了解如何向執行角色新增其他政策以授予其對其他 Amazon S3 儲存貯體和物件的存取權限，請參閱[將其他 Amazon S3 許可新增至 SageMaker AI 執行角色](#sagemaker-roles-get-execution-role-s3)。

**注意**  
您可以在建立 SageMaker AI 網域或筆記本執行個體時直接建立執行角色。  
如需如何建立 SageMaker AI 網域的相關資訊，請參閱[使用 Amazon SageMaker AI 進行設定的指南](gs.md)。
如需如何建立筆記本執行個體的資訊，請參閱[建立教學課程的 Amazon SageMaker 筆記本執行個體](gs-setup-working-env.md)。

**從 SageMaker AI 主控台建立新的執行角色**

1. 前往 [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/) 開啟 IAM 主控台。

1. 選擇**角色**，然後選擇**建立角色**。

1. 將 **AWS 服務**保留為**信任的實體類型**，然後使用向下箭頭在**其他 AWS 服務的使用案例**中尋找 **SageMaker AI**。

1. 選擇 **SageMaker AI - 執行**，然後選擇**下一步**。

1. IAM 受管政策`AmazonSageMakerFullAccess`會自動連線至角色。若要查看此政策中包含的許可，請選擇政策名稱旁邊的加號 (**\$1**)。選擇**下一步**。

1. 輸入**角色名稱**和**描述**。

1. (選用) 將其他許可和標籤新增至角色。

1. 選擇建**立角色**。

1. 在 IAM 主控台的**角色**區段中，找到您剛建立的角色。如有需要，請使用文字方塊用來搜尋角色名稱的角色。

1. 在角色摘要頁面上，記下 ARN。

**若要從 AWS CLI建立新的執行角色**

使用 建立執行角色之前 AWS CLI，請務必遵循 中的指示進行更新和設定[（選用） 設定 AWS CLI](gs-set-up.md#gs-cli-prereq)，然後繼續執行 中的指示[使用 自訂設定 AWS CLI](onboard-custom.md#onboard-custom-instructions-cli)。

一旦建立了執行角色，您就可以將其與 SageMaker AI 網域、使用者設定檔或 Jupyter 筆記本執行個體建立關聯。
+ 若要了解如何將執行角色與現有 SageMaker AI 網域建立關聯，請參閱[編輯網域設定](domain-edit.md)。
+ 若要了解如何將執行角色與現有使用者設定檔建立關聯，請參閱[新增使用者設定檔](domain-user-profile-add.md)。
+ 若要了解如何將執行角色與現有筆記本執行個體建立關聯，請參閱[更新筆記本執行個體](nbi-update.md)。

您也可以將執行角色的 ARN 傳遞給 API 呼叫。例如，使用 [Amazon SageMaker Python SDK](https://sagemaker.readthedocs.io/en/stable)，您可以將執行角色的 ARN 傳遞給估算器。在接下來的程式碼範例中，我們使用 XGBoost 演算法容器建立估算器，並傳遞執行角色的 ARN 作為參數。有關 GitHub 上的完整範例，請參閱[使用 XGBoost 進行客戶流失率預測](https://github.com/aws/amazon-sagemaker-examples/blob/89c54681b7e0f83ce137b34b879388cf5960af93/introduction_to_applying_machine_learning/xgboost_customer_churn/xgboost_customer_churn.ipynb)。

```
import sagemaker, boto3
from sagemaker import image_uris

sess = sagemaker.Session()
region = sess.boto_region_name
bucket = sess.default_bucket()
prefix = "sagemaker/DEMO-xgboost-churn"
container = sagemaker.image_uris.retrieve("xgboost", region, "1.7-1")

xgb = sagemaker.estimator.Estimator(
    container,
    execution-role-ARN,
    instance_count=1,
    instance_type="ml.m4.xlarge",
    output_path="s3://{}/{}/output".format(bucket, prefix),
    sagemaker_session=sess,
)

...
```

### 將其他 Amazon S3 許可新增至 SageMaker AI 執行角色
<a name="sagemaker-roles-get-execution-role-s3"></a>

當您使用 SageMaker AI 特徵搭配 Amazon S3 中的資源 (例如輸入資料) 時，您在請求 (例如 `CreateTrainingJob`) 中指定的執行角色會用來存取這些資源。

如果您將 IAM 受管政策附加到執行角色，則該角色有權對具有名為`AmazonSageMakerFullAccess`、`SageMaker`、`Sagemaker`、`sagemaker`或`aws-glue`中的儲存貯體或物件執行特定 Amazon S3 動作。它也有權在任何 Amazon S3 資源上執行下列動作：

```
"s3:CreateBucket", 
"s3:GetBucketLocation",
"s3:ListBucket",
"s3:ListAllMyBuckets",
"s3:GetBucketCors",
"s3:PutBucketCors"
```

若要授予執行角色存取 Amazon S3 中一或多個特定儲存貯體的許可，您可以將類似下列內容的政策附加到該角色。此政策授予 IAM 角色許可，以執行 `AmazonSageMakerFullAccess` 允許但限制只能存取儲存貯體 amzn-s3-demo-bucket1 和 amzn-s3-demo-bucket2 的所有動作。請參閱您正在使用的特定 SageMaker AI 特徵的安全文件，以進一步了解該特徵所需的 Amazon S3 許可。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:PutObject",
                "s3:DeleteObject",
                "s3:AbortMultipartUpload"
            ],
            "Resource": [
                "arn:aws:s3:::amzn-s3-demo-bucket1/*",
                "arn:aws:s3:::amzn-s3-demo-bucket2/*"
            ]
        }, 
        {
            "Effect": "Allow",
            "Action": [
                "s3:CreateBucket",
                "s3:GetBucketLocation",
                "s3:ListBucket",
                "s3:ListAllMyBuckets",
                "s3:GetBucketCors",
                "s3:PutBucketCors"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetBucketAcl",
                "s3:PutObjectAcl"
            ],
            "Resource": [
                "arn:aws:s3:::amzn-s3-demo-bucket1",
                "arn:aws:s3:::amzn-s3-demo-bucket2"
            ]
        }
    ]
}
```

------

## 取得您的執行角色
<a name="sagemaker-roles-get-execution-role"></a>

您可以使用 [SageMaker AI 主控台](https://console.aws.amazon.com/sagemaker)、[Amazon SageMaker Python SDK](https://sagemaker.readthedocs.io/en/stable) 或 [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html)，來擷取連接到 SageMaker AI 網域、空間或使用者設定檔的執行角色的 ARN 和名稱。

**Topics**
+ [取得網域執行角色](#sagemaker-roles-get-execution-role-domain)
+ [取得空間執行角色](#sagemaker-roles-get-execution-role-space)
+ [取得使用者執行角色](#sagemaker-roles-get-execution-role-user)

### 取得網域執行角色
<a name="sagemaker-roles-get-execution-role-domain"></a>

以下提供有關尋找網域執行角色的指示。

#### 取得網域執行角色 (主控台)
<a name="sagemaker-roles-get-execution-role-domain-console"></a>

**尋找連接至您網域的執行角色**

1. 開啟 SageMaker AI 主控台，網址為 [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)

1. 在左側導覽窗格中，選擇**管理員組態**下的**網域**。

1. 選擇與您網域對應的連結。

1. 選擇**網域設定**索引標籤。

1. 在**一般設定**區段中，執行角色 ARN 會列示在**執行角色**下。

   執行角色名稱是在執行角色 ARN 中最後一個 `/` 之後。

### 取得空間執行角色
<a name="sagemaker-roles-get-execution-role-space"></a>

以下提供有關尋找空間執行角色的指示。

#### 取得空間執行角色 (主控台)
<a name="sagemaker-roles-get-execution-role-space-console"></a>

**尋找連接至您空間的執行角色**

1. 開啟 SageMaker AI 主控台，網址為 [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)

1. 在左側導覽窗格中，選擇**管理員組態**下的**網域**。

1. 選擇與您網域對應的連結。

1. 選擇**空間管理**索引標籤。

1. 在**詳細資訊**區段中，執行角色 ARN 會列示在**執行角色**下。

   執行角色名稱是在執行角色 ARN 中最後一個 `/` 之後。

#### 取得空間執行角色 (SDK for Python)
<a name="sagemaker-roles-get-execution-role-space-sdk"></a>

**注意**  
下列程式碼旨在 SageMaker AI 環境中執行，就像 Amazon SageMaker Studio 中的任何 IDE 一樣。如果您在 SageMaker AI 環境之外執行 `get_execution_role`，您將會收到錯誤。

下列 [https://sagemaker.readthedocs.io/en/stable/api/utility/session.html#sagemaker.session.get_execution_role](https://sagemaker.readthedocs.io/en/stable/api/utility/session.html#sagemaker.session.get_execution_role) [Amazon SageMaker Python SDK](https://sagemaker.readthedocs.io/en/stable) 命令會擷取連接至空間之執行角色的 ARN。

```
from sagemaker import get_execution_role
role = get_execution_role()
print(role)
```

執行角色名稱是在執行角色 ARN 中最後一個 `/` 之後。

### 取得使用者執行角色
<a name="sagemaker-roles-get-execution-role-user"></a>

以下提供有關尋找使用者執行角色的指示。

#### 取得使用者執行角色 (主控台)
<a name="sagemaker-roles-get-execution-role-user-console"></a>

**尋找連接至使用者的執行角色**

1. 開啟 SageMaker AI 主控台，網址為 [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)

1. 在左側導覽窗格中，選擇**管理員組態**下的**網域**。

1. 選擇與您網域對應的連結。

1. 選擇**使用者設定檔**索引標籤。

1. 選擇對應至您使用者的連結。

1. 在**詳細資訊**區段中，執行角色 ARN 會列示在**執行角色**下。

   執行角色名稱是在執行角色 ARN 中最後一個 `/` 之後。

#### 取得空間執行角色 (AWS CLI)
<a name="sagemaker-roles-get-execution-role-user-cli"></a>

**注意**  
若要使用下列範例，您必須安裝並設定 AWS Command Line Interface (AWS CLI)。如需詳細資訊，請參閱《AWS Command Line Interface 使用者指南第 2 版》**中的[開始使用 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)。

下列 [https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sts/get-caller-identity.html](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sts/get-caller-identity.html) AWS CLI 命令會顯示用於驗證請求的 IAM 身分相關資訊。呼叫者是 IAM 使用者。

```
aws sts get-caller-identity
```

執行角色名稱是在執行角色 ARN 中最後一個 `/` 之後。

## 變更您的執行角色
<a name="sagemaker-roles-change-execution-role"></a>

執行角色是 SageMaker AI 身分 (例如 SageMaker AI 使用者、空間或網域) 擔任的 IAM 角色。變更 IAM 角色會變更擔任該角色之所有身分的許可。

當您變更執行角色時，對應空間的執行角色也會變更。變更的效果可能需要一些時間才能傳播。
+ 當您變更*使用者的執行角色*時，該使用者建立的*私有空間*將擔任已變更的執行角色。
+ 當您變更*空間的預設執行角色*時，網域中的*共用空間*將擔任已變更的執行角色。

如需執行角色和空間的詳細資訊，請參閱 [了解網域空間許可和執行角色](execution-roles-and-spaces.md)。

您可以使用下列其中一個指示，將身分的執行角色變更為不同的 IAM 角色。

如果您想要改為*修改*身分正在擔任的角色，請參閱[修改執行角色的許可](#sagemaker-roles-modify-to-execution-role)。

**Topics**
+ [變更網域預設執行角色](#sagemaker-roles-change-execution-role-domain)
+ [變更空間預設執行角色](#sagemaker-roles-change-execution-role-space)
+ [變更使用者設定檔執行角色](#sagemaker-roles-change-execution-role-user)

### 變更網域預設執行角色
<a name="sagemaker-roles-change-execution-role-domain"></a>

以下提供有關變更網域預設執行角色的指示。

#### 變更網域預設執行角色 (主控台)
<a name="sagemaker-roles-change-execution-role-domain-console"></a>

**變更連接至網域的預設執行角色**

1. 開啟 SageMaker AI 主控台，網址為 [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)

1. 在左側導覽窗格中，選擇**管理員組態**下的**網域**。

1. 選擇與您網域對應的連結。

1. 選擇**網域設定**索引標籤。

1. 在**一般設定**區段中，選擇**編輯**。

1. 在**許可**區段的**預設執行角色**下，展開下拉式清單。

1. 在下拉式清單中，您可以選擇現有角色、輸入自訂 IAM 角色 ARN，或建立新的角色。

   如果想要建立新的角色，您可以選擇**使用角色建立精靈建立角色**選項。

1. 在下列步驟中選擇 [下一步]，然後在最後一個步驟上選擇 [提交]。

### 變更空間預設執行角色
<a name="sagemaker-roles-change-execution-role-space"></a>

以下提供有關變更空間預設執行角色的指示。變更此執行角色將變更網域中所有共用空間擔任的角色。

#### 變更空間預設執行角色 (主控台)
<a name="sagemaker-roles-change-execution-role-space-console"></a>

**變更您建立新空間時的空間預設執行角色**

1. 開啟 SageMaker AI 主控台，網址為 [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)

1. 在左側導覽窗格中，選擇**管理員組態**下的**網域**。

1. 選擇與您網域對應的連結。

1. 選擇**網域設定**索引標籤。

1. 在**一般設定**區段中，選擇**編輯**。

1. 在**許可**區段的**空間預設執行角色**下，展開下拉式清單。

1. 在下拉式清單中，您可以選擇現有角色、輸入自訂 IAM 角色 ARN，或建立新的角色。

   如果想要建立新的角色，您可以選擇**使用角色建立精靈建立角色**選項。

1. 在下列步驟中選擇**下一步**，然後在最後一個步驟上選擇**提交**。

### 變更使用者設定檔執行角色
<a name="sagemaker-roles-change-execution-role-user"></a>

以下提供有關變更使用者執行角色的指示。變更此執行角色將變更此使用者建立的所有私有空間擔任的角色。

#### 變更使用者設定檔執行角色 (主控台)
<a name="sagemaker-roles-change-execution-role-user-console"></a>

**變更連接至使用者的執行角色**

1. 開啟 SageMaker AI 主控台，網址為 [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)

1. 在左側導覽窗格中，選擇**管理員組態**下的**網域**。

1. 選擇與您網域對應的連結。

1. 選擇**使用者設定檔**索引標籤。

1. 選擇對應至使用者設定檔名稱的連結。

1. 選擇**編輯**。

1. 在下拉式清單中，您可以選擇現有角色、輸入自訂 IAM 角色 ARN，或建立新的角色。

   如果想要建立新的角色，您可以選擇**使用角色建立精靈建立角色**選項。

1. 在下列步驟中選擇**下一步**，然後在最後一個步驟上選擇**提交**。

## 修改執行角色的許可
<a name="sagemaker-roles-modify-to-execution-role"></a>

您可以將現有的許可修改為身分的執行角色 (例如 SageMaker AI 使用者、空間或網域)。此作法是尋找身分擔任的適當 IAM 角色，然後修改該 IAM 角色。以下將提供有關透過主控台實現此操作的指示。

當您修改執行角色時，對應空間的執行角色也會變更。變更的效果可能不會立即顯現。
+ 當您修改*使用者的執行角色時*，該使用者建立的*私有空間*將擔任已修改的執行角色。
+ 當您修改*空間的預設執行角色*時，網域中的*共用空間*將擔任已修改的執行角色。

如需執行角色和空間的詳細資訊，請參閱 [了解網域空間許可和執行角色](execution-roles-and-spaces.md)。

如果您想要*變更*身分正在擔任的角色，請參閱[變更您的執行角色](#sagemaker-roles-change-execution-role)。

### 修改執行角色的許可 (主控台)
<a name="sagemaker-roles-modify-to-execution-role-console"></a>

**修改執行角色的許可**

1. 首先取得您要修改的身分名稱。
   + [取得網域執行角色](#sagemaker-roles-get-execution-role-domain)
   + [取得空間執行角色](#sagemaker-roles-get-execution-role-space)
   + [取得使用者執行角色](#sagemaker-roles-get-execution-role-user)

1. 若要修改身分正在擔任的角色，請參閱《AWS Identity and Access Management 使用者指南》**中的[修改角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_manage_modify.html)。

   如需如何將許可新增至 IAM 身分的詳細資訊和指示，請參閱《AWS Identity and Access Management 使用者指南》**中的[新增和移除 IAM 身分許可](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html)。

## 傳遞角色
<a name="sagemaker-roles-pass-role"></a>

例如在服務間傳遞角色之類的動作是 SageMaker AI 內的常用功能。您可以在《服務授權參考》**中的 [SageMaker AI 的動作、資源和條件金鑰](https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonsagemaker.html#amazonsagemaker-actions-as-permissions)中找到更多的詳細資訊。

您會在進行這些 API 呼叫時傳遞角色 (`iam:PassRole`)：[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateAutoMLJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateAutoMLJob.html)、[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateCompilationJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateCompilationJob.html)、[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateDomain.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateDomain.html)、[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateFeatureGroup.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateFeatureGroup.html)、[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateFlowDefinition.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateFlowDefinition.html)、[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateHyperParameterTuningJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateHyperParameterTuningJob.html)、[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateImage.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateImage.html)、[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateLabelingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateLabelingJob.html)、[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html)、[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateMonitoringSchedule.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateMonitoringSchedule.html)、[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateNotebookInstance.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateNotebookInstance.html)、[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateProcessingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateProcessingJob.html)、[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html)、[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateUserProfile.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateUserProfile.html)、[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_RenderUiTemplate.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_RenderUiTemplate.html)、[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateImage.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateImage.html) 和 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateNotebookInstance.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateNotebookInstance.html)。

您會將以下信任政策連接至 IAM 角色，授予 SageMaker AI 主體擔任角色的許可，並且對於所有執行角色都是相同的操作：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "sagemaker.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}
```

------

您需要授予給角色的許可會視 API 呼叫而異。以下各節會說明這些許可。

**注意**  
您可以使用 受管許可政策，而不是透過制定許可政策來 AWS管理`AmazonSageMakerFullAccess`許可。此政策中的許可相當廣泛，允許您可能想要在 SageMaker AI 中執行的任何動作。如需包含新增許多許可原因資訊的政策清單，請參閱[AWS 受管政策：AmazonSageMakerFullAccess](security-iam-awsmanpol.md#security-iam-awsmanpol-AmazonSageMakerFullAccess)。如果想要建立自訂政策及管理許可，只限定執行角色所需的執行動作許可，請參閱下列主題。

**重要**  
如果您遇到任何問題，請參閱[針對 Amazon SageMaker AI Identity and Access 進行疑難排解](security_iam_troubleshoot.md)。

如需 IAM 角色的詳細資訊，請參閱《服務授權參考》**中的 [IAM 角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles.html)。

**Topics**
+ [建立執行角色](#sagemaker-roles-create-execution-role)
+ [取得您的執行角色](#sagemaker-roles-get-execution-role)
+ [變更您的執行角色](#sagemaker-roles-change-execution-role)
+ [修改執行角色的許可](#sagemaker-roles-modify-to-execution-role)
+ [傳遞角色](#sagemaker-roles-pass-role)
+ [CreateAutoMLJob 和 CreateAutoMLJobV2 API：執行角色許可](#sagemaker-roles-autopilot-perms)
+ [CreateModel API：執行角色許可](#sagemaker-roles-createdomain-perms)
+ [CreateImage 和 UpdateImage API：執行角色許可](#sagemaker-roles-createimage-perms)
+ [CreateNotebookInstance API：執行角色許可](#sagemaker-roles-createnotebookinstance-perms)
+ [CreateHyperParameterTuningJob API：執行角色許可](#sagemaker-roles-createhyperparametertiningjob-perms)
+ [CreateProcessingJob API：執行角色許可](#sagemaker-roles-createprocessingjob-perms)
+ [CreateTrainingJob API：執行角色許可](#sagemaker-roles-createtrainingjob-perms)
+ [CreateModel API：執行角色許可](#sagemaker-roles-createmodel-perms)
+ [SageMaker 地理空間功能角色](sagemaker-geospatial-roles.md)

## CreateAutoMLJob 和 CreateAutoMLJobV2 API：執行角色許可
<a name="sagemaker-roles-autopilot-perms"></a>

如需您可以在 `CreateAutoMLJob` 或 `CreateAutoMLJobV2` API 請求中傳遞的執行角色，您可以將以下最低許可政策連接至角色：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iam:PassRole"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "iam:PassedToService": "sagemaker.amazonaws.com"
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "sagemaker:DescribeEndpointConfig",
                "sagemaker:DescribeModel",
                "sagemaker:InvokeEndpoint",
                "sagemaker:ListTags",
                "sagemaker:DescribeEndpoint",
                "sagemaker:CreateModel",
                "sagemaker:CreateEndpointConfig",
                "sagemaker:CreateEndpoint",
                "sagemaker:DeleteModel",
                "sagemaker:DeleteEndpointConfig",
                "sagemaker:DeleteEndpoint",
                "cloudwatch:PutMetricData",
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "logs:CreateLogGroup",
                "logs:DescribeLogStreams",
                "s3:GetObject",
                "s3:PutObject",
                "s3:ListBucket",
                "ecr:GetAuthorizationToken",
                "ecr:BatchCheckLayerAvailability",
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage"
            ],
            "Resource": "*"
        }
    ]
}
```

------

欲指定 AutoML 工作的私有 VPC，則請新增以下許可：

```
{
    "Effect": "Allow",
    "Action": [
        "ec2:CreateNetworkInterface",
        "ec2:CreateNetworkInterfacePermission",
        "ec2:DeleteNetworkInterface",
        "ec2:DeleteNetworkInterfacePermission",
        "ec2:DescribeNetworkInterfaces",
        "ec2:DescribeVpcs",
        "ec2:DescribeDhcpOptions",
        "ec2:DescribeSubnets",
        "ec2:DescribeSecurityGroups"
    ]
}
```

如果您的輸入使用伺服器端加密搭配 AWS KMS 受管金鑰 (SSE-KMS) 進行加密，請新增下列許可：

```
{
    "Effect": "Allow",
    "Action": [
        "kms:Decrypt"
    ]
}
```

如果您在 AutoML 任務的輸出組態中指定 KMS 金鑰，請新增下列許可：

```
{
    "Effect": "Allow",
    "Action": [
    "kms:Encrypt"
    ]
}
```

如果您在 AutoML 任務的資源組態中指定磁碟區 KMS 金鑰，請新增下列許可：

```
{
    "Effect": "Allow",
    "Action": [
    "kms:CreateGrant"
    ]
}
```

## CreateModel API：執行角色許可
<a name="sagemaker-roles-createdomain-perms"></a>

當您在 `CreateDomain` API 請求`KmsKeyId`中將 AWS KMS 客戶受管金鑰作為 傳遞時，IAM Identity Center 網域的執行角色和 IAM 網域的使用者/執行角色需要下列許可。許可會在 `CreateApp` API 呼叫期間強制執行。

如需採用能夠在 `CreateDomain` API 請求中進行傳遞的執行角色，則可以將以下許可政策連接至角色：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "kms:CreateGrant",
                "kms:DescribeKey"
            ],
            "Resource": "arn:aws:kms:us-east-1:111122223333:key/kms-key-id"
        }
    ]
}
```

------

或者，如果在 KMS 政策中指定許可，您可以將下列政策附加至角色：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "Allow use of the key",
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::111122223333:role/ExecutionRole"
                ]
            },
            "Action": [
                "kms:CreateGrant",
                "kms:DescribeKey"
            ],
            "Resource": "*"
        }
    ]
}
```

------

## CreateImage 和 UpdateImage API：執行角色許可
<a name="sagemaker-roles-createimage-perms"></a>

如需採用能夠在 `CreateImage` 或 `UpdateImage` API 請求中進行傳遞的執行角色，則可以將以下許可政策連接至角色：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ecr:BatchGetImage",
                "ecr:GetDownloadUrlForLayer"
            ],
            "Resource": "*"
        }
    ]
}
```

------

## CreateNotebookInstance API：執行角色許可
<a name="sagemaker-roles-createnotebookinstance-perms"></a>

您為了呼叫 `CreateNotebookInstance` API 而授予給執行角色的許可，將取決於預期的筆記本執行個體功用。如果您計劃將其用來調用 SageMaker AI API，並在呼叫 `CreateTrainingJob` 和 `CreateModel` API 時傳遞相同的角色，請將以下許可政策連接至角色：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "sagemaker:*",
                "ecr:GetAuthorizationToken",
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage",
                "ecr:BatchCheckLayerAvailability",
                "ecr:SetRepositoryPolicy",
                "ecr:CompleteLayerUpload",
                "ecr:BatchDeleteImage",
                "ecr:UploadLayerPart",
                "ecr:DeleteRepositoryPolicy",
                "ecr:InitiateLayerUpload",
                "ecr:DeleteRepository",
                "ecr:PutImage",
                "ecr:CreateRepository",
                "cloudwatch:PutMetricData",
                "cloudwatch:GetMetricData",
                "cloudwatch:GetMetricStatistics",
                "cloudwatch:ListMetrics",
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:DescribeLogStreams",
                "logs:PutLogEvents",
                "logs:GetLogEvents",
                "s3:CreateBucket",
                "s3:ListBucket",
                "s3:GetBucketLocation",
                "s3:GetObject",
                "s3:PutObject",
                "s3:DeleteObject",
                "robomaker:CreateSimulationApplication",
                "robomaker:DescribeSimulationApplication",
                "robomaker:DeleteSimulationApplication",
                "robomaker:CreateSimulationJob",
                "robomaker:DescribeSimulationJob",
                "robomaker:CancelSimulationJob",
                "ec2:CreateVpcEndpoint",
                "ec2:DescribeRouteTables",
                "elasticfilesystem:DescribeMountTargets"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "codecommit:GitPull",
                "codecommit:GitPush"
            ],
            "Resource": [
                "arn:aws:codecommit:*:*:*sagemaker*",
                "arn:aws:codecommit:*:*:*SageMaker*",
                "arn:aws:codecommit:*:*:*Sagemaker*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iam:PassRole"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "iam:PassedToService": "sagemaker.amazonaws.com"
                }
            }
        }
    ]
}
```

------

若要限縮許可，請藉由限制 `"Resource": "*"` 將它們限制為特定的 Amazon S3 和 Amazon ECR 資源，如下所示：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "sagemaker:*",
                "ecr:GetAuthorizationToken",
                "cloudwatch:PutMetricData",
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:DescribeLogStreams",
                "logs:PutLogEvents",
                "logs:GetLogEvents"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "iam:PassRole"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "iam:PassedToService": "sagemaker.amazonaws.com"
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::inputbucket"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:PutObject",
                "s3:DeleteObject"
            ],
            "Resource": [
                "arn:aws:s3:::inputbucket/object1",
                "arn:aws:s3:::outputbucket/path",
                "arn:aws:s3:::inputbucket/object2",
                "arn:aws:s3:::inputbucket/object3"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "ecr:BatchCheckLayerAvailability",
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage"
            ],
            "Resource": [
                "arn:aws:ecr:us-east-1:111122223333:repository/my-repo1",
                "arn:aws:ecr:us-east-1:111122223333:repository/my-repo2",
                "arn:aws:ecr:us-east-1:111122223333:repository/my-repo3"
            ]
        }
    ]
}
```

------

如果您計劃存取其他資源 (例如 Amazon DynamoDB 或 Amazon 關聯式資料庫服務)，請將相關的許可新增至此政策。

您在上述政策中所限制的政策範圍如下：
+ 將 `s3:ListBucket` 許可範圍限制為特定儲存貯體，其為您在 `InputDataConfig.DataSource.S3DataSource.S3Uri` 請求中指定為 `CreateTrainingJob` 的儲存貯體。
+ 將 `s3:GetObject `、`s3:PutObject` 與 `s3:DeleteObject` 許可範圍限制如下：
  + 限制範圍是您在 `CreateTrainingJob` 請求中指定的下列值：

    `InputDataConfig.DataSource.S3DataSource.S3Uri`

    `OutputDataConfig.S3OutputPath`
  + 限制範圍是您在 `CreateModel` 請求中指定的下列值：

    `PrimaryContainer.ModelDataUrl`

    `SuplementalContainers.ModelDataUrl`
+ 將 `ecr` 許可範圍限制如下：
  + 限制範圍是您在 `AlgorithmSpecification.TrainingImage` 請求中指定的 `CreateTrainingJob` 值。
  + 限制範圍是您在 `PrimaryContainer.Image` 請求中指定的 `CreateModel` 值：

`cloudwatch` 與 `logs` 動作皆適用於 “\$1” 資源。如需更多資訊，請參閱 Amazon CloudWatch 使用者指南中的 [CloudWatch 資源和操作](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/iam-access-control-overview-cw.html#CloudWatch_ARN_Format)。



## CreateHyperParameterTuningJob API：執行角色許可
<a name="sagemaker-roles-createhyperparametertiningjob-perms"></a>

如需採用能夠在 `CreateHyperParameterTuningJob` API 請求中進行傳遞的執行角色，則可以將以下許可政策連接至角色：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudwatch:PutMetricData",
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "logs:CreateLogGroup",
                "logs:DescribeLogStreams",
                "s3:GetObject",
                "s3:PutObject",
                "s3:ListBucket",
                "ecr:GetAuthorizationToken",
                "ecr:BatchCheckLayerAvailability",
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage"
            ],
            "Resource": "*"
        }
    ]
}
```

------

您不需要指定 `"Resource": "*"`，僅需將這些許可範圍限制為特定的 Amazon S3、Amazon ECR 和 Amazon CloudWatch Logs 資源即可：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudwatch:PutMetricData",
                "ecr:GetAuthorizationToken"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::inputbucket"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::inputbucket/object",
                "arn:aws:s3:::outputbucket/path"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "ecr:BatchCheckLayerAvailability",
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage"
            ],
            "Resource": "arn:aws:ecr:us-east-1:111122223333:repository/my-repo"
        },
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "logs:CreateLogGroup",
                "logs:DescribeLogStreams"
            ],
            "Resource": "arn:aws:logs:us-east-1:111122223333:log-group:/aws/sagemaker/TrainingJobs*"
        }
    ]
}
```

------

如果與超參數調校任務相關聯的訓練容器需要存取其他資料來源 (例如 DynamoDB 或 Amazon RDS 資源)，請將相關的許可新增至此政策。

您在上述政策中所限制的政策範圍如下：
+ 將 `s3:ListBucket` 許可範圍限制為特定儲存貯體，其為您在 `InputDataConfig.DataSource.S3DataSource.S3Uri` 請求中指定為 `CreateTrainingJob` 的儲存貯體。
+ 將 `s3:GetObject ` 與 `s3:PutObject` 許可範圍限制為下列物件，其為您在 `CreateHyperParameterTuningJob` 請求的輸入和輸出資料組態中所指定的物件：

  `InputDataConfig.DataSource.S3DataSource.S3Uri`

  `OutputDataConfig.S3OutputPath`
+ 將 Amazon ECR 許可範圍限制為登錄檔路徑 (`AlgorithmSpecification.TrainingImage`)，其為您在 `CreateHyperParameterTuningJob` 請求中指定的路徑。
+ 將 Amazon CloudWatch Logs 許可範圍限制為日誌群組，以記錄 SageMaker 訓練任務群組。

`cloudwatch` 動作皆適用於 “\$1” 資源。如需詳細資訊，請參閱《Amazon CloudWatch 使用者指南》中的 [CloudWatch 資源和操作](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/iam-access-control-overview-cwl.html#CWL_ARN_Format)。

如指定超參數調校任務的私有 VPC，請新增下列許可：

```
{
    "Effect": "Allow",
    "Action": [
        "ec2:CreateNetworkInterface",
        "ec2:CreateNetworkInterfacePermission",
        "ec2:DeleteNetworkInterface",
        "ec2:DeleteNetworkInterfacePermission",
        "ec2:DescribeNetworkInterfaces",
        "ec2:DescribeVpcs",
        "ec2:DescribeDhcpOptions",
        "ec2:DescribeSubnets",
        "ec2:DescribeSecurityGroups"
    ]
}
```

如果您的輸入使用伺服器端加密搭配 AWS KMS 受管金鑰 (SSE-KMS) 進行加密，請新增下列許可：

```
{
    "Effect": "Allow",
    "Action": [
        "kms:Decrypt"
    ]
}
```

如果您在超參數調校任務的輸出組態中指定 KMS 金鑰，請新增下列許可：

```
{
    "Effect": "Allow",
    "Action": [
    "kms:Encrypt"
    ]
}
```

如果您在超參數調校任務的資源組態中指定磁碟區 KMS 金鑰，請新增下列許可：

```
{
    "Effect": "Allow",
    "Action": [
    "kms:CreateGrant"
    ]
}
```

## CreateProcessingJob API：執行角色許可
<a name="sagemaker-roles-createprocessingjob-perms"></a>

如需採用能夠在 `CreateProcessingJob` API 請求中進行傳遞的執行角色，則可以將以下許可政策連接至角色：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudwatch:PutMetricData",
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "logs:CreateLogGroup",
                "logs:DescribeLogStreams",
                "s3:GetObject",
                "s3:PutObject",
                "s3:ListBucket",
                "ecr:GetAuthorizationToken",
                "ecr:BatchCheckLayerAvailability",
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage"
            ],
            "Resource": "*"
        }
    ]
}
```

------

您不需要指定 `"Resource": "*"`，僅需將這些許可範圍限制為特定的 Amazon S3 和 Amazon ECR 資源即可：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudwatch:PutMetricData",
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "logs:CreateLogGroup",
                "logs:DescribeLogStreams",
                "ecr:GetAuthorizationToken"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::inputbucket"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::inputbucket/object",
                "arn:aws:s3:::outputbucket/path"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "ecr:BatchCheckLayerAvailability",
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage"
            ],
            "Resource": "arn:aws:ecr:us-east-1:111122223333:repository/my-repo"
        }
    ]
}
```

------

如果 `CreateProcessingJob.AppSpecification.ImageUri` 需要存取其他資料來源 (例如 DynamoDB 或 Amazon RDS 資源)，請將相關的許可新增至此政策。

您在上述政策中所限制的政策範圍如下：
+ 將 `s3:ListBucket` 許可範圍限制為特定儲存貯體，其為您在 `ProcessingInputs` 請求中指定為 `CreateProcessingJob` 的儲存貯體。
+ 將 `s3:GetObject ` 和 `s3:PutObject` 許可的範圍限制為將於 `ProcessingInputs` 和 `CreateProcessingJob` 請求中下載或上載 `ProcessingOutputConfig` 的物件。
+ 將 Amazon ECR 許可範圍限制為登錄檔路徑 (`AppSpecification.ImageUri`)，其為您在 `CreateProcessingJob` 請求中指定的路徑。

`cloudwatch` 與 `logs` 動作皆適用於 “\$1” 資源。如需更多資訊，請參閱 Amazon CloudWatch 使用者指南中的 [CloudWatch 資源和操作](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/iam-access-control-overview-cw.html#CloudWatch_ARN_Format)。

如果您為處理任務指定私有 VPC，請新增下列許可。請勿在政策中使用任何條件或資源篩選器來設定範圍。否則，建立處理任務失敗期間發生驗證檢查。

```
{
    "Effect": "Allow",
    "Action": [
        "ec2:CreateNetworkInterface",
        "ec2:CreateNetworkInterfacePermission",
        "ec2:DeleteNetworkInterface",
        "ec2:DeleteNetworkInterfacePermission",
        "ec2:DescribeNetworkInterfaces",
        "ec2:DescribeVpcs",
        "ec2:DescribeDhcpOptions",
        "ec2:DescribeSubnets",
        "ec2:DescribeSecurityGroups"
    ]
}
```

如果您的輸入使用伺服器端加密搭配 AWS KMS 受管金鑰 (SSE-KMS) 進行加密，請新增下列許可：

```
{
    "Effect": "Allow",
    "Action": [
        "kms:Decrypt"
    ]
}
```

如果您在處理任務的輸出組態中指定 KMS 金鑰，請新增下列許可：

```
{
    "Effect": "Allow",
    "Action": [
    "kms:Encrypt"
    ]
}
```

如果您在處理任務的資源組態中指定磁碟區 KMS 金鑰，請新增下列許可：

```
{
    "Effect": "Allow",
    "Action": [
    "kms:CreateGrant"
    ]
}
```

## CreateTrainingJob API：執行角色許可
<a name="sagemaker-roles-createtrainingjob-perms"></a>

如需採用能夠在 `CreateTrainingJob` API 請求中進行傳遞的執行角色，則可以將以下許可政策連接至角色：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudwatch:PutMetricData",
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "logs:CreateLogGroup",
                "logs:DescribeLogStreams",
                "s3:GetObject",
                "s3:PutObject",
                "s3:ListBucket",
                "ecr:GetAuthorizationToken",
                "ecr:BatchCheckLayerAvailability",
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage"
            ],
            "Resource": "*"
        }
    ]
}
```

------

您不需要指定 `"Resource": "*"`，僅需將這些許可範圍限制為特定的 Amazon S3 和 Amazon ECR 資源即可：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudwatch:PutMetricData",
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "logs:CreateLogGroup",
                "logs:DescribeLogStreams",
                "ecr:GetAuthorizationToken"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::inputbucket"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::inputbucket/object",
                "arn:aws:s3:::outputbucket/path"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "ecr:BatchCheckLayerAvailability",
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage"
            ],
            "Resource": "arn:aws:ecr:us-east-1:111122223333:repository/my-repo"
        }
    ]
}
```

------

如果 `CreateTrainingJob.AlgorithSpecifications.TrainingImage` 需要存取其他資料來源 (例如 DynamoDB 或 Amazon RDS 資源)，請將相關的許可新增至此政策。

如果您使用 `AlgorithmSpecification.AlgorithmArn` 參數指定演算法資源，執行角色也必須具有下列許可：

```
{
    "Effect": "Allow",
    "Action": [
        "sagemaker:DescribeAlgorithm"
    ],
    "Resource": "arn:aws:sagemaker:*:*:algorithm/*"
}
```

您在上述政策中所限制的政策範圍如下：
+ 將 `s3:ListBucket` 許可範圍限制為特定儲存貯體，其為您在 `InputDataConfig.DataSource.S3DataSource.S3Uri` 請求中指定為 `CreateTrainingJob` 的儲存貯體。
+ 將 `s3:GetObject ` 與 `s3:PutObject` 許可範圍限制為下列物件，其為您在 `CreateTrainingJob` 請求的輸入和輸出資料組態中所指定的物件：

  `InputDataConfig.DataSource.S3DataSource.S3Uri`

  `OutputDataConfig.S3OutputPath`
+ 將 Amazon ECR 許可範圍限制為登錄檔路徑 (`AlgorithmSpecification.TrainingImage`)，其為您在 `CreateTrainingJob` 請求中指定的路徑。

`cloudwatch` 與 `logs` 動作皆適用於 “\$1” 資源。如需更多資訊，請參閱 Amazon CloudWatch 使用者指南中的 [CloudWatch 資源和作業](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/iam-access-control-overview-cw.html#CloudWatch_ARN_Format)。

欲指定訓練工作的私有 VPC，則請新增以下許可：

```
{
    "Effect": "Allow",
    "Action": [
      "ec2:CreateNetworkInterface",
      "ec2:CreateNetworkInterfacePermission",
      "ec2:DeleteNetworkInterface",
      "ec2:DeleteNetworkInterfacePermission",
      "ec2:DescribeNetworkInterfaces",
      "ec2:DescribeVpcs",
      "ec2:DescribeDhcpOptions",
      "ec2:DescribeSubnets",
      "ec2:DescribeSecurityGroups"
    ]
}
```

如果您的輸入使用伺服器端加密搭配 AWS KMS 受管金鑰 (SSE-KMS) 進行加密，請新增下列許可：

```
{
    "Effect": "Allow",
    "Action": [
        "kms:Decrypt"
    ]
}
```

如果您在訓練任務的輸出組態中指定 KMS 金鑰，請新增下列許可：

```
{
    "Effect": "Allow",
    "Action": [
    "kms:Encrypt"
    ]
}
```

如果您在訓練任務的資源組態中指定磁碟區 KMS 金鑰，請新增下列許可：

```
{
    "Effect": "Allow",
    "Action": [
    "kms:CreateGrant"
    ]
}
```

## CreateModel API：執行角色許可
<a name="sagemaker-roles-createmodel-perms"></a>

如需採用能夠在 `CreateModel` API 請求中進行傳遞的執行角色，則可以將以下許可政策連接至角色：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudwatch:PutMetricData",
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "logs:CreateLogGroup",
                "logs:DescribeLogStreams",
                "s3:GetObject",
                "s3:ListBucket",
                "ecr:GetAuthorizationToken",
                "ecr:BatchCheckLayerAvailability",
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage"
            ],
            "Resource": "*"
        }
    ]
}
```

------

您不需要指定 `"Resource": "*"`，僅需將這些許可範圍限制為特定的 Amazon S3 與 Amazon ECR 資源即可：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudwatch:PutMetricData",
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "logs:CreateLogGroup",
                "logs:DescribeLogStreams",
                "ecr:GetAuthorizationToken"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::inputbucket/object"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "ecr:BatchCheckLayerAvailability",
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage"
            ],
            "Resource": [
                "arn:aws:ecr:us-east-1:111122223333:repository/my-repo",
                "arn:aws:ecr:us-east-1:111122223333:repository/my-repo"
            ]
        }
    ]
}
```

------

如果 `CreateModel.PrimaryContainer.Image` 需要存取其他資料來源 (例如 Amazon DynamoDB 或 Amazon RDS 資源)，請將相關的許可新增至此政策。

您在上述政策中所限制的政策範圍如下：
+ 將 S3 許可範圍限制為物件，其為您在 `PrimaryContainer.ModelDataUrl` 請求的 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html) 中所指定的物件。
+ 將 Amazon ECR 許可範圍限制為特定的登錄檔路徑，其為您在 `PrimaryContainer.Image` 請求中指定為 `SecondaryContainer.Image` 與 `CreateModel` 的路徑。

`cloudwatch` 與 `logs` 動作皆適用於 “\$1” 資源。如需更多資訊，請參閱 Amazon CloudWatch 使用者指南中的 [CloudWatch 資源和作業](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/iam-access-control-overview-cw.html#CloudWatch_ARN_Format)。

**注意**  
如果您計劃在生產環境中使用 [SageMaker AI 部署防護機制功能](https://docs.aws.amazon.com/sagemaker/latest/dg/deployment-guardrails.html)進行模型部署，請確保您的執行角色具有對自動還原警示執行 `cloudwatch:DescribeAlarms` 動作的許可。

欲指定模型的私有 VPC，則請新增以下許可：

```
{
    "Effect": "Allow",
    "Action": [
        "ec2:CreateNetworkInterface",
        "ec2:CreateNetworkInterfacePermission",
        "ec2:DeleteNetworkInterface",
        "ec2:DeleteNetworkInterfacePermission",
        "ec2:DescribeNetworkInterfaces",
        "ec2:DescribeVpcs",
        "ec2:DescribeDhcpOptions",
        "ec2:DescribeSubnets",
        "ec2:DescribeSecurityGroups"
    ]
}
```