

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

# Amazon SageMaker Studio 中的程式碼編輯器
<a name="code-editor"></a>

以 [Code-OSS、Visual Studio Code - Open Source](https://github.com/microsoft/vscode#visual-studio-code---open-source-code---oss) 為基礎的程式碼編輯器，可協助您撰寫、測試、偵錯和執行您的分析和機器學習程式碼。程式碼編輯器會擴展並與 Amazon SageMaker Studio 完全整合。它還支援 [Open VSX 登錄](https://open-vsx.org/)中可用的整合式開發環境 (IDE) 延伸模組。以下頁面提供程式碼編輯器的相關資訊，以及使用程式碼編輯器的重要詳細資訊。

Code Editor 已預先安裝 [AWS Toolkit for VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) 延伸模組，可連線至 ， AWS 服務 例如 [Amazon CodeWhisperer](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/codewhisperer.html)，這是一般用途、採用機器學習技術的程式碼產生器，可即時提供程式碼建議。如需延伸模組的詳細資訊，請參閱[程式碼編輯器連線和延伸模組](code-editor-use-connections-and-extensions.md)。

**重要**  
自 2023 年 11 月 30 日起，先前的 Amazon SageMaker Studio 體驗現在命名為 Amazon SageMaker Studio Classic。下節專門介紹如何使用更新的 Studio 體驗。如需使用 Studio Classic 應用程式的資訊，請參閱 [Amazon SageMaker Studio Classic](studio.md)。

若要啟動程式碼編輯器，請建立程式碼編輯器私有空間。程式碼編輯器空間會使用單一 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體進行運算，以及使用單一 Amazon Elastic Block Store (Amazon EBS) 磁碟區進行儲存。您空間中的所有內容，例如程式碼、git 設定檔和環境變數，都會存放在相同的 Amazon EBS 磁碟區。磁碟區具有 3000 IOPS 和 125 MBps 的輸送量。您的管理員已為您的空間設定預設 Amazon EBS 儲存體設定。

預設儲存大小為 5 GB，但您的管理員可以增加您取得的空間量。如需詳細資訊，請參閱[變更預設儲存大小](code-editor-admin-storage-size.md)。

儲存磁碟區內使用者的工作目錄為 `/home/sagemaker-user`。如果您指定自己的 AWS KMS 金鑰來加密磁碟區，工作目錄中的所有內容都會使用客戶受管金鑰加密。如果您未指定 AWS KMS 金鑰，內部的資料`/home/sagemaker-user`會使用 AWS 受管金鑰加密。無論您是否指定 AWS KMS 金鑰，工作目錄以外的所有資料都會使用 AWS 受管金鑰加密。

您可以透過變更執行程式碼編輯器應用程式的 Amazon EC2 執行個體類型來擴展或縮減運算。在變更相關聯的執行個體類型之前，您必須先停止程式碼編輯器空間。如需詳細資訊，請參閱[程式碼編輯器應用程式執行個體和映像](code-editor-use-instances.md)。

您的管理員可能會為您提供生命週期組態，以自訂您的環境。您可以在建立空間時指定生命週期組態。如需詳細資訊，請參閱[程式碼編輯器生命週期組態](code-editor-use-lifecycle-configurations.md)。

如果您有 Amazon EFS 磁碟區，您也可以自帶檔案儲存系統。

![\[程式碼編輯器應用程式 UI 的歡迎頁面。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/code-editor/code-editor-home.png)


**Topics**
+ [使用程式碼編輯器](code-editor-use.md)
+ [程式碼編輯器管理員指南](code-editor-admin.md)

# 使用程式碼編輯器
<a name="code-editor-use"></a>

本節中的主題提供使用程式碼編輯器的指南，包括如何啟動、新增連線 AWS 服務、關閉資源等。建立程式碼編輯器空間後，您可以透過瀏覽器直接存取程式碼編輯器工作階段。

在程式碼編輯器環境內，您可以執行下列動作：
+ 存取您主目錄中保存的所有成品
+ 複製您的 GitHub 儲存庫並遞交變更
+ 存取 SageMaker Python SDK

您可以返回 Studio 來檢閱程式碼編輯器環境中建立的任何資產，例如實驗、管道或訓練任務。

**Topics**
+ [檢查程式碼編輯器的版本](code-editor-use-version.md)
+ [程式碼編輯器應用程式執行個體和映像](code-editor-use-instances.md)
+ [在 Studio 中啟動程式碼編輯器應用程式](code-editor-use-studio.md)
+ [使用 啟動程式碼編輯器應用程式 AWS CLI](code-editor-launch-cli.md)
+ [在程式碼編輯器中複製儲存庫](code-editor-use-clone-a-repository.md)
+ [程式碼編輯器連線和延伸模組](code-editor-use-connections-and-extensions.md)
+ [關閉程式碼編輯器資源](code-editor-use-log-out.md)

# 檢查程式碼編輯器的版本
<a name="code-editor-use-version"></a>

下列步驟說明如何檢查程式碼編輯器應用程式的版本。

**檢查程式碼編輯器應用程式版本**

1. 啟動並執行程式碼編輯器空間，然後導覽至程式碼編輯器應用程式 UI。如需詳細資訊，請參閱[在 Studio 中啟動程式碼編輯器應用程式](code-editor-use-studio.md)。

1. 在程式碼編輯器 UI 的左上角，選擇功能表按鈕 (![\[Hamburger menu icon with three horizontal lines.\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/code-editor/code-editor-menu-icon.png))。然後，選擇**說明**。然後，選擇**關於**。

# 程式碼編輯器應用程式執行個體和映像
<a name="code-editor-use-instances"></a>

只有某些執行個體與程式碼編輯器應用程式相容。您可以從**執行個體**下拉式功能表中選擇與您使用案例相容的執行個體類型。

**快速啟動**執行個體的啟動速度比其他執行個體快得多。如需在 Studio 中快速啟動執行個體類型的詳細資訊，請參閱[可與 Amazon SageMaker Studio Classic 筆記本搭配使用的執行個體類型](notebooks-available-instance-types.md)。

**注意**  
如果您在設定程式碼編輯器應用程式時使用 GPU 執行個體類型，您也必須使用 GPU 型映像。當您選取執行個體類型時，程式碼編輯器空間 UI 會自動選取相容的映像。

在空間內，您的資料會儲存在與執行個體生命週期無關的 Amazon EBS 磁碟區中。當您變更執行個體時，不會遺失資料。如果您的程式碼編輯器空間是 `Running`，您必須在變更執行個體類型之前停止空間。

下表列出每個區域的可用程式碼編輯器 CPU 和 GPU 映像的 ARN。


|  區域  |  CPU  |  GPU  | 
| --- | --- | --- | 
|  us-east-1  | arn:aws:sagemaker:us-east-1:885854791233:image/sagemaker-distribution-cpu |  arn:aws:sagemaker:us-east-1:885854791233:image/sagemaker-distribution-gpu | 
|  us-east-2  | arn:aws:sagemaker:us-east-2:37914896644:image/sagemaker-distribution-cpu | arn:aws:sagemaker:us-east-2:37914896644:image/sagemaker-distribution-gpu | 
|  us-west-1  | arn:aws:sagemaker:us-west-1:053634841547:image/sagemaker-distribution-cpu | arn:aws:sagemaker:us-west-1:053634841547:image/sagemaker-distribution-gpu | 
|  us-west-2  | arn:aws:sagemaker:us-west-2:542918446943:image/sagemaker-distribution-cpu |  arn:aws:sagemaker:us-west-2:542918446943:image/sagemaker-distribution-gpu | 
|  af-south-1  | arn:aws:sagemaker:af-south-1:238384257742:image/sagemaker-distribution-cpu | arn:aws:sagemaker:af-south-1:238384257742:image/sagemaker-distribution-gpu | 
|  ap-east-1  | arn:aws:sagemaker:ap-east-1:523751269255:image/sagemaker-distribution-cpu | arn:aws:sagemaker:ap-east-1:523751269255:image/sagemaker-distribution-gpu | 
|  ap-south-1  | arn:aws:sagemaker:ap-south-1:245090515133:image/sagemaker-distribution-cpu | arn:aws:sagemaker:ap-south-1:245090515133:image/sagemaker-distribution-gpu | 
|  ap-northeast-2  | arn:aws:sagemaker:ap-northeast-2:064688005998:image/sagemaker-distribution-cpu | arn:aws:sagemaker:ap-northeast-2:064688005998:image/sagemaker-distribution-gpu | 
|  ap-southeast-1  | arn:aws:sagemaker:ap-southeast-1:022667117163:image/sagemaker-distribution-cpu | arn:aws:sagemaker:ap-southeast-1:022667117163:image/sagemaker-distribution-gpu | 
|  ap-southeast-2  | arn:aws:sagemaker:ap-southeast-2:648430277019:image/sagemaker-distribution-cpu | arn:aws:sagemaker:ap-southeast-2:648430277019:image/sagemaker-distribution-gpu | 
|  ap-northeast-1  | arn:aws:sagemaker:ap-northeast-1:010972774902:image/sagemaker-distribution-cpu | arn:aws:sagemaker:ap-northeast-1:010972774902:image/sagemaker-distribution-gpu | 
|  ca-central-1  | arn:aws:sagemaker:ca-central-1:481561238223:image/sagemaker-distribution-cpu | arn:aws:sagemaker:ca-central-1:481561238223:image/sagemaker-distribution-gpu | 
|  eu-central-1  | arn:aws:sagemaker:eu-central-1:545423591354:image/sagemaker-distribution-cpu | arn:aws:sagemaker:eu-central-1:545423591354:image/sagemaker-distribution-gpu | 
|  eu-west-1  | arn:aws:sagemaker:eu-west-1:819792524951:image/sagemaker-distribution-cpu | arn:aws:sagemaker:eu-west-1:819792524951:image/sagemaker-distribution-gpu | 
|  eu-west-2  | arn:aws:sagemaker:eu-west-2:021081402939:image/sagemaker-distribution-cpu | arn:aws:sagemaker:eu-west-2:021081402939:image/sagemaker-distribution-gpu | 
|  eu-west-3  | arn:aws:sagemaker:eu-west-3:856416204555:image/sagemaker-distribution-cpu | arn:aws:sagemaker:eu-west-3:856416204555:image/sagemaker-distribution-gpu | 
|  eu-north-1  | arn:aws:sagemaker:eu-north-1:175620155138:image/sagemaker-distribution-cpu | arn:aws:sagemaker:eu-north-1:175620155138:image/sagemaker-distribution-gpu | 
|  eu-south-1  | arn:aws:sagemaker:eu-south-1:810671768855:image/sagemaker-distribution-cpu | arn:aws:sagemaker:eu-south-1:810671768855:image/sagemaker-distribution-gpu | 
|  sa-east-1  | arn:aws:sagemaker:sa-east-1:567556641782:image/sagemaker-distribution-cpu | arn:aws:sagemaker:sa-east-1:567556641782:image/sagemaker-distribution-gpu | 
|  ap-northeast-3  | arn:aws:sagemaker:ap-northeast-3:564864627153:image/sagemaker-distribution-cpu | arn:aws:sagemaker:ap-northeast-3:564864627153:image/sagemaker-distribution-gpu | 
|  ap-southeast-3  | arn:aws:sagemaker:ap-southeast-3:370607712162:image/sagemaker-distribution-cpu | arn:aws:sagemaker:ap-southeast-3:370607712162:image/sagemaker-distribution-gpu | 
|  me-south-1  | arn:aws:sagemaker:me-south-1:523774347010:image/sagemaker-distribution-cpu | arn:aws:sagemaker:me-south-1:523774347010:image/sagemaker-distribution-gpu | 
|  me-central-1  | arn:aws:sagemaker:me-central-1:358593528301:image/sagemaker-distribution-cpu | arn:aws:sagemaker:me-central-1:358593528301:image/sagemaker-distribution-gpu | 
|  il-central-1  | arn:aws:sagemaker:il-central-1:080319125002:image/sagemaker-distribution-cpu | arn:aws:sagemaker:il-central-1:080319125002:image/sagemaker-distribution-gpu | 
|  cn-north-1  | arn:aws:sagemaker:cn-north-1:674439102856:image/sagemaker-distribution-cpu |  arn:aws:sagemaker:cn-north-1:674439102856:image/sagemaker-distribution-gpu  | 
|  cn-northwest-1  | arn:aws:sagemaker:cn-northwest-1:651871951035:image/sagemaker-distribution-cpu |  arn:aws:sagemaker:cn-northwest-1:651871951035:image/sagemaker-distribution-gpu  | 
|  us-gov-west-1  | arn:aws:sagemaker:us-gov-west-1:300992924816:image/sagemaker-distribution-cpu | arn:aws:sagemaker:us-gov-west-1:300992924816:image/sagemaker-distribution-gpu | 
|  us-gov-east-1  | arn:aws:sagemaker:us-gov-east-1:300993876623:image/sagemaker-distribution-cpu | arn:aws:sagemaker:us-gov-east-1:300993876623:image/sagemaker-distribution-gpu | 

如果您遇到執行個體限制，請聯絡您的管理員。若要為使用者取得更多儲存空間和運算，管理員可以請求提高使用者的 AWS 配額。如需要求配額增加的詳細資訊，請參閱 [Amazon SageMaker AI 端點和配額](https://docs.aws.amazon.com/general/latest/gr/sagemaker.html)。

# 在 Studio 中啟動程式碼編輯器應用程式
<a name="code-editor-use-studio"></a>

若要透過 Studio 設定和存取您的程式碼編輯器整合式開發環境，您必須建立程式碼編輯器空間。如需 Studio 中空間的詳細資訊，請參閱 [Amazon SageMaker Studio 空間](studio-updated-spaces.md)。

![\[Studio UI 中的程式碼編輯器應用程式按鈕和概觀圖磚。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/code-editor/code-editor-studio-home.png)


下列程序說明如何建立和執行程式碼編輯器空間。

**建立和執行程式碼編輯器空間**

1. 啟動更新的 Studio 體驗。如需詳細資訊，請參閱[啟動 Amazon SageMaker Studio](https://docs.aws.amazon.com/sagemaker/latest/dg/studio-updated-launch.html)。

1. 執行以下任意一項：
   + 在更新的 Amazon SageMaker Studio UI 內，從**應用程式**功能表中選取**程式碼編輯器**。
   + 在更新的 Amazon SageMaker Studio UI 內，選擇 Studio 首頁的**概觀**區段中的**檢視程式碼編輯器空間**。

1. 在程式碼編輯器登陸頁面的右上角，選擇**建立程式碼編輯器空間**。

1. 輸入程式碼編輯器空間的名稱。名稱長度必須為 1-62 個字元，只能使用字母、數字和破折號。

1. 選擇**建立空間**。

1. 建立空間後，在選擇執行空間之前，您有一些選項：
   + 您可以編輯**儲存 (GB)**、**生命週期組態**或**連接自訂 EFS 檔案系統**設定。這些設定的選項會根據管理員規格提供。
   + 從**執行個體**下拉式功能表中，您可以選擇與您使用案例最相容的執行個體類型。從**映像**下拉式功能表中，您可以選擇 SageMaker Distribution 映像或管理員提供的自訂映像。
**注意**  
在 sagemaker-distribution 映像之間切換會變更正在使用的程式碼編輯器基礎版本，這可能會由於瀏覽器快取而導致不相容。在映像之間切換時，您應該清除瀏覽器快取。

     如果您在設定程式碼編輯器應用程式時使用 GPU 執行個體類型，您也必須使用 GPU 型映像。在空間內，您的資料會儲存在與執行個體生命週期無關的 Amazon EBS 磁碟區中。當您變更執行個體時，不會遺失資料。
**重要**  
允許 Studio 使用者建立空間的自訂 IAM 政策，也必須授與列出映像 (`sagemaker: ListImage`) 以檢視自訂映像的許可。若要新增許可，請參閱《*AWS Identity and Access Management* 使用者指南》中的[新增或移除身分許可](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html)。  
提供許可來建立 SageMaker AI 資源的 [AWS Amazon SageMaker AI 的 受管政策](security-iam-awsmanpol.md) 已包含建立這些資源時列出映像的許可。
**注意**  
若要更新空間設定，您必須先停止您的空間。如果您的程式碼編輯器使用執行個體搭配 NVMe 執行個體儲存體，則會在空間停止時刪除存放在 NVMe 存放區的任何資料。

1. 更新您的設定後，請在空間詳細資訊頁面中選擇**執行空間**。

1. 在空間的狀態為 `Running` 之後，選擇**開啟程式碼編輯器**以前往程式碼編輯器工作階段。

![\[Studio UI 中程式碼編輯器應用程式的空間詳細資訊頁面。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/code-editor/code-editor-open.png)


# 使用 啟動程式碼編輯器應用程式 AWS CLI
<a name="code-editor-launch-cli"></a>

若要透過 AWS Command Line Interface (AWS CLI) 設定和存取您的程式碼編輯器整合式開發環境，您必須建立程式碼編輯器空間。在進行下列步驟之前，請務必符合[完成事前準備](code-editor-admin-prerequisites.md)。使用下列程序來建立和執行程式碼編輯器空間。

**建立和執行程式碼編輯器空間**

1. 使用 AWS Identity and Access Management (IAM) 或 AWS IAM Identity Center 身分驗證存取空間。如需使用 存取空間的詳細資訊 AWS CLI，請參閱 中的*使用 存取空間 AWS Command Line Interface*[Amazon SageMaker Studio 空間](studio-updated-spaces.md)。

1. 使用下列命令建立應用程式，並將 `CodeEditor` 指定為 `app-type`。

   如果您在建立程式碼編輯器應用程式時使用 GPU 執行個體類型，您也必須使用 GPU 型映像。

   ```
   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:account-id:image/sagemaker-distribution-cpu"
   ```

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

1. 程式碼編輯器應用程式處於服務狀態後，請使用預先簽章的 URL 啟動應用程式。您可以使用 `describe-app` API 來檢查您的應用程式是否處於服務中。使用 `create-presigned-domain-url` API 建立預先簽章的 URL：

   ```
   aws sagemaker create-presigned-domain-url \
   --domain-id domain-id \
   --space-name space-name \
   --user-profile-name user-profile-name \
   --session-expiration-duration-in-seconds 43200 \
   --landing-uri app:CodeEditor:
   ```

1. 開啟產生的 URL 以開始在您的程式碼編輯器應用程式中運作。

# 在程式碼編輯器中複製儲存庫
<a name="code-editor-use-clone-a-repository"></a>

您可以在程式碼編輯器應用程式 UI 的 **Explorer** 視窗中瀏覽資料夾並複製儲存庫。

若要複製儲存庫，請執行下列步驟：

**複製儲存庫**

1. 在瀏覽器中開啟您的程式碼編輯器應用程式，然後選擇左側導覽窗格中的**探索**按鈕 (![\[Icon representing multiple documents or pages stacked on top of each other.\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/code-editor/code-editor-exploration-icon.png))。

1. 在 **Explorer** 視窗中選擇**複製儲存庫**。然後，提供儲存庫 URL 或在提示中挑選儲存庫來源。

1. 選擇一個資料夾，以將您的儲存庫複製至其中。請注意，預設程式碼編輯器資料夾為 `/home/sagemaker-user/`。複製您的儲存庫可能需要一些時間。

1. 若要開啟複製的儲存庫，請選擇**在新視窗中開啟**或**開啟**。

1.  若要返回程式碼編輯器應用程式 UI 首頁，請選擇**取消**。

1. 在儲存庫中，提示會詢問您是否信任新儲存庫中檔案的作者。您有兩個選擇：

   1. 若要信任資料夾並啟用所有功能，請選擇**是，我信任作者**。

   1. 若要以*受限模式*瀏覽儲存庫內容，請選擇**否，我不信任作者**。

      在限制模式中，不允許執行任務、停用偵錯、不套用工作區設定，以及延伸模組的功能有限。

      若要結束受限模式，請信任目前資料夾或其父資料夾中所有檔案的作者，並啟用所有功能，請在**受限模式**橫幅中選擇**管理**。

# 程式碼編輯器連線和延伸模組
<a name="code-editor-use-connections-and-extensions"></a>

Code Editor 支援與 的 IDE 連線 AWS 服務 ，以及 [Open VSX 登錄](https://open-vsx.org/)檔中可用的擴充功能。

## 與 的連線 AWS
<a name="code-editor-use-connections"></a>

程式碼編輯器環境已與 [AWS Toolkit for VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) 整合，以將連線新增至 AWS 服務。若要開始使用 的連線 AWS 服務，您必須擁有 valid AWS Identity and Access Management (IAM) 登入資料。如需詳細資訊，請參閱 [AWS Toolkit for Visual Studio Code 的身分驗證和存取](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/establish-credentials.html)。

在程式碼編輯器環境內，您可以將連線新增至：
+  [AWS Explorer ](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/aws-explorer.html) – 檢視、修改和部署 Amazon S3、CloudWatch 等 AWS 資源。

  在 AWS Explorer 中存取特定功能需要特定 AWS 許可。如需詳細資訊，請參閱 [AWS Toolkit for Visual Studio Code 的身分驗證和存取](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/establish-credentials.html)。
+ [Amazon CodeWhisperer](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/codewhisperer.html) - 使用採用 AI 技術的程式碼建議更快地建置應用程式。

  若要 Amazon CodeWhisperer 搭配程式碼編輯器使用 ，您必須將下列許可新增至 SageMaker AI 執行角色。

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

****  

  ```
  {
    "Version":"2012-10-17",		 	 	 
    "Statement": [
      {
        "Sid": "CodeWhispererPermissions",
        "Effect": "Allow",
        "Action": ["codewhisperer:GenerateRecommendations"],
        "Resource": "*"
      }
    ]
  }
  ```

------

  如需詳細資訊，請參閱《IAM 使用者指南》**中的[建立 IAM 政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html)，以及[新增和移除 IAM 身分許可](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html)。

## 延伸模組
<a name="code-editor-use-extensions"></a>

程式碼編輯器支援 [Open VSX 登錄](https://open-vsx.org/)中可用的 IDE 延伸模組。

若要在程式碼編輯器環境中開始使用延伸模組，請選擇左側導覽窗格中的**延伸模組**圖示 (![\[Icon showing two overlapping squares representing multiple windows or instances.\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/code-editor/code-editor-extensions-icon.png))。在這裡，您可以透過安裝 AWS 來設定 AWS 工具組的連線。如需詳細資訊，請參閱[安裝 AWS Toolkit for Visual Studio Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/setup-toolkit.html)。

在搜尋列中，您可以透過 [Open VSX 登錄](https://open-vsx.org/)直接搜尋其他延伸模組，例如 AWS 工具組、Jupyter、Python 等。

# 關閉程式碼編輯器資源
<a name="code-editor-use-log-out"></a>

使用完程式碼編輯器空間時，您可以使用 Studio 來將其停止。如此一來，您就可以停止產生空間的成本。

或者，您可以使用 AWS CLI刪除未使用的程式碼編輯器資源。

## 使用 Studio 停止程式碼編輯器空間
<a name="code-editor-use-log-out-stop-space"></a>

若要在 Studio 中停止您的程式碼編輯器空間，請使用下列步驟：

**在 Studio 中停止您的程式碼編輯器空間**

1. 請執行下列其中一個動作，返回程式碼編輯器登陸頁面：

   1. 在左上角的導覽列中，選擇**程式碼編輯器**。

   1. 或者，在左側導覽窗格中，選擇**應用程式**功能表中的**程式碼編輯器**。

1. 尋找您建立的程式碼編輯器空間名稱。如果空間的狀態為**執行中**，請在**動作**欄中選擇**停止**。您也可以選擇**停止空間**，直接在空間詳細資訊頁面中停止空間。空間可能需要一些時間才能停止。

![\[程式碼編輯器應用程式 UI 中的空間詳細資訊頁面。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/code-editor/code-editor-stop-space.png)


當您的空間執行個體關閉時，不會自動刪除其他資源，例如 SageMaker AI 端點、Amazon EMR (Amazon EMR) 叢集，以及從 Studio 建立的 Amazon Simple Storage Service (Amazon S3) 儲存貯體。若要停止從資源產生費用，請刪除任何其他資源。如需詳細資訊，請參閱[刪除未使用的資源](https://docs.aws.amazon.com/sagemaker/latest/dg/studio-updated-jl-admin-guide-clean-up.html)。

## 使用 刪除程式碼編輯器資源 AWS CLI
<a name="code-editor-use-log-out-shut-down-resources"></a>

您可以使用 AWS Command Line Interface () 刪除程式碼編輯器應用程式和空間AWS CLI。
+ [DeleteApp](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DeleteApp.html)
+ [DeleteSpace](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DeleteSpace.html)

# 程式碼編輯器管理員指南
<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"]
```

------