

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

# 程式碼編輯器管理員指南
<a name="code-editor-admin"></a>

您可以使用程式碼編輯器搭配隨需執行個體，以加快啟動時間和進行可設定的儲存。您可以透過 Amazon SageMaker Studio 或透過 AWS CLI啟動程式碼編輯器應用程式。您也可以在網域主控台內編輯程式碼編輯器預設設定。如需詳細資訊，請參閱[編輯網域設定](domain-edit.md)。下列主題概述管理員如何設定程式碼編輯器 (以 Code-OSS、Visual Studio Code - Open Source 為基礎)，方法是變更儲存選項、自訂環境和管理使用者存取，以及提供使用程式碼編輯器所需之先決條件的相關資訊。

**Topics**
+ [完成事前準備](code-editor-admin-prerequisites.md)
+ [讓您的使用者可以存取私有空間](code-editor-admin-user-access.md)
+ [變更預設儲存大小](code-editor-admin-storage-size.md)
+ [程式碼編輯器生命週期組態](code-editor-use-lifecycle-configurations.md)
+ [自訂映像](code-editor-custom-images.md)

# 完成事前準備
<a name="code-editor-admin-prerequisites"></a>

若要使用程式碼編輯器 (以 Code-OSS、Visual Studio Code - Open Source 為基礎)，您必須完成下列先決條件。

1. 您必須先加入 Amazon SageMaker AI 網域並建立使用者設定檔。如需詳細資訊，請參閱[Amazon SageMaker AI 網域概觀](gs-studio-onboard.md)。

1. 如果您使用 與程式碼編輯器應用程式互動 AWS CLI，您也必須完成下列先決條件。

   1.  AWS CLI 按照[安裝目前 AWS CLI 版本](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv1.html#install-tool-bundled)中的步驟更新 。

   1.  從您的本機機器，執行 `aws configure` 並提供您的 AWS 憑證。如需 AWS 登入資料的資訊，請參閱[了解並取得您的 AWS 登入](https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html)資料。

1. （選用） 若要為您的應用程式取得更多儲存空間和運算，您可以請求提高 AWS 配額。如需要求配額增加的詳細資訊，請參閱 [Amazon SageMaker AI 端點和配額](https://docs.aws.amazon.com/general/latest/gr/sagemaker.html)。

# 讓您的使用者可以存取私有空間
<a name="code-editor-admin-user-access"></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) 已包含建立這些資源時新增標籤的許可。

本節提供一個授予使用者存取私有空間的政策。您也可以使用政策，將與其相關聯的私有空間和應用程式限制為與使用者設定檔相關聯的擁有者。

您必須提供使用者下列項目的許可：
+ 私有空間
+ 存取私有空間所需的使用者設定檔

若要提供許可，請將下列政策連接至使用者的 IAM 角色。

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

****  

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

      "Effect": "Allow",
      "Action": [
        "sagemaker:CreateApp",
        "sagemaker:DeleteApp"
      ],
      "Resource": "arn:aws:sagemaker:us-east-1:111122223333:app/*",
      "Condition": {
        "Null": {
          "sagemaker:OwnerUserProfileArn": "true"
        }
      }
    },
    {
      "Sid": "SMStudioCreatePresignedDomainUrlForUserProfile",
      "Effect": "Allow",
      "Action": [
        "sagemaker:CreatePresignedDomainUrl"
      ],
      "Resource": "arn:aws:sagemaker:us-east-1:111122223333:user-profile/domain-id/user-profile-name"
    },
    {
      "Sid": "SMStudioAppPermissionsListAndDescribe",
      "Effect": "Allow",
      "Action": [
        "sagemaker:ListApps",
        "sagemaker:ListDomains",
        "sagemaker:ListUserProfiles",
        "sagemaker:ListSpaces",
        "sagemaker:DescribeApp",
        "sagemaker:DescribeDomain",
        "sagemaker:DescribeUserProfile",
        "sagemaker:DescribeSpace"
      ],
      "Resource": "*"
    },
    {
      "Sid": "SMStudioAppPermissionsTagOnCreate",
      "Effect": "Allow",
      "Action": [
        "sagemaker:AddTags"
      ],
      "Resource": "arn:aws:sagemaker:us-east-1:111122223333:*/*",
      "Condition": {
        "Null": {
          "sagemaker:TaggingAction": "false"
        }
      }
    },
    {
      "Sid": "SMStudioRestrictSharedSpacesWithoutOwners",
      "Effect": "Allow",
      "Action": [
        "sagemaker:CreateSpace",
        "sagemaker:UpdateSpace",
        "sagemaker:DeleteSpace"
      ],
      "Resource": "arn:aws:sagemaker:us-east-1:111122223333:space/domain-id/*",
      "Condition": {
        "Null": {
          "sagemaker:OwnerUserProfileArn": "true"
        }
      }
    },
    {
      "Sid": "SMStudioRestrictSpacesToOwnerUserProfile",
      "Effect": "Allow",
      "Action": [
        "sagemaker:CreateSpace",
        "sagemaker:UpdateSpace",
        "sagemaker:DeleteSpace"
      ],
      "Resource": "arn:aws:sagemaker:us-east-1:111122223333:space/domain-id/*",
      "Condition": {
        "ArnLike": {
        "sagemaker:OwnerUserProfileArn": "arn:aws:sagemaker:us-east-1:111122223333:user-profile/domain-id/user-profile-name"
        },
        "StringEquals": {
          "sagemaker:SpaceSharingType": [
            "Private",
            "Shared"
          ]
        }
      }
    },
    {
      "Sid": "SMStudioRestrictCreatePrivateSpaceAppsToOwnerUserProfile",
      "Effect": "Allow",
      "Action": [
        "sagemaker:CreateApp",
        "sagemaker:DeleteApp"
      ],
      "Resource": "arn:aws:sagemaker:us-east-1:111122223333:app/domain-id/*",
      "Condition": {
        "ArnLike": {
        "sagemaker:OwnerUserProfileArn": "arn:aws:sagemaker:us-east-1:111122223333:user-profile/domain-id/user-profile-name"
        },
        "StringEquals": {
          "sagemaker:SpaceSharingType": [
            "Private"
          ]
        }
      }
    }
  ]
}
```

------

# 變更預設儲存大小
<a name="code-editor-admin-storage-size"></a>

您可以變更使用者的預設儲存設定。您也可以根據您的組織要求和使用者的需求來變更預設儲存設定。

若要變更使用者的儲存大小，請執行下列動作：

1. 更新網域中的 Amazon EBS 儲存設定。

1. 建立使用者設定檔，並在其中指定儲存設定。

使用 following AWS Command Line Interface (AWS CLI) 命令更新網域。

```
aws --region $REGION sagemaker update-domain \
--domain-id $DOMAIN_ID \
--default-user-settings '{
    "SpaceStorageSettings": {
        "DefaultEbsStorageSettings":{
            "DefaultEbsVolumeSizeInGb":5,
            "MaximumEbsVolumeSizeInGb":100
        }
    }
}'
```

使用下列 AWS CLI 命令來建立使用者設定檔，並指定預設儲存設定。

```
aws --region $REGION sagemaker create-user-profile \
--domain-id $DOMAIN_ID \
--user-profile-name $USER_PROFILE_NAME \
--user-settings '{
    "SpaceStorageSettings": {
        "DefaultEbsStorageSettings":{
            "DefaultEbsVolumeSizeInGb":5,
            "MaximumEbsVolumeSizeInGb":100
        }
    }
}'
```

使用下列 AWS CLI 命令來更新使用者設定檔中的預設儲存設定。

```
aws --region $REGION sagemaker update-user-profile \
--domain-id $DOMAIN_ID \
--user-profile-name $USER_PROFILE_NAME \
--user-settings '{
    "SpaceStorageSettings": {
        "DefaultEbsStorageSettings":{
            "DefaultEbsVolumeSizeInGb":25,
            "MaximumEbsVolumeSizeInGb":200
        }
    }
}'
```

# 程式碼編輯器生命週期組態
<a name="code-editor-use-lifecycle-configurations"></a>

您可以使用程式碼編輯器生命週期組態，自動自訂您的 Studio 環境。此自訂包含安裝自訂套件、設定筆記本延伸模組、預先載入資料集，以及設定來源碼儲存庫。

下列指示使用 AWS Command Line Interface (AWS CLI) 來建立、連接、偵錯和分離`CodeEditor`應用程式類型的生命週期組態：
+ [在 Studio 中建立和連接生命週期組態](code-editor-use-lifecycle-configurations-studio-create.md)
+ [在 Studio 中偵錯生命週期組態](code-editor-use-lifecycle-configurations-studio-debug.md)
+ [在 Studio 中分離生命週期組態](code-editor-use-lifecycle-configurations-studio-detach.md)

# 在 Studio 中建立和連接生命週期組態
<a name="code-editor-use-lifecycle-configurations-studio-create"></a>

下一節提供 AWS CLI 命令來建立生命週期組態、在建立新使用者設定檔時連接生命週期組態，以及在更新使用者設定檔時連接生命週期組態。如需在 Studio 中建立和連接生命週期組態的先決條件和一般步驟，請參閱[生命週期組態建立](jl-lcc-create.md)。

使用 `create-studio-lifecycle-config` 命令建立 Studio 生命週期組態時，請務必將 `studio-lifecycle-config-app-type` 指定為 `CodeEditor`。以下範例顯示如何為您的程式碼編輯器應用程式建立新的 Studio 生命週期組態。

```
aws sagemaker create-studio-lifecycle-config \
--studio-lifecycle-config-name my-code-editor-lcc \
--studio-lifecycle-config-content $LCC_CONTENT \
--studio-lifecycle-config-app-type CodeEditor
```

記下傳回之新建立之生命週期組態的 ARN。連接生命週期組態時，請在 `CodeEditorAppSettings` 的 `LifecycleConfigArns` 清單內提供此 ARN。

您可以在建立使用者設定檔或網域時連接生命週期組態。以下範例示範如何建立連接生命週期組態的新使用者描述檔。您也可以使用 [create-domain](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/opensearch/create-domain.html) 命令，建立已連接生命週期組態的新網域或空間。

```
# Create a new UserProfile
aws sagemaker create-user-profile \
--domain-id domain-id \
--user-profile-name user-profile-name \
--user-settings '{
"CodeEditorAppSettings": {
  "LifecycleConfigArns":
    [lifecycle-configuration-arn-list]
  }
}'
```

或者，您也可以在更新使用者設定檔或網域時連接生命週期組態。以下範例展示如何更新已連接生命週期組態的使用者設定檔。您也可以使用 [update-domain](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/update-domain.html) 命令，更新已連接生命週期組態的新網域。

```
# Update a UserProfile
aws sagemaker update-user-profile \
--domain-id domain-id \
--user-profile-name user-profile-name \
--user-settings '{
"CodeEditorAppSettings": {
  "LifecycleConfigArns":
    [lifecycle-configuration-arn-list]
  }
}'
```

# 在 Studio 中偵錯生命週期組態
<a name="code-editor-use-lifecycle-configurations-studio-debug"></a>

若要偵錯程式碼編輯器的生命週期組態指令碼，您必須使用 Studio。如需在 Studio 中偵錯生命週期組態的指示，請參閱[生命週期組態偵錯](jl-lcc-debug.md)。若要尋找特定應用程式的日誌，請使用下列格式搜尋日誌串流：

```
domain-id/space-name/CodeEditor/default/LifecycleConfigOnStart
```

# 在 Studio 中分離生命週期組態
<a name="code-editor-use-lifecycle-configurations-studio-detach"></a>

若要分離程式碼編輯器的生命週期組態，您可以使用主控台或 AWS CLI。如需從 Studio 主控台分離生命週期組態的步驟，請參閱[分離生命週期組態](jl-lcc-delete.md)。

若要使用 分離生命週期組態 AWS CLI，請從連接至資源的生命週期組態清單中移除所需的生命週期組態。然後將清單當作個別命令的一部分傳遞：
+ [update-user-profile](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/update-user-profile.html)
+ [update-domain](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/update-domain.html)

例如，以下命令會移除已連接至網域的程式碼編輯器應用程式的所有生命週期組態。

```
aws sagemaker update-domain --domain-id domain-id \
--default-user-settings '{
"CodeEditorAppSettings": {
  "LifecycleConfigArns":
    []
  }
}'
```

# 建立生命週期組態，將儲存庫複製到程式碼編輯器應用程式
<a name="code-editor-use-lifecycle-configurations-repositories"></a>

本節說明如何複製儲存庫，並建立已連接生命週期組態的程式碼編輯器應用程式。

1. 從您的本機電腦中，使用以下內容建立名為 `my-script.sh` 的檔案：

   ```
   #!/bin/bash
   set -eux
   ```

1. 在生命週期組態指令碼中複製您選擇的儲存庫。

   ```
   export REPOSITORY_URL="https://github.com/aws-samples/sagemaker-studio-lifecycle-config-examples.git"
   git -C /home/sagemaker-user clone $REPOSITORY_URL
   ```

1. 在完成指令碼之後，請建立並連接生命週期組態。如需詳細資訊，請參閱[在 Studio 中建立和連接生命週期組態](code-editor-use-lifecycle-configurations-studio-create.md)。

1. 建立已連接生命週期組態的程式碼編輯器應用程式。

   ```
   aws sagemaker create-app \
   --domain-id domain-id \
   --space-name space-name \
   --app-type CodeEditor \
   --app-name default \
   --resource-spec "SageMakerImageArn=arn:aws:sagemaker:region:image-account-id:image/sagemaker-distribution-cpu,LifecycleConfigArn=arn:aws:sagemaker:region:user-account-id:studio-lifecycle-config/my-code-editor-lcc,InstanceType=ml.t3.large"
   ```

   如需可用程式碼編輯器映像 ARN 的詳細資訊，請參閱 [程式碼編輯器應用程式執行個體和映像](code-editor-use-instances.md)。

# 建立生命週期組態以安裝程式碼編輯器延伸模組
<a name="code-editor-use-lifecycle-configurations-extensions"></a>

本節說明如何建立生命週期組態，以在您的程式碼編輯器環境中從 [Open VSX 登錄](https://open-vsx.org/)安裝延伸模組。

1. 從您的本機電腦中，使用以下內容建立名為 `my-script.sh` 的檔案：

   ```
   #!/bin/bash
   set -eux
   ```

1. 在指令碼內，安裝您選擇的 [Open VSX 登錄](https://open-vsx.org/)延伸模組：

   ```
   sagemaker-code-editor --install-extension AmazonEMR.emr-tools --extensions-dir /opt/amazon/sagemaker/sagemaker-code-editor-server-data/extensions
   ```

   您可以從 [Open VSX 登錄](https://open-vsx.org/)中延伸模組的 URL 擷取延伸模組名稱。在 `sagemaker-code-editor` 命令中使用的延伸模組名稱應該包含 URL 中 `https://open-vsx.org/extension/` 後面的所有文字。將斜線 (`.`) 的所有執行個體取代為句點 (`/`)。例如，`AmazonEMR/emr-tools` 應該是 `AmazonEMR.emr-tools`。  
![\[Open VSX 登錄中的 Amazon EMR 延伸模組頁面。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/code-editor/code-editor-emr-extension.png)

1. 在完成指令碼之後，請建立並連接生命週期組態。如需詳細資訊，請參閱[在 Studio 中建立和連接生命週期組態](code-editor-use-lifecycle-configurations-studio-create.md)。

1. 建立已連接生命週期組態的程式碼編輯器應用程式：

   ```
   aws sagemaker create-app \
   --domain-id domain-id \
   --space-name space-name \
   --app-type CodeEditor \
   --app-name default \
   --resource-spec "SageMakerImageArn=arn:aws:sagemaker:region:image-account-id:image/sagemaker-distribution-cpu,LifecycleConfigArn=arn:aws:sagemaker:region:user-account-id:studio-lifecycle-config/my-code-editor-lcc,InstanceType=ml.t3.large"
   ```

   如需可用程式碼編輯器映像 ARN 的詳細資訊，請參閱 [程式碼編輯器應用程式執行個體和映像](code-editor-use-instances.md)。如需連線和延伸模組的詳細資訊，請參閱[程式碼編輯器連線和延伸模組](code-editor-use-connections-and-extensions.md)。

# 自訂映像
<a name="code-editor-custom-images"></a>

如果您需要的功能與 SageMaker Distribution 提供的功能不同，您可以自帶映像搭配您的自訂延伸模組和套件。您也可以使用它來個人化程式碼編輯器 UI，以滿足您自己的品牌或合規需求。

以下頁面將提供程式碼編輯器特定的資訊和範本，以建立您自己的自訂 SageMaker AI 映像。這是為了補充 Amazon SageMaker Studio 的資訊和指示，以建立您自己的 SageMaker AI 映像並自帶映像至 Studio。若要了解自訂 Amazon SageMaker AI 映像，以及如何自帶映像至 Studio，請參閱 [自帶映像 (BYOI)](studio-updated-byoi.md)。

**Topics**
+ [應用程式的運作狀態檢查和 URL](#code-editor-custom-images-app-healthcheck)
+ [Dockerfile 範例](#code-editor-custom-images-dockerfile-templates)

## 應用程式的運作狀態檢查和 URL
<a name="code-editor-custom-images-app-healthcheck"></a>
+ `Base URL` - BYOI 應用程式的基礎 URL 必須為 `CodeEditor/default`。您只能有一個應用程式，且必須一律命名為 `default`。
+ 運作狀態檢查端點 - 您必須將程式碼編輯器伺服器託管在 0.0.0.0 連接埠 8888，SageMaker AI 才能偵測它。
+  驗證 - 您必須在開啟 `--without-connection-token` 時傳遞 `sagemaker-code-editor`，以允許 SageMaker AI 驗證您的使用者。

**注意**  
如果您使用 Amazon SageMaker Distribution 作為基礎映像，這些要求已在包含的 `entrypoint-code-editor` 指令碼中處理。

## Dockerfile 範例
<a name="code-editor-custom-images-dockerfile-templates"></a>

下列範例是符合上述資訊和 [自訂映像規格](studio-updated-byoi-specs.md) 的 `Dockerfile`。

**注意**  
如果您要將自有映像帶入 SageMaker Unified Studio，則需要遵循《Amazon SageMaker Unified Studio 使用者指南》**中的 [Dockerfile 規格](https://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/byoi-specifications.html)。  
您可以在《Amazon SageMaker Unified Studio 使用者指南》**的 [Dockerfile 範例中](https://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/byoi-specifications.html#byoi-specifications-example)找到 SageMaker Unified Studio 的 `Dockerfile` 範例。

------
#### [ Example micromamba Dockerfile ]

以下是使用 [https://mamba.readthedocs.io/en/latest/user_guide/micromamba.html](https://mamba.readthedocs.io/en/latest/user_guide/micromamba.html) 基礎環境從頭開始建立映像的範例 Dockerfile：

```
FROM mambaorg/micromamba:latest
ARG NB_USER="sagemaker-user"
ARG NB_UID=1000
ARG NB_GID=100

USER root

RUN micromamba install -y --name base -c conda-forge sagemaker-code-editor

USER $NB_UID

CMD eval "$(micromamba shell hook --shell=bash)"; \
    micromamba activate base; \
    sagemaker-code-editor --host 0.0.0.0 --port 8888 \
        --without-connection-token \
        --base-path "/CodeEditor/default"
```

------
#### [ Example SageMaker AI Distribution Dockerfile ]

以下是根據 [Amazon SageMaker AI Distribution](https://github.com/aws/sagemaker-distribution/tree/main) 建立映像的範例 Dockerfile：

```
FROM public.ecr.aws/sagemaker/sagemaker-distribution:latest-cpu
ARG NB_USER="sagemaker-user"
ARG NB_UID=1000
ARG NB_GID=100
ENV MAMBA_USER=$NB_USER

USER root

 # install scrapy in the base environment
RUN micromamba install -y --name base -c conda-forge scrapy

 # download VSCodeVim
RUN \
  wget https://github.com/VSCodeVim/Vim/releases/download/v1.27.2/vim-1.27.2.vsix \
  -P /tmp/exts/ --no-check-certificate

 # Install the extension
RUN \
  extensionloc=/opt/amazon/sagemaker/sagemaker-code-editor-server-data/extensions \
  && sagemaker-code-editor \
    --install-extension "/tmp/exts/vim-1.27.2.vsix" \
    --extensions-dir "${extensionloc}"

USER $MAMBA_USER
ENTRYPOINT ["entrypoint-code-editor"]
```

------