

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

# Amazon SageMaker AI 身分型政策範例
<a name="security_iam_id-based-policy-examples"></a>

根據預設，IAM 使用者和角色沒有建立或修改 SageMaker AI 資源的許可。他們也無法使用 AWS 管理主控台 AWS CLI或 AWS API 執行任務。IAM 管理員必須建立 IAM 政策，授予使用者和角色在指定資源上執行特定 API 作業的所需許可。管理員接著必須將這些政策連接至需要這些許可的 IAM 使用者或群組。若要了解如何將策略連接到 IAM 使用者或群組，請參閱《服務授權參考》**中的[新增和移除 IAM 身分許可](https://docs.aws.amazon.com/service-authorization/latest/reference/access_policies_manage-attach-detach.html)。

若要了解如何使用這些範例 JSON 政策文件建立 IAM 身分型政策，請參閱[在 JSON 索引標籤上建立政策](https://docs.aws.amazon.com/service-authorization/latest/reference/access_policies_create.html#access_policies_create-json-editor)。

**Topics**
+ [政策最佳實務](#security_iam_service-with-iam-policy-best-practices)
+ [使用 SageMaker AI 主控台](#security_iam_id-based-policy-examples-console)
+ [允許使用者檢視他們自己的許可](#security_iam_id-based-policy-examples-view-own-permissions)
+ [使用條件金鑰控制 SageMaker AI 資源的建立](#sagemaker-condition-examples)
+ [使用身分型政策控制對 SageMaker AI API 的存取](#api-access-policy)
+ [限制只能透過 IP 位址存取 SageMaker AI API 和執行時期呼叫](#api-ip-filter)
+ [依 IP 位址限制對筆記本執行個體的存取](#nbi-ip-filter)
+ [使用標籤控制對 SageMaker AI 資源的存取](#access-tag-policy)
+ [提供標記 SageMaker AI 資源的許可](#grant-tagging-permissions)
+ [使用可見性條件限制對可搜尋資源的存取](#limit-access-to-searchable-resources)

## 政策最佳實務
<a name="security_iam_service-with-iam-policy-best-practices"></a>

身分型政策會判斷某人是否可以在您的帳戶中建立、存取或刪除 SageMaker AI 資源。這些動作可能會讓您的 AWS 帳戶產生費用。當您建立或編輯身分型政策時，請遵循下列準則及建議事項：
+ **開始使用 AWS 受管政策並邁向最低權限許可** – 若要開始將許可授予您的使用者和工作負載，請使用將許可授予許多常見使用案例的 *AWS 受管政策*。它們可在您的 中使用 AWS 帳戶。我們建議您定義特定於使用案例 AWS 的客戶受管政策，以進一步減少許可。如需更多資訊，請參閱《*IAM 使用者指南*》中的 [AWS 受管政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-vs-inline.html#aws-managed-policies)或[任務職能的AWS 受管政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_job-functions.html)。
+ **套用最低權限許可** – 設定 IAM 政策的許可時，請僅授予執行任務所需的許可。為實現此目的，您可以定義在特定條件下可以對特定資源採取的動作，這也稱為*最低權限許可*。如需使用 IAM 套用許可的更多相關資訊，請參閱《*IAM 使用者指南*》中的 [IAM 中的政策和許可](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html)。
+ **使用 IAM 政策中的條件進一步限制存取權** – 您可以將條件新增至政策，以限制動作和資源的存取。例如，您可以撰寫政策條件，指定必須使用 SSL 傳送所有請求。如果透過特定 例如 使用服務動作 AWS 服務，您也可以使用條件來授予其存取權 CloudFormation。如需詳細資訊，請參閱《*IAM 使用者指南*》中的 [IAM JSON 政策元素：條件](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition.html)。
+ **使用 IAM Access Analyzer 驗證 IAM 政策，確保許可安全且可正常運作** – IAM Access Analyzer 驗證新政策和現有政策，確保這些政策遵從 IAM 政策語言 (JSON) 和 IAM 最佳實務。IAM Access Analyzer 提供 100 多項政策檢查及切實可行的建議，可協助您撰寫安全且實用的政策。如需詳細資訊，請參閱《*IAM 使用者指南*》中的[使用 IAM Access Analyzer 驗證政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access-analyzer-policy-validation.html)。
+ **需要多重要素驗證 (MFA)** – 如果您的案例需要 IAM 使用者或 中的根使用者 AWS 帳戶，請開啟 MFA 以提高安全性。如需在呼叫 API 操作時請求 MFA，請將 MFA 條件新增至您的政策。如需詳細資訊，請參閱《*IAM 使用者指南*》中的[透過 MFA 的安全 API 存取](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa_configure-api-require.html)。

如需 IAM 中最佳實務的相關資訊，請參閱《*IAM 使用者指南*》中的 [IAM 安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)。

## 使用 SageMaker AI 主控台
<a name="security_iam_id-based-policy-examples-console"></a>

若要存取 Amazon SageMaker AI 主控台，您必須具有一組最低許可。這些許可必須允許您列出和檢視 AWS 帳戶中 SageMaker AI 資源的詳細資訊。如果您建立比最低必要許可更嚴格的身分型政策，則對於具有該政策的實體而言，主控台將無法正常運作。這包括具有該政策的使用者或角色。

為了確保這些實體仍然可以使用 SageMaker AI 主控台，您還必須將以下 AWS 受管政策連接到實體。如需詳細資訊，請參閱《服務授權參考》**中的[將許可新增至使用者](https://docs.aws.amazon.com/service-authorization/latest/reference/id_users_change-permissions.html#users_change_permissions-add-console)：

對於僅呼叫 AWS CLI 或 AWS API 的使用者，您不需要允許最低主控台許可。反之，只需允許存取符合您嘗試執行之 API 作業的動作就可以了。

**Topics**
+ [使用 Amazon SageMaker AI 主控台所需的許可](#console-permissions)
+ [使用 Amazon SageMaker Ground Truth 主控台所需的許可](#groundtruth-console-policy)
+ [使用 Amazon 增強版 AI (預覽) 主控台所需的許可](#amazon-augmented-ai-console-policy)

### 使用 Amazon SageMaker AI 主控台所需的許可
<a name="console-permissions"></a>

許可參考資料表列出了 Amazon SageMaker AI API 操作，以及顯示每個操作所需的許可。如需 Amazon SageMaker AI API 操作的詳細資訊，請參閱 [Amazon SageMaker AI API 許可：動作、許可與資源參考](api-permissions-reference.md)。

若要使用 Amazon SageMaker AI 主控台，您需要授予其他動作的許可。特別是主控台需要允許 `ec2` 動作的許可才可顯示子網路、VPC 和安全群組。或者，主控台需要許可來建立任務的*執行角色*，例如 `CreateNotebook`、`CreateTrainingJob` 和 `CreateModel`。使用以下許可政策來授予這些許可：

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
          "Sid": "SageMakerApis",
          "Effect": "Allow",
          "Action": [
            "sagemaker:*"
          ],
          "Resource": "*"
        },
        {
          "Sid": "VpcConfigurationForCreateForms",
          "Effect": "Allow",
          "Action": [
            "ec2:DescribeVpcs",
            "ec2:DescribeSubnets",
            "ec2:DescribeSecurityGroups"
          ],
          "Resource": "*"
        },
        {
            "Sid":"KmsKeysForCreateForms",
            "Effect":"Allow",
            "Action":[
              "kms:DescribeKey",
              "kms:ListAliases"
            ],
            "Resource":"*"
        },
        {
          "Sid": "AccessAwsMarketplaceSubscriptions",
          "Effect": "Allow",
          "Action": [
            "aws-marketplace:ViewSubscriptions"
          ],
          "Resource": "*"
        },
        {
          "Effect": "Allow",
          "Action": [
            "codecommit:BatchGetRepositories",
            "codecommit:CreateRepository",
            "codecommit:GetRepository",
            "codecommit:ListRepositories",
            "codecommit:ListBranches",
            "secretsmanager:CreateSecret",
            "secretsmanager:DescribeSecret",
            "secretsmanager:ListSecrets"
          ],
          "Resource": "*"
        },
        {
          "Sid":"ListAndCreateExecutionRoles",
          "Effect":"Allow",
          "Action":[
            "iam:ListRoles",
            "iam:CreateRole",
            "iam:CreatePolicy",
            "iam:AttachRolePolicy"
          ],
          "Resource":"*"
        },
        {
          "Sid": "DescribeECRMetaData",
          "Effect": "Allow",
          "Action": [
              "ecr:Describe*"
          ],
          "Resource": "*"
        },
        {
          "Sid": "PassRoleForExecutionRoles",
          "Effect": "Allow",
          "Action": [
            "iam:PassRole"
          ],
          "Resource": "*",
          "Condition": {
            "StringEquals": {
                "iam:PassedToService": "sagemaker.amazonaws.com"
            }
          }
        }
    ]
}
```

------



### 使用 Amazon SageMaker Ground Truth 主控台所需的許可
<a name="groundtruth-console-policy"></a>

若要使用 Amazon SageMaker Ground Truth 主控台，您需要授予其他資源的許可。具體而言，主控台需要：
+  AWS Marketplace 以檢視訂閱、
+ Amazon Cognito 操作的許可來管理私有人力
+ Amazon S3 動作的許可，用於存取輸入和輸出檔案
+ AWS Lambda 列出和叫用 函數的動作

使用以下許可政策來授予這些許可：

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "GroundTruthConsole",
            "Effect": "Allow",
            "Action": [
                "aws-marketplace:ViewSubscriptions",

                "cognito-idp:AdminAddUserToGroup",
                "cognito-idp:AdminCreateUser",
                "cognito-idp:AdminDeleteUser",
                "cognito-idp:AdminDisableUser",
                "cognito-idp:AdminEnableUser",
                "cognito-idp:AdminRemoveUserFromGroup",
                "cognito-idp:CreateGroup",
                "cognito-idp:CreateUserPool",
                "cognito-idp:CreateUserPoolClient",
                "cognito-idp:CreateUserPoolDomain",
                "cognito-idp:DescribeUserPool",
                "cognito-idp:DescribeUserPoolClient",
                "cognito-idp:ListGroups",
                "cognito-idp:ListIdentityProviders",
                "cognito-idp:ListUsers",
                "cognito-idp:ListUsersInGroup",
                "cognito-idp:ListUserPoolClients",
                "cognito-idp:ListUserPools",
                "cognito-idp:UpdateUserPool",
                "cognito-idp:UpdateUserPoolClient",

                "groundtruthlabeling:DescribeConsoleJob",
                "groundtruthlabeling:ListDatasetObjects",
                "groundtruthlabeling:RunGenerateManifestByCrawlingJob",

                "lambda:InvokeFunction",
                "lambda:ListFunctions",

                "s3:GetObject",
                "s3:PutObject"
            ],
            "Resource": "*"
        }
    ]
}
```

------

### 使用 Amazon 增強版 AI (預覽) 主控台所需的許可
<a name="amazon-augmented-ai-console-policy"></a>

若要使用增強版 AI 主控台，您需要授予其他資源的許可。使用以下許可政策來授予這些許可：

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "sagemaker:*Algorithm",
                "sagemaker:*Algorithms",
                "sagemaker:*App",
                "sagemaker:*Apps",
                "sagemaker:*AutoMLJob",
                "sagemaker:*AutoMLJobs",
                "sagemaker:*CodeRepositories",
                "sagemaker:*CodeRepository",
                "sagemaker:*CompilationJob",
                "sagemaker:*CompilationJobs",
                "sagemaker:*Endpoint",
                "sagemaker:*EndpointConfig",
                "sagemaker:*EndpointConfigs",
                "sagemaker:*EndpointWeightsAndCapacities",
                "sagemaker:*Endpoints",
                "sagemaker:*Experiment",
                "sagemaker:*Experiments",
                "sagemaker:*FlowDefinitions",
                "sagemaker:*HumanLoop",
                "sagemaker:*HumanLoops",
                "sagemaker:*HumanTaskUi",
                "sagemaker:*HumanTaskUis",
                "sagemaker:*HyperParameterTuningJob",
                "sagemaker:*HyperParameterTuningJobs",
                "sagemaker:*LabelingJob",
                "sagemaker:*LabelingJobs",
                "sagemaker:*Metrics",
                "sagemaker:*Model",
                "sagemaker:*ModelPackage",
                "sagemaker:*ModelPackages",
                "sagemaker:*Models",
                "sagemaker:*MonitoringExecutions",
                "sagemaker:*MonitoringSchedule",
                "sagemaker:*MonitoringSchedules",
                "sagemaker:*NotebookInstance",
                "sagemaker:*NotebookInstanceLifecycleConfig",
                "sagemaker:*NotebookInstanceLifecycleConfigs",
                "sagemaker:*NotebookInstanceUrl",
                "sagemaker:*NotebookInstances",
                "sagemaker:*ProcessingJob",
                "sagemaker:*ProcessingJobs",
                "sagemaker:*RenderUiTemplate",
                "sagemaker:*Search",
                "sagemaker:*SearchSuggestions",
                "sagemaker:*Tags",
                "sagemaker:*TrainingJob",
                "sagemaker:*TrainingJobs",
                "sagemaker:*TransformJob",
                "sagemaker:*TransformJobs",
                "sagemaker:*Trial",
                "sagemaker:*TrialComponent",
                "sagemaker:*TrialComponents",
                "sagemaker:*Trials",
                "sagemaker:*Workteam",
                "sagemaker:*Workteams"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "sagemaker:*FlowDefinition"
            ],
            "Resource": "*",
            "Condition": {
                "StringEqualsIfExists": {
                    "sagemaker:WorkteamType": [
                        "private-crowd",
                        "vendor-crowd"
                    ]
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "application-autoscaling:DeleteScalingPolicy",
                "application-autoscaling:DeleteScheduledAction",
                "application-autoscaling:DeregisterScalableTarget",
                "application-autoscaling:DescribeScalableTargets",
                "application-autoscaling:DescribeScalingActivities",
                "application-autoscaling:DescribeScalingPolicies",
                "application-autoscaling:DescribeScheduledActions",
                "application-autoscaling:PutScalingPolicy",
                "application-autoscaling:PutScheduledAction",
                "application-autoscaling:RegisterScalableTarget",
                "aws-marketplace:ViewSubscriptions",
                "cloudwatch:DeleteAlarms",
                "cloudwatch:DescribeAlarms",
                "cloudwatch:GetMetricData",
                "cloudwatch:GetMetricStatistics",
                "cloudwatch:ListMetrics",
                "cloudwatch:PutMetricAlarm",
                "cloudwatch:PutMetricData",
                "codecommit:BatchGetRepositories",
                "codecommit:CreateRepository",
                "codecommit:GetRepository",
                "codecommit:ListBranches",
                "codecommit:ListRepositories",
                "cognito-idp:AdminAddUserToGroup",
                "cognito-idp:AdminCreateUser",
                "cognito-idp:AdminDeleteUser",
                "cognito-idp:AdminDisableUser",
                "cognito-idp:AdminEnableUser",
                "cognito-idp:AdminRemoveUserFromGroup",
                "cognito-idp:CreateGroup",
                "cognito-idp:CreateUserPool",
                "cognito-idp:CreateUserPoolClient",
                "cognito-idp:CreateUserPoolDomain",
                "cognito-idp:DescribeUserPool",
                "cognito-idp:DescribeUserPoolClient",
                "cognito-idp:ListGroups",
                "cognito-idp:ListIdentityProviders",
                "cognito-idp:ListUserPoolClients",
                "cognito-idp:ListUserPools",
                "cognito-idp:ListUsers",
                "cognito-idp:ListUsersInGroup",
                "cognito-idp:UpdateUserPool",
                "cognito-idp:UpdateUserPoolClient",
                "ec2:CreateNetworkInterface",
                "ec2:CreateNetworkInterfacePermission",
                "ec2:CreateVpcEndpoint",
                "ec2:DeleteNetworkInterface",
                "ec2:DeleteNetworkInterfacePermission",
                "ec2:DescribeDhcpOptions",
                "ec2:DescribeNetworkInterfaces",
                "ec2:DescribeRouteTables",
                "ec2:DescribeSecurityGroups",
                "ec2:DescribeSubnets",
                "ec2:DescribeVpcEndpoints",
                "ec2:DescribeVpcs",
                "ecr:BatchCheckLayerAvailability",
                "ecr:BatchGetImage",
                "ecr:CreateRepository",
                "ecr:Describe*",
                "ecr:GetAuthorizationToken",
                "ecr:GetDownloadUrlForLayer",
                "elasticfilesystem:DescribeFileSystems",
                "elasticfilesystem:DescribeMountTargets",
                "fsx:DescribeFileSystems",
                "glue:CreateJob",
                "glue:DeleteJob",
                "glue:GetJob",
                "glue:GetJobRun",
                "glue:GetJobRuns",
                "glue:GetJobs",
                "glue:ResetJobBookmark",
                "glue:StartJobRun",
                "glue:UpdateJob",
                "groundtruthlabeling:*",
                "iam:ListRoles",
                "kms:DescribeKey",
                "kms:ListAliases",
                "lambda:ListFunctions",
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:DescribeLogGroups",
                "logs:DescribeLogStreams",
                "logs:GetLogEvents",
                "logs:PutLogEvents",
                "sns:ListTopics"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogDelivery",
                "logs:DeleteLogDelivery",
                "logs:DescribeResourcePolicies",
                "logs:GetLogDelivery",
                "logs:ListLogDeliveries",
                "logs:PutResourcePolicy",
                "logs:UpdateLogDelivery"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "ecr:SetRepositoryPolicy",
                "ecr:CompleteLayerUpload",
                "ecr:BatchDeleteImage",
                "ecr:UploadLayerPart",
                "ecr:DeleteRepositoryPolicy",
                "ecr:InitiateLayerUpload",
                "ecr:DeleteRepository",
                "ecr:PutImage"
            ],
            "Resource": "arn:aws:ecr:*:*:repository/*sagemaker*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "codecommit:GitPull",
                "codecommit:GitPush"
            ],
            "Resource": [
                "arn:aws:codecommit:*:*:*sagemaker*",
                "arn:aws:codecommit:*:*:*SageMaker*",
                "arn:aws:codecommit:*:*:*Sagemaker*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "secretsmanager:ListSecrets"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "secretsmanager:DescribeSecret",
                "secretsmanager:GetSecretValue",
                "secretsmanager:CreateSecret"
            ],
            "Resource": [
                "arn:aws:secretsmanager:*:*:secret:AmazonSageMaker-*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "secretsmanager:DescribeSecret",
                "secretsmanager:GetSecretValue"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "secretsmanager:ResourceTag/SageMaker": "true"
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "robomaker:CreateSimulationApplication",
                "robomaker:DescribeSimulationApplication",
                "robomaker:DeleteSimulationApplication"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "robomaker:CreateSimulationJob",
                "robomaker:DescribeSimulationJob",
                "robomaker:CancelSimulationJob"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:PutObject",
                "s3:DeleteObject",
                "s3:AbortMultipartUpload",
                "s3:GetBucketCors",
                "s3:PutBucketCors"
            ],
            "Resource": [
                "arn:aws:s3:::*SageMaker*",
                "arn:aws:s3:::*Sagemaker*",
                "arn:aws:s3:::*sagemaker*",
                "arn:aws:s3:::*aws-glue*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:CreateBucket",
                "s3:GetBucketLocation",
                "s3:ListBucket",
                "s3:ListAllMyBuckets"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject"
            ],
            "Resource": "*",
            "Condition": {
                "StringEqualsIgnoreCase": {
                    "s3:ExistingObjectTag/SageMaker": "true"
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "lambda:InvokeFunction"
            ],
            "Resource": [
                "arn:aws:lambda:*:*:function:*SageMaker*",
                "arn:aws:lambda:*:*:function:*sagemaker*",
                "arn:aws:lambda:*:*:function:*Sagemaker*",
                "arn:aws:lambda:*:*:function:*LabelingFunction*"
            ]
        },
        {
            "Action": "iam:CreateServiceLinkedRole",
            "Effect": "Allow",
            "Resource": "arn:aws:iam::*:role/aws-service-role/sagemaker.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_SageMakerEndpoint",
            "Condition": {
                "StringLike": {
                    "iam:AWSServiceName": "sagemaker.application-autoscaling.amazonaws.com"
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": "iam:CreateServiceLinkedRole",
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "iam:AWSServiceName": "robomaker.amazonaws.com"
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "sns:Subscribe",
                "sns:CreateTopic"
            ],
            "Resource": [
                "arn:aws:sns:*:*:*SageMaker*",
                "arn:aws:sns:*:*:*Sagemaker*",
                "arn:aws:sns:*:*:*sagemaker*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iam:PassRole"
            ],
            "Resource": "arn:aws:iam::*:role/*",
            "Condition": {
                "StringEquals": {
                    "iam:PassedToService": [
                        "sagemaker.amazonaws.com",
                        "glue.amazonaws.com",
                        "robomaker.amazonaws.com",
                        "states.amazonaws.com"
                    ]
                }
            }
        }
    ]
}
```

------

## 允許使用者檢視他們自己的許可
<a name="security_iam_id-based-policy-examples-view-own-permissions"></a>

此範例會示範如何建立政策，允許 IAM 使用者檢視附加到他們使用者身分的內嵌及受管政策。此政策包含在主控台或使用 或 AWS CLI AWS API 以程式設計方式完成此動作的許可。

```
{
    "Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "ViewOwnUserInfo",
            "Effect": "Allow",
            "Action": [
                "iam:GetUserPolicy",
                "iam:ListGroupsForUser",
                "iam:ListAttachedUserPolicies",
                "iam:ListUserPolicies",
                "iam:GetUser"
            ],
            "Resource": ["arn:aws:iam::*:user/${aws:username}"]
        },
        {
            "Sid": "NavigateInConsole",
            "Effect": "Allow",
            "Action": [
                "iam:GetGroupPolicy",
                "iam:GetPolicyVersion",
                "iam:GetPolicy",
                "iam:ListAttachedGroupPolicies",
                "iam:ListGroupPolicies",
                "iam:ListPolicyVersions",
                "iam:ListPolicies",
                "iam:ListUsers"
            ],
            "Resource": "*"
        }
    ]
}
```

## 使用條件金鑰控制 SageMaker AI 資源的建立
<a name="sagemaker-condition-examples"></a>

使用 SageMaker AI 特定的條件金鑰，控制精細存取以允許建立 SageMaker AI 資源。如需有關在 IAM 政策中使用條件金鑰的相關資訊，請參閱 *IAM 使用者指南*中的 [IAM JSON 政策元素：條件](https://docs.aws.amazon.com/service-authorization/latest/reference/reference_policies_elements_condition.html)。

條件金鑰、相關 API 動作和相關文件的連結列示在《服務授權參考》**中的 [SageMaker AI 的條件金鑰](https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonsagemaker.html#amazonsagemaker-policy-keys)。

以下範例展示如何使用 SageMaker AI 條件金鑰來控制存取。

**Topics**
+ [使用檔案系統條件金鑰控制對 SageMaker AI 資源的存取](#access-fs-condition-keys)
+ [限制訓練只能使用特定 VPC](#sagemaker-condition-vpc)
+ [限制存取 Ground Truth 標記任務籤和 Amazon A2I 人工檢閱工作流程的人力類型](#sagemaker-condition-keys-labeling)
+ [強制加密輸入資料](#sagemaker-condition-kms)
+ [對訓練任務強制執行網路隔離](#sagemaker-condition-isolation)
+ [對訓練任務強制執行特定執行個體類型](#sagemaker-condition-instance)
+ [強制停用網際網路存取和根存取以建立筆記本執行個體](#sagemaker-condition-nbi-lockdown)

### 使用檔案系統條件金鑰控制對 SageMaker AI 資源的存取
<a name="access-fs-condition-keys"></a>

SageMaker AI 訓練提供安全基礎設施，讓訓練演算法可在其中執行，但對於某些情況，您可能想要提高防禦深度。例如，您可以將在演算法中執行不受信任程式碼的風險降到最低，或者在組織中具有特定的安全性規定。對於這些案例，您可以在 IAM 政策的條件元素中使用服務特定條件金鑰，將使用者縮小到以下範圍：
+ 特定檔案系統
+ 目錄
+ 存取模式 (讀寫、唯讀)
+ 安全群組

**Topics**
+ [限制 IAM 使用者只能使用特定目錄和存取模式](#access-fs-condition-keys-ex-dirs)
+ [限制使用者只能使用特定檔案系統](#access-fs-condition-keys-ex-fs)

#### 限制 IAM 使用者只能使用特定目錄和存取模式
<a name="access-fs-condition-keys-ex-dirs"></a>

以下政策限制使用者對 EFS 檔案系統的 `/sagemaker/xgboost-dm/train` 和 `/sagemaker/xgboost-dm/validation` 目錄只有 `ro` (唯讀) AccessMode：

**注意**  
允許目錄時，訓練演算法也可以存取其所有子目錄。POSIX 權限會遭到忽略。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AccessToElasticFileSystem",
            "Effect": "Allow",
            "Action": [
                "sagemaker:CreateTrainingJob",
                "sagemaker:CreateHyperParameterTuningJob"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "sagemaker:FileSystemId": "fs-12345678",
                    "sagemaker:FileSystemAccessMode": "ro",
                    "sagemaker:FileSystemType": "EFS",
                    "sagemaker:FileSystemDirectoryPath": "/sagemaker/xgboost-dm/train"
                }
            }
        },
        {
            "Sid": "AccessToElasticFileSystemValidation",
            "Effect": "Allow",
            "Action": [
                "sagemaker:CreateTrainingJob",
                "sagemaker:CreateHyperParameterTuningJob"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "sagemaker:FileSystemId": "fs-12345678",
                    "sagemaker:FileSystemAccessMode": "ro",
                    "sagemaker:FileSystemType": "EFS",
                    "sagemaker:FileSystemDirectoryPath": "/sagemaker/xgboost-dm/validation"
                }
            }
        }
    ]
}
```

------

#### 限制使用者只能使用特定檔案系統
<a name="access-fs-condition-keys-ex-fs"></a>

若要防止惡意演算法利用使用者空間用戶端，直接以您的帳戶存取任何檔案系統，您可以限制網路流量。若要限制此流量，僅允許從特定安全群組傳入。在下列範例中， 使用者只能使用指定的安全群組來存取檔案系統：

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AccessToLustreFileSystem",
            "Effect": "Allow",
            "Action": [
                "sagemaker:CreateTrainingJob",
                "sagemaker:CreateHyperParameterTuningJob"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "sagemaker:FileSystemId": "fs-12345678",
                    "sagemaker:FileSystemAccessMode": "ro",
                    "sagemaker:FileSystemType": "FSxLustre",
                    "sagemaker:FileSystemDirectoryPath": "/fsx/sagemaker/xgboost/train"
                },
                "ForAllValues:StringEquals": {
                    "sagemaker:VpcSecurityGroupIds": [
                        "sg-12345678"
                    ]
                }
            }
        }
    ]
}
```

------

此範例可以限制演算法只能使用特定檔案系統。不過，它不會防止演算法利用使用者空間用戶端存取該檔案系統內的任何目錄。若要緩解此情況，您可以：
+ 確定檔案系統只包含您信任 使用者可以存取的資料
+ 建立 IAM 角色，限制您的使用者只能利用已核准的 ECR 儲存庫中的演算法啟動訓練任務

如需如何使用角色搭配 SageMaker AI 的詳細資訊，請參閱 [SageMaker AI 角色](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-roles.html)。

### 限制訓練只能使用特定 VPC
<a name="sagemaker-condition-vpc"></a>

限制 AWS 使用者從 Amazon VPC 內建立訓練任務。在 VPC 內建立訓練任務時，請使用 VPC 流程日誌，來監控進出訓練叢集的所有流量。有關使用 VPC 流日誌的資訊，請參閱 [VPC 流日誌](https://docs.aws.amazon.com/vpc/latest/userguide/flow-logs.html)在 *Amazon Virtual Private Cloud 使用者指南*。

以下政策強制使用者從 VPC 內呼叫 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html) 來建立訓練任務：

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowFromVpc",
            "Effect": "Allow",
            "Action": [
                "sagemaker:CreateTrainingJob",
                "sagemaker:CreateHyperParameterTuningJob"
            ],
            "Resource": "*",
            "Condition": {
                "ForAllValues:StringEquals": {
                    "sagemaker:VpcSubnets": ["subnet-a1234"],
                    "sagemaker:VpcSecurityGroupIds": ["sg12345", "sg-67890"]
                },
                "Null": {
                    "sagemaker:VpcSubnets": "false",
                    "sagemaker:VpcSecurityGroupIds": "false"
                }
            }
        }

    ]
}
```

------

### 限制存取 Ground Truth 標記任務籤和 Amazon A2I 人工檢閱工作流程的人力類型
<a name="sagemaker-condition-keys-labeling"></a>

Amazon SageMaker Ground Truth 和 Amazon 增強版 AI 工作團隊屬於三種[人力類型](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-workforce-management.html)之一：
+ 公用 (具有 Amazon Mechanical Turk)
+ private
+ 廠商

您可以限制使用者只能使用其中一種類型或工作團隊 ARN 來存取特定的工作團隊。若要這樣做，請使用 `sagemaker:WorkteamType` 和/或 `sagemaker:WorkteamArn` 條件金鑰。若是 `sagemaker:WorkteamType` 條件金鑰，請使用[字串條件運算子](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition_operators.html#Conditions_String)。若是 `sagemaker:WorkteamArn` 條件金鑰，請使用 [Amazon Resource Name (ARN) 條件運算子](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition_operators.html#Conditions_ARN)。如果使用者嘗試利用有限的工作團隊來建立標記任務，SageMaker AI 會傳回存取遭拒錯誤。

下列政策顯示不同的方式，使用 `sagemaker:WorkteamType` 及 `sagemaker:WorkteamArn` 條件金鑰搭配適當的條件運算子及有效的條件值。

下列範例將 `sagemaker:WorkteamType` 條件金鑰與 `StringEquals` 條件運算子搭配使用，以限制對公有工作團隊的存取權。它可接受以下格式的條件值：`{{workforcetype}}-crowd`，其中 {{workforcetype}} 可以等於 `public`、`private` 或 `vendor`。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "RestrictWorkteamType",
            "Effect": "Deny",
            "Action": "sagemaker:CreateLabelingJob",
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "sagemaker:WorkteamType": "{{public-crowd}}"
                }
            }
        }
    ]
}
```

------

下列政策示範如何使用 `sagemaker:WorkteamArn` 條件金鑰來限制對公有工作團隊的存取權。第一個政策示範如何將其與工作團隊 ARN 的有效 IAM Regex 變體及`ArnLike`條件運算子搭配使用。第二個政策示範如何將其與 `ArnEquals` 條件運算子及工作團隊 ARN 搭配使用。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "RestrictWorkteamType",
            "Effect": "Deny",
            "Action": "sagemaker:CreateLabelingJob",
            "Resource": "*",
            "Condition": {
                "ArnLike": {
                    "sagemaker:WorkteamArn": "arn:aws:sagemaker:*:*:workteam/public-crowd/*"
                }
            }
        }
    ]
}
```

------

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "RestrictWorkteamType",
            "Effect": "Deny",
            "Action": "sagemaker:CreateLabelingJob",
            "Resource": "*",
            "Condition": {
                "ArnEquals": {
                    "sagemaker:WorkteamArn": "{{arn:aws:sagemaker:us-west-2:394669845002:workteam/public-crowd/default}}"
                }
            }
        }
    ]
}
```

------

 

### 強制加密輸入資料
<a name="sagemaker-condition-kms"></a>

下列政策會限制使用者指定 AWS KMS 金鑰，以在建立時，使用 `sagemaker:VolumeKmsKey`條件金鑰加密輸入資料：
+ 訓練
+ 超參數調校
+ 標籤工作

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "EnforceEncryption",
            "Effect": "Allow",
            "Action": [
                "sagemaker:CreateTrainingJob",
                "sagemaker:CreateHyperParameterTuningJob",
                "sagemaker:CreateLabelingJob",
                "sagemaker:CreateFlowDefinition"
            ],
            "Resource": "*",
            "Condition": {
                "ArnEquals": {
                    "sagemaker:VolumeKmsKey": "arn:aws:kms:{{us-east-1}}:{{111122223333}}:key/1234abcd-12ab-34cd-56ef-1234567890ab"
                }
            }
        }

     ]
}
```

------

### 對訓練任務強制執行網路隔離
<a name="sagemaker-condition-isolation"></a>

下列政策限制使用者在建立訓練任務時，只能使用 `sagemaker:NetworkIsolation` 條件金鑰來啟用網路隔離：

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "EnforceIsolation",
            "Effect": "Allow",
            "Action": [
                "sagemaker:CreateTrainingJob",
                "sagemaker:CreateHyperParameterTuningJob"
            ],
            "Resource": "*",
            "Condition": {
                "Bool": {
                    "sagemaker:NetworkIsolation": "true"
                }
            }
        }
    ]
}
```

------

### 對訓練任務強制執行特定執行個體類型
<a name="sagemaker-condition-instance"></a>

下列政策限制使用者在建立訓練任務時，只能使用 `sagemaker:InstanceTypes` 條件金鑰來使用特定的執行個體類型：

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "EnforceInstanceType",
            "Effect": "Allow",
            "Action": [
                "sagemaker:CreateTrainingJob",
                "sagemaker:CreateHyperParameterTuningJob"
            ],
            "Resource": "*",
            "Condition": {
                "ForAllValues:StringLike": {
                    "sagemaker:InstanceTypes": ["ml.c5.*"]
                }
            }
        }

     ]
}
```

------

### 強制停用網際網路存取和根存取以建立筆記本執行個體
<a name="sagemaker-condition-nbi-lockdown"></a>

您可以同時對筆記本執行個體停用網際網路存取和根存取，以協助讓它們更加安全。如需控制對筆記本執行個體的根存取相關資訊，請參閱[控制 SageMaker 筆記本執行個體的根存取權](nbi-root-access.md)。如需停用筆記本執行個體的網際網路存取相關資訊，請參閱[將 VPC 中的筆記本執行個體連接外部資源](appendix-notebook-and-internet-access.md)。

下列政策要求使用者在建立執行個體時停用網路存取，以及在建立或更新筆記本執行個體時停用根存取。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "LockDownCreateNotebookInstance",
            "Effect": "Allow",
            "Action": [
                "sagemaker:CreateNotebookInstance"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "sagemaker:DirectInternetAccess": "Disabled",
                    "sagemaker:RootAccess": "Disabled"
                },
                "Null": {
                  "sagemaker:VpcSubnets": "false",
                  "sagemaker:VpcSecurityGroupIds": "false"
                }
            }
        },
        {
            "Sid": "LockDownUpdateNotebookInstance",
            "Effect": "Allow",
            "Action": [
                "sagemaker:UpdateNotebookInstance"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "sagemaker:RootAccess": "Disabled"
                }
            }
        }
     ]
}
```

------

## 使用身分型政策控制對 SageMaker AI API 的存取
<a name="api-access-policy"></a>

若要控制對 SageMaker AI API 呼叫的存取，以及對 SageMaker AI 託管端點的呼叫，請使用身分型 IAM 政策。

**Topics**
+ [限制只有來自您的 VPC 內的呼叫才能存取 SageMaker AI API 和執行時期](#api-access-policy-vpc)

### 限制只有來自您的 VPC 內的呼叫才能存取 SageMaker AI API 和執行時期
<a name="api-access-policy-vpc"></a>

如果您在 VPC 中設定介面端點，VPC 外的個人也可以透過網際網路連線到 SageMaker AI API 和執行時期。若要避免這種情況，請連接 IAM 政策，限制只有來自 VPC 內的呼叫才能存取。這些呼叫必須限制為可以存取您的 SageMaker AI 資源的所有使用者和群組。如需為 SageMaker AI API 和執行時期建立 VPC 介面端點的相關資訊，請參閱[在您的 VPC 中連線到 SageMaker AI](interface-vpc-endpoint.md)。

**重要**  
如果您套用類似下列其中之一的 IAM 政策，使用者將無法透過主控台存取指定的 SageMaker AI API。

若要限制只能存取從您的 VPC 內建立的連線，請建立 AWS Identity and Access Management 政策來限制存取。此存取必須僅限於來自 VPC 內的呼叫。然後，將該政策新增至用來存取 SageMaker AI API 或執行時期的每個 AWS Identity and Access Management 使用者、群組或角色。

**注意**  
此政策僅允許向建立介面端點之子網路中的呼叫者建立連線。

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

****  

```
{
    "Id": "api-example-1",
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "EnableAPIAccess",
            "Effect": "Allow",
            "Action": [
                "sagemaker:*"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "aws:SourceVpc": "vpc-111bbaaa"
                }
            }
        }
    ]
}
```

------

若要僅限使用介面端點建立的呼叫才能存取 API，請使用 `aws:SourceVpce` 條件金鑰，而不是 `aws:SourceVpc`：

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

****  

```
{
    "Id": "api-example-1",
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "EnableAPIAccess",
            "Effect": "Allow",
            "Action": [
                "sagemaker:CreatePresignedNotebookInstanceUrl"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "aws:sourceVpce": [
                        "vpce-111bbccc",
                        "vpce-111bbddd"
                    ]
                }
            }
        }
    ]
}
```

------

## 限制只能透過 IP 位址存取 SageMaker AI API 和執行時期呼叫
<a name="api-ip-filter"></a>

您可以僅允許從您指定的清單中的 IP 位址存取 SageMaker AI API 呼叫和執行時期調用。若要這樣做，請建立拒絕存取 API 的 IAM 政策，除非呼叫來自清單中的 IP 位址。然後將該政策連接到用於存取 API 或執行時間的每個 AWS Identity and Access Management 使用者、群組或角色。如需有關建立和編輯 IAM 政策的資訊，請參閱 *AWS Identity and Access Management 使用者指南*中的[建立 IAM 政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html)。

若要指定可存取 API 呼叫的 IP 位址清單，請使用：
+ `IpAddress` 條件運算子
+ `aws:SourceIP` 條件內容金鑰

如需有關條件運算子的相關資訊，請參閱 *AWS Identity and Access Management 使用者指南*中的 [IAM JSON 政策元素：條件運算子](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition_operators.html)。如需 IAM 條件內容金鑰的資訊，請參閱 [AWS 全域條件金鑰](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html)。

例如，以下政策只允許從範圍 `192.0.2.0`-`192.0.2.255` 和 `203.0.113.0`-`203.0.113.255` 的 IP 地址存取 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html)：

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [

        {
            "Effect": "Allow",
            "Action": "sagemaker:CreateTrainingJob",
            "Resource": "*",
            "Condition": {
                "IpAddress": {
                    "aws:SourceIp": [
                        "192.0.2.0/24",
                        "203.0.113.0/24"
                    ]
                }
            }
        }
    ]
}
```

------

## 依 IP 位址限制對筆記本執行個體的存取
<a name="nbi-ip-filter"></a>

您可以僅允許從您指定的清單中的 IP 位址存取筆記本執行個體。若要這樣做，請建立拒絕存取 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreatePresignedNotebookInstanceUrl.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreatePresignedNotebookInstanceUrl.html) 的 IAM 政策，除非呼叫來自清單中的 IP 位址。然後，將此政策連接到用於存取筆記本執行個體的每個 AWS Identity and Access Management 使用者、群組或角色。如需有關建立和編輯 IAM 政策的資訊，請參閱 *AWS Identity and Access Management 使用者指南*中的[建立 IAM 政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html)。

若要指定您想要其可存取筆記本執行個體的 IP 位址清單，請使用：
+ `IpAddress` 條件運算子
+ `aws:SourceIP` 條件內容金鑰

如需有關條件運算子的相關資訊，請參閱 *AWS Identity and Access Management 使用者指南*中的 [IAM JSON 政策元素：條件運算子](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition_operators.html)。如需 IAM 條件內容金鑰的資訊，請參閱 [AWS 全域條件金鑰](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html)。

例如，以下政策只允許從範圍在 `192.0.2.0`-`192.0.2.255` 和 `203.0.113.0`-`203.0.113.255` 的 IP 地址存取筆記本執行個體：

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [

        {
            "Effect": "Allow",
            "Action": "sagemaker:CreatePresignedNotebookInstanceUrl",
            "Resource": "*",
            "Condition": {
                "IpAddress": {
                    "aws:SourceIp": [
                        "192.0.2.0/24",
                        "203.0.113.0/24"
                    ]
                }
            }
        }
    ]
}
```

------

此政策限制存取對 `CreatePresignedNotebookInstanceUrl` 的呼叫和該呼叫傳回的 URL。政策也會限制存取在主控台中開啟筆記本執行個體。它會針對嘗試連線至筆記本執行個體的每個 HTTP 請求和 WebSocket 框架強制執行。

**注意**  
[透過 VPC 介面端點連線到 SageMaker AI](https://docs.aws.amazon.com/sagemaker/latest/dg/interface-vpc-endpoint.html) 時，使用此方法依 IP 位址篩選不相容。如需透過 VPC 介面端點連線時限制存取筆記本執行個體的資訊，請參閱[透過 VPC 介面端點連線至筆記本執行個體](notebook-interface-endpoint.md)。

## 使用標籤控制對 SageMaker AI 資源的存取
<a name="access-tag-policy"></a>

在 IAM 政策內指定標籤，以控制對 SageMaker AI 資源群組的存取。使用標籤以進行屬性型存取控制 (ABAC)。使用標籤可協助您將資源的存取分割給特定使用者群組。您可以讓一個團隊存取一個資源群組，而另一個團隊可以存取另一組資源。您可以在 IAM 政策中提供`ResourceTag`條件，以便為每個群組提供存取權。

**注意**  
以標籤為基礎的政策不會限制以下 API 呼叫：  
DeleteImageVersion
DescribeImageVersion
ListAlgorithms
ListCodeRepositories
ListCompilationJobs
ListEndpointConfigs
ListEndpoints
ListFlowDefinitions
ListHumanTaskUis
ListHyperparameterTuningJobs
ListLabelingJobs
ListLabelingJobsForWorkteam
ListModelPackages
ListModels
ListNotebookInstanceLifecycleConfigs
ListNotebookInstances
ListSubscribedWorkteams
ListTags
ListProcessingJobs
ListTrainingJobs
ListTrainingJobsForHyperParameterTuningJob
ListTransformJobs
ListWorkteams
搜尋

一個簡單的範例可以幫助您了解如何使用標籤來分區資源。假設您已`DevTeam2`在 AWS 帳戶中定義兩個不同的 IAM 群組，名為 `DevTeam1`和 。您也建立了 10 個筆記本執行個體。您正在為一個專案使用 5 個筆記本執行個體。您正在為第二個專案使用另外 5 個筆記本執行個體。您可以提供 `DevTeam1` 在第一個專案使用的筆記本執行個體上進行 API 呼叫的許可。您可以提供 `DevTeam2` 用於第二個專案的筆記本執行個體上進行 API 呼叫。

下列程序提供一個簡單的範例，可協助您了解新增標籤的概念。您可以使用它來實現前段中說明的解決方案。

**控制對 API 呼叫的存取權 (範例)**

1. 使用鍵 `Project` 和值 `A` 在用於第一個專案的筆記本執行個體新增標籤。如需將標籤新增至 SageMaker AI 資源的相關資訊，請參閱 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AddTags.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AddTags.html)。

1. 使用鍵 `Project` 和值 `B` 在用於第二個專案的筆記本執行個體新增標籤。

1. 建立 IAM 政策，其中有一個 `ResourceTag` 條件，拒絕存取用於第二個專案的筆記本執行個體。然後，將該政策連接至 `DevTeam1`。以下範例政策拒絕任何筆記本執行個體上的所有 API 呼叫，該執行個體具有金鑰為 `Project` 且值為 `B` 的標籤。

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

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement": [
       {
         "Effect": "Allow",
         "Action": "sagemaker:*",
         "Resource": "*"
       },
       {
         "Effect": "Deny",
         "Action": "sagemaker:*",
         "Resource": "*",
         "Condition": {
           "StringEquals": {
             "sagemaker:ResourceTag/Project": "B"
           }
         }
       },
       {
         "Effect": "Deny",
         "Action": [
           "sagemaker:AddTags",
           "sagemaker:DeleteTags"
         ],
         "Resource": "*"
       }
     ]
   }
   ```

------

   有關建立 IAM 政策並將其附加到身分的資訊，請參閱 *AWS Identity and Access Management 使用者指南*中的 [控制對使用政策的存取](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_controlling.html)。

1. 建立 IAM 政策，其中有一個 `ResourceTag` 條件，拒絕存取用於第一個專案的筆記本執行個體。然後，將該政策連接至 `DevTeam2`。以下範例政策拒絕任何筆記本執行個體上的所有 API 呼叫，該執行個體具有金鑰為 `Project` 且值為 `A` 的標籤。

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

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement": [
       {
         "Effect": "Allow",
         "Action": "sagemaker:*",
         "Resource": "*"
       },
       {
         "Effect": "Deny",
         "Action": "sagemaker:*",
         "Resource": "*",
         "Condition": {
           "StringEquals": {
             "sagemaker:ResourceTag/Project": "A"
           }
         }
       },
       {
         "Effect": "Deny",
         "Action": [
           "sagemaker:AddTags",
           "sagemaker:DeleteTags"
         ],
         "Resource": "*"
       }
     ]
   }
   ```

------

## 提供標記 SageMaker AI 資源的許可
<a name="grant-tagging-permissions"></a>

[標籤](https://docs.aws.amazon.com/whitepapers/latest/tagging-best-practices/what-are-tags.html)是您可以連接到特定 AWS 資源的中繼資料標籤。標籤由鍵值對組成，其中提供靈活的方式，為各種[標記使用案例](https://docs.aws.amazon.com/whitepapers/latest/tagging-best-practices/tagging-use-cases.html)註釋具有中繼資料屬性的資源，包括：
+ search
+ 安全
+ [成本歸因](https://docs.aws.amazon.com/whitepapers/latest/sagemaker-studio-admin-best-practices/cost-attribution.html)
+ 存取控制
+  自動化

它們可用於許可和政策、服務配額，以及與其他 AWS 服務的整合。標籤可以在建立資源時由使用者定義或 AWS 產生。這取決於使用者是手動指定自訂標籤，還是 AWS 服務自動產生標籤。
+ SageMaker AI *中的使用者定義標籤*：使用者可以在使用 SageMaker SDKs、 AWS CLI CLI、SageMaker APIs、SageMaker AI 主控台或 CloudFormation 範本建立 SageMaker AI 資源時新增標籤。
**注意**  
如果稍後更新資源且變更或取代標籤值，則可以覆寫使用者定義的標籤。例如，使用 {Team: A} 建立的訓練任務可能未正確地更新，並重新標記為 {Team: B}。因此，可能未正確地指派允許的許可。因此，在允許使用者或群組新增標籤時應小心，因為他們可能會覆寫現有的標籤值。最佳實務是嚴格限制標籤許可範圍，並使用 IAM 條件來控制標記功能。
+ SageMaker AI 中*AWS 產生的標籤*：SageMaker AI 會自動標記其建立的特定資源。例如，Studio 和 Studio Classic 會自動將 `sagemaker:domain-arn` 標籤指派給他們建立的 SageMaker AI 資源。使用網域 ARN 標記新資源可以追蹤訓練任務、模型和端點等 SageMaker AI 資源的來源。為了進行更精細的控制和追蹤，新資源會收到額外標籤，例如：
  + `sagemaker:user-profile-arn` - 已建立資源的使用者設定檔 ARN。這允許追蹤特定使用者建立的資源。
  + `sagemaker:space-arn` - 已在其中建立資源的空間 ARN。這允許分組和隔離每個空間的資源。
**注意**  
AWS 產生的標籤無法由使用者變更。

如需標記 AWS 資源和最佳實務的一般資訊，請參閱[標記 AWS 資源](https://docs.aws.amazon.com/tag-editor/latest/userguide/tagging.html)。如需主要標記使用案例的相關資訊，請參閱[標記使用案例](https://docs.aws.amazon.com/whitepapers/latest/tagging-best-practices/tagging-use-cases.html)。

### 授予在建立 SageMaker AI 資源時新增標籤的許可
<a name="grant-tagging-permissions-with-create"></a>

您可以允許使用者 *（使用者定義的標籤*) 或 Studio 和 Studio Classic (*AWS 產生的標籤*) 在建立時在新的 SageMaker AI 資源上新增標籤。若要這樣做，其 IAM 許可必須同時包括下列許可：
+ 該資源類型的基礎 SageMaker AI 建立許可。
+ `sagemaker:AddTags` 許可。

例如，允許使用者建立 SageMaker 訓練任務並將其標記需要授予 `sagemaker:CreateTrainingJob` 和 `sagemaker:AddTags` 的許可。

**重要**  
允許 Amazon SageMaker Studio 或 Amazon SageMaker Studio Classic 建立 Amazon SageMaker AI 資源的自訂 IAM 政策也必須授予許可，才能將標籤新增至這些資源。需要將標籤新增至資源的許可，因為 Studio 和 Studio Classic 會自動標記它們建立的任何資源。如果 IAM 政策允許 Studio 和 Studio Classic 建立資源，但不允許標記，則在嘗試建立資源時可能會發生 "AccessDenied" 錯誤。  
提供許可來建立 SageMaker AI 資源的 [AWS Amazon SageMaker AI 的 受管政策](security-iam-awsmanpol.md) 已包含建立這些資源時新增標籤的許可。

管理員會將這些 IAM 許可連接到下列任一角色：
+ AWS 針對使用者定義的標籤指派給使用者的 IAM 角色
+ Studio 或 Studio Classic 針對 AWS 產生的標籤使用的執行角色

如需建立和套用自訂 IAM 政策的指示，請參閱[建立 IAM 政策 (主控台)](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create-console.html)。

**注意**  
您可以在 [SageMaker API 文件](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_Operations_Amazon_SageMaker_Service.html)中找到 SageMaker AI 資源建立操作的清單，方法是搜尋以 `Create` 開頭的動作。這些建立動作，例如 `CreateTrainingJob` 和 `CreateEndpoint`，是建立新 SageMaker AI 資源的操作。

**將標籤許可新增至特定建立動作 **

您可以將額外 IAM 政策連接至原始資源建立政策，以授予具有限制條件的 `sagemaker:AddTags` 許可。下列範例政策允許 `sagemaker:AddTags`，但限制它僅能執行特定 SageMaker AI 資源建立動作，例如 `CreateTrainingJob`。

```
{
  "Sid": "AllowAddTagsForCreateOperations",
  "Effect": "Allow",
  "Action": [
    "sagemaker:AddTags"
  ],
  "Resource": "*",
  "Condition": {
    "StringEquals": {
      "sagemaker:TaggingAction": "CreateTrainingJob"
    }
  }
}
```

政策條件限制 `sagemaker:AddTags` 與特定建立動作搭配使用。在此方法中，建立許可政策會保持不變，而額外政策會提供受限制的 `sagemaker:AddTags` 存取。條件會將綜合 `sagemaker:AddTags` 許可範圍縮小到需要標記的建立動作，以防止該許可。這會針對 `sagemaker:AddTags` 實作最低權限，方法是只允許其使用特定的 SageMaker AI 資源建立使用案例。

**範例：允許全域標籤許可，並將建立動作限制為網域**

在這個自訂 IAM 政策範例中，前兩個陳述式示範如何使用標籤來追蹤資源建立。它允許在所有資源上執行 `sagemaker:CreateModel` 動作，並在使用該動作時標記這些資源。第三個聲明示範如何使用標籤值來控制資源上的操作。在此情況下，它會防止建立任何以特定網域 ARN 標記的 SageMaker AI 資源，並根據標籤值限制存取。

尤其是：
+ 第一個聲明允許對任何資源 (`*`) 執行 `CreateModel` 動作。
+ 第二個聲明允許 `sagemaker:AddTags` 動作，但只有在 `sagemaker:TaggingAction` 條件金鑰等於 `CreateModel` 時才允許。這會限制 `sagemaker:AddTags` 動作只有在用來標記新建立的模型時才會執行。
+ 第三個聲明會拒絕任何資源 (`*`) 上的任何 SageMaker AI 建立動作 (`Create*`)，但前提是資源具有等於特定網域 ARN `{{domain-arn}}` 的標籤 `sagemaker:domain-arn`。

```
{
   "Statement":[
      {
         "Effect":"Allow",
         "Action":[
            "sagemaker:CreateModel"
         ],
         "Resource":"*"
      },
      {
         "Effect":"Allow",
         "Action":[
            "sagemaker:AddTags"
         ],
         "Resource":"*",
         "Condition":{
            "String":{
               "sagemaker:TaggingAction":[
                  "CreateModel"
               ]
            }
         }
      },
      {
         "Sid":"IsolateDomain",
         "Effect":"Deny",
         "Resource":"*",
         "Action":[
            "sagemaker:Create*"
         ],
         "Condition":{
            "StringEquals":{
               "aws:ResourceTag/sagemaker:domain-arn":"{{domain-arn}}"
            }
         }
      }
   ]
}
```

## 使用可見性條件限制對可搜尋資源的存取
<a name="limit-access-to-searchable-resources"></a>

使用可見性條件來限制使用者對 AWS 帳戶內特定標記資源的存取。您的使用者只能存取那些他們具有許可的資源。當您的使用者搜尋其資源時，他們可以將搜尋結果限制為特定資源。

您可能想要使用者只能查看與特定 Amazon SageMaker Studio 或 Amazon SageMaker Studio Classic 網域相關聯的資源，並與其互動。您可以使用可見性條件來限制其可以存取單一網域或多個網域。

```
{
    "Sid": "SageMakerApis",
    "Effect": "Allow",
    "Action": "sagemaker:Search", 
    "Resource": "*",
    "Condition": {
        "StringEquals": {
            "sagemaker:SearchVisibilityCondition/Tags.sagemaker:{{example-domain-arn}}/EqualsIfExists": "arn:aws:sagemaker:{{AWS 區域}}:{{111122223333}}:domain/{{example-domain-1}}",
            "sagemaker:SearchVisibilityCondition/Tags.sagemaker:{{example-domain-arn}}/EqualsIfExists": "arn:aws:sagemaker:{{AWS 區域}}:{{111122223333}}:domain/{{example-domain-2}}"
        }
    }
}
```

可見性條件的一般格式為 `"sagemaker:SearchVisibilityCondition/Tags.key": "value"`。您可以為任何標記的資源提供鍵值對。

```
{
   "MaxResults": number,
   "NextToken": "string",
   "Resource": "string", # Required Parameter
   "SearchExpression": { 
      "Filters": [ 
         { 
            "Name": "string",
            "Operator": "string",
            "Value": "string"
         }
      ],
      "NestedFilters": [ 
         { 
            "Filters": [ 
               { 
                  "Name": "string",
                  "Operator": "string",
                  "Value": "string"
               }
            ],
            "NestedPropertyName": "string"
         }
      ],
      "Operator": "string",
      "SubExpressions": [ 
         "SearchExpression"
      ]
   },
   "IsCrossAccount": "string",
   "VisibilityConditions" : [ List of conditions for visibility
         {"Key": "Tags.sagemaker:{{example-domain-arn}}", "Value": "arn:aws:sagemaker:{{AWS 區域}}:{{111122223333}}:domain/{{example-domain-1}}"},
         {"Key": "Tags.sagemaker:{{example-domain-arn}}", "Value": "arn:aws:sagemaker:{{AWS 區域}}:111122223333:domain/{{example-domain-2}}"}
]
   ],
   "SortBy": "string",
   "SortOrder": "string"
}
```

可見性條件會使用政策中指定的相同 `"sagemaker:SearchVisibilityCondition/Tags.key": "value"` 格式。您的使用者可以指定用於任何標記資源的鍵值對。

如果使用者在其[搜尋](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_Search.html)請求中包含 `VisibilityConditions` 參數，但套用至該使用者的存取政策未包含 `VisibilityConditions` 中指定的任何相符條件金鑰，則仍然允許 `Search` 請求且其將會執行。

如果未在使用者的[搜尋](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_Search.html) API 請求中指定 `VisibilityConditions` 參數，但套用至該使用者的存取政策包含與 `VisibilityConditions` 相關的條件金鑰，則會拒絕該使用者的 `Search` 請求。