

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

# 將 RStudio 支援新增至現有的網域
<a name="rstudio-add-existing"></a>

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

 如果您已透過 新增 RStudio 授權 AWS License Manager，則可以建立支援 RStudio on Amazon SageMaker SageMaker AI 網域。如果您現有的網域不支援 RStudio，您可以將 RStudio 支援新增至該網域，無需刪除並重新建立網域。  

 下列主題概述如何新增此支援。

## 先決條件
<a name="rstudio-add-existing-prerequisites"></a>

 您必須先完成下列步驟，然後才能更新目前的網域，以新增 RStudio on SageMaker AI 的支援。  
+  安裝與設定[AWS CLI 第 2 版](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) 
+  使用 IAM 憑證 設定 [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html#cli-configure-quickstart-config) 
+  請遵循[使用 AWS CLI搭配 RStudio 建立 SageMaker 網域](https://docs.aws.amazon.com/sagemaker/latest/dg/rstudio-create-cli.html#rstudio-create-cli-domainexecution)中的步驟，建立網域執行角色。RStudioServerPro 應用程式中需要這種網域層級的 IAM 角色。該角色需要 AWS License Manager 的存取權，才能驗證有效的 Posit Workbench 授權以及用於發佈伺服器日誌的 Amazon CloudWatch 日誌。  
+  AWS License Manager 遵循 RStudio 授權中的步驟，將您的 [RStudio 授權](https://docs.aws.amazon.com/sagemaker/latest/dg/rstudio-license.html)帶到 。
+  (選用) 如果您想要在 `VPCOnly` 模式下使用 RStudio，請完成[僅限 VPC 下 RStudio ](https://docs.aws.amazon.com/sagemaker/latest/dg/rstudio-network.html)中的步驟。
+  請確定網域中每個 [UserProfile](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateUserProfile.html) 所設定的安全群組都符合帳戶層級配額。在建立網域期間設定預設使用者設定檔時，您可以使用 [CreateDomain](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateDomain.html) API 的 `DefaultUserSettings` 參數新增 `SecurityGroups`，這些群組由網域中已建立的所有使用者設定檔所繼承。您也可以提供特定使用者額外的安全群組，做為 [CreateUserProfile](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateUserProfile.html) API `UserSettings` 參數的一部分。如果您已透過這種方式新增安全群組，則必須確定每個使用者設定檔的安全群組總數在 `VPCOnly` 模式下不會超出 2 個群組的配額上限，以及在 `PublicInternetOnly` 模式下不會超出 4 個群組的配額上限。如果任何使用者設定檔產生的安全群組總數超過該配額，您可以將多個安全群組規則合併為一個安全群組。  

## 將 RStudio 支援新增至現有的網域
<a name="rstudio-add-existing-enable"></a>

完成了先決條件後，您就可以將 RStudio 支援新增至現有網域。下列步驟概述如何更新現有網域，以新增 RStudio 的支援。

### 步驟 1：刪除網域中的所有應用程式
<a name="rstudio-add-existing-enable-step1"></a>

若要在您的網域中新增 RStudio 的支援，SageMaker AI 必須更新所有現有使用者設定檔的基礎安全群組。若要完成此操作，您必須刪除並重新建立網域中的所有現有應用程式。以下程序說明如何刪除所有應用程式。

1.  列出網域中的所有應用程式。

   ```
   aws sagemaker \
      list-apps \
      --domain-id-equals <DOMAIN_ID>
   ```

1.  刪除網域中每個使用者設定檔的每個應用程式。

   ```
   // JupyterServer apps 
   aws sagemaker \
       delete-app \
       --domain-id <DOMAIN_ID> \
       --user-profile-name <USER_PROFILE> \
       --app-type JupyterServer \
       --app-name <APP_NAME>
   
   // KernelGateway apps
   aws sagemaker \
       delete-app \
       --domain-id <DOMAIN_ID> \
       --user-profile-name <USER_PROFILE> \
       --app-type KernelGateway \
       --app-name <APP_NAME>
   ```

### 步驟 2 — 使用新的安全群組清單更新所有使用者設定檔
<a name="rstudio-add-existing-enable-step2"></a>

 當您已重構現有的安全群組時，這是您必須針對網域中所有現有使用者設定檔完成的一次性動作。這可讓您預防達到安全群組數量上限的配額。如果使用者具有任何處於 [InService](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeApp.html#sagemaker-DescribeApp-response-Status) 狀態的應用程式，則 `UpdateUserProfile` API 會呼叫失敗。刪除所有應用程式，然後呼叫 `UpdateUserProfile` API 更新安全群組。

**注意**  
由於 SageMaker AI 服務管理 `AppSecurityGroupManagement`，因此新增 RStudio 支援時，不再需要以下 [將 VPC 中 Amazon SageMaker Studio Classic 筆記本連線到外部資源](https://docs.aws.amazon.com/sagemaker/latest/dg/studio-notebooks-and-internet-access.html#studio-notebooks-and-internet-access-vpc-only)中所概述的 `VPCOnly` 模式需求：  
[安全群組內的 TCP 流量](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/security-group-rules-reference.html#sg-rules-other-instances)。JupyterServer 應用程式和 KernelGateway 應用程式之間的連線能力為必要。您必須至少允許存取範圍 `8192-65535` 內的連接埠。” 

```
aws sagemaker \
    update-user-profile \
    --domain-id <DOMAIN_ID>\
    --user-profile-name <USER_PROFILE> \
    --user-settings "{\"SecurityGroups\": [\"<SECURITY_GROUP>\", \"<SECURITY_GROUP>\"]}"
```

### 步驟 3 — 呼叫 UpdateDomain API 啟用 RStudio
<a name="rstudio-add-existing-enable-step3"></a>

1.  呼叫 [UpdateDomain](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateDomain.html) API 以新增 RStudio on SageMaker AI 的支援。只有在您已重構使用者設定檔的預設安全群組時，才需要 `defaultusersettings` 參數。
   +  針對 `VPCOnly` 模式：

     ```
     aws sagemaker \
         update-domain \
         --domain-id <DOMAIN_ID> \
         --app-security-group-management Service \
         --domain-settings-for-update RStudioServerProDomainSettingsForUpdate={DomainExecutionRoleArn=<DOMAIN_EXECUTION_ROLE_ARN>} \
         --default-user-settings "{\"SecurityGroups\": [\"<SECURITY_GROUP>\", \"<SECURITY_GROUP>\"]}"
     ```
   +  針對 `PublicInternetOnly` 模式：

     ```
     aws sagemaker \
         update-domain \
         --domain-id <DOMAIN_ID> \
         --domain-settings-for-update RStudioServerProDomainSettingsForUpdate={DomainExecutionRoleArn=<DOMAIN_EXECUTION_ROLE_ARN>} \
         --default-user-settings "{\"SecurityGroups\": [\"<SECURITY_GROUP>\", \"<SECURITY_GROUP>\"]}"
     ```

1. 驗證網域狀態是否為 `InService`。在網域狀態為 `InService` 之後，就會新增 RStudio on SageMaker AI 的支援。

   ```
   aws sagemaker \
       describe-domain \
       --domain-id <DOMAIN_ID>
   ```

1. 使用以下命令驗證 RStudioServerPro 應用程式狀態是否為 `InService`。

   ```
   aws sagemaker list-apps --user-profile-name domain-shared
   ```

### 步驟 4 — 新增現有使用者的 RStudio 存取權
<a name="rstudio-add-existing-enable-step4"></a>

 此步驟為步驟 3 中更新的一部分，SageMaker AI 預設會將網域中所有現有使用者設定檔的 RStudio [AccessStatus](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_RStudioServerProAppSettings.html#sagemaker-Type-RStudioServerProAppSettings-AccessStatus) 標示為 `DISABLED`。這可讓您防止超出目前授權所允許的使用者數目。若要新增現有使用者的存取權，系統有一次性選擇加入的步驟。使用以下 [RStudioServerProAppSettings](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UserSettings.html#sagemaker-Type-UserSettings-RStudioServerProAppSettings) 呼叫 [UpdateUserProfile](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateUserProfile.html) 來執行選擇加入：
+  `AccessStatus` = `ENABLED` 
+  *選用* — `UserGroup` = `R_STUDIO_USER` 或 `R_STUDIO_ADMIN` 

```
aws sagemaker \
    update-user-profile \
    --domain-id <DOMAIN_ID>\
    --user-profile-name <USER_PROFILE> \
    --user-settings "{\"RStudioServerProAppSettings\": {\"AccessStatus\": \"ENABLED\"}}"
```

**注意**  
根據預設，可以具有 RStudio 存取權的使用者數目為 60 名。

### 步驟 5 — 停用新使用者的 RStudio 存取權
<a name="rstudio-add-existing-enable-step5"></a>

 除非在呼叫 `UpdateDomain` 時另有指定，否則新增 RStudio on SageMaker AI 的支援後，預設會為所有已建立的新使用者設定檔新增 RStudio 支援。若要停用新使用者設定檔的存取權，您必須明確設定 `AccessStatus` 參數為 `DISABLED`，做為 `CreateUserProfile` API 呼叫的一部分。如果沒有指定 `AccessStatus` 參數做為 `CreateUserProfile` API 的一部分，則預設存取狀態為 `ENABLED`。

```
aws sagemaker \
    create-user-profile \
    --domain-id <DOMAIN_ID>\
    --user-profile-name <USER_PROFILE> \
    --user-settings "{\"RStudioServerProAppSettings\": {\"AccessStatus\": \"DISABLED\"}}"
```