

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Code Editor 管理员指南
<a name="code-editor-admin"></a>

您可以通过按需型实例使用 Code Editor，以获得更快的启动时间和可配置的存储空间。您可以通过 Amazon SageMaker Studio 或通过启动代码编辑器应用程序 AWS CLI。您还可以在域管理控制台中编辑 Code Editor 的默认设置。有关更多信息，请参阅 [编辑域设置](domain-edit.md)。以下主题概述了管理员如何通过更改存储选项、自定义环境和管理用户访问来配置基于 Code-OSS、Visual Studio Code - Open Source 的 Code Editor，并提供了使用 Code Editor 所需的先决条件信息。

**Topics**
+ [满足先决条件](code-editor-admin-prerequisites.md)
+ [让用户访问专用空间](code-editor-admin-user-access.md)
+ [更改默认存储容量](code-editor-admin-storage-size.md)
+ [Code Editor 生命周期配置](code-editor-use-lifecycle-configurations.md)
+ [自定义镜像](code-editor-custom-images.md)

# 满足先决条件
<a name="code-editor-admin-prerequisites"></a>

要使用基于 Code-OSS（Visual Studio Code 的开源版本）的代码编辑器，必须满足以下先决条件。

1. 您必须先登录 Amazon SageMaker AI 域并创建用户个人资料。有关更多信息，请参阅 [亚马逊 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 A SageMaker I 终端节点和配额](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 创建亚马逊 SageMaker资源的自定义 IAM 策略还必须授予向这些资源添加标签的权限。之所以需要为资源添加标签的权限，是因为 Studio 和 Studio Classic 会自动为创建的任何资源添加标签。如果 IAM 策略允许 Studio 和 Studio Classic 创建资源但不允许标记，则在尝试创建资源时可能会出现 AccessDenied “” 错误。有关更多信息，请参阅 [提供标记 A SageMaker I 资源的权限](security_iam_id-based-policy-examples.md#grant-tagging-permissions)。  
[AWS 亚马逊 A SageMaker I 的托管策略](security-iam-awsmanpol.md)授予创建 SageMaker 资源的权限已经包括在创建这些资源时添加标签的权限。

本节提供了一项允许用户访问专用空间的政策。您还可以使用策略将专用空间和与之关联的应用程序限制为与用户配置文件关联的所有者。

您必须为用户提供以下权限：
+ 专用空间
+ 进入专用空间所需的用户配置文件

要提供权限，请将以下策略附加到用户的 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. 创建用户配置文件并在其中指定存储设置。

使用以下 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
        }
    }
}'
```

# Code Editor 生命周期配置
<a name="code-editor-use-lifecycle-configurations"></a>

您可以使用 Code Editor 生命周期配置来自动自定义您的 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)
+ [分离生命周期配置](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`。下面的示例显示了如何为 Code Editor 应用程序创建新的 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>

要调试 Code Editor 的生命周期配置脚本，您必须使用 Studio。有关在 Studio 中调试生命周期配置的说明，请参阅 [调试生命周期配置](jl-lcc-debug.md)。要查找特定应用程序的日志，请使用以下格式搜索日志流：

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

# 分离生命周期配置
<a name="code-editor-use-lifecycle-configurations-studio-detach"></a>

要分离 Code Editor 的生命周期配置，您可以使用管理控制台或 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)

例如，下面的命令会删除域中附加的 Code Editor 应用程序的所有生命周期配置。

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

# 创建生命周期配置，将软件源克隆到 Code Editor 应用程序中
<a name="code-editor-use-lifecycle-configurations-repositories"></a>

本节将介绍如何克隆一个存储库，并创建一个附加生命周期配置的 Code Editor 应用程序。

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. 创建 Code Editor 应用程序，并附加生命周期配置。

   ```
   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"
   ```

   有关可用代码编辑器图像的更多信息 ARNs，请参见[代码编辑器应用程序实例和映像](code-editor-use-instances.md)。

# 创建生命周期配置以安装 Code Editor 扩展
<a name="code-editor-use-lifecycle-configurations-extensions"></a>

本节将介绍如何创建生命周期配置，以便在 Code Editor 环境中从 [Open VSX Registry](https://open-vsx.org/) 安装扩展。

1. 在本地计算机上，创建一个名为 `my-script.sh` 的文件，内容如下：

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

1. 在脚本中，安装 [Open VSX Registry](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 Registry](https://open-vsx.org/) 中扩展名的 URL 获取扩展名。在 `sagemaker-code-editor` 命令中使用的扩展名应包含 URL 中 `https://open-vsx.org/extension/` 后面的所有文本。将所有斜线 (`/`) 替换为句号 (`.`)。例如，`AmazonEMR/emr-tools` 应为 `AmazonEMR.emr-tools`。  
![\[Open VSX Registry 中的 Amazon EMR 扩展页面。\]](http://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/images/code-editor/code-editor-emr-extension.png)

1. 最终确定脚本后，创建并附加生命周期配置。有关更多信息，请参阅 [在 Studio 中创建并附加生命周期配置](code-editor-use-lifecycle-configurations-studio-create.md)。

1. 创建 Code Editor 应用程序，并附加生命周期配置：

   ```
   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"
   ```

   有关可用代码编辑器图像的更多信息 ARNs，请参见[代码编辑器应用程序实例和映像](code-editor-use-instances.md)。有关连接和扩展的更多信息，请参阅 [Code Editor 连接和扩展](code-editor-use-connections-and-extensions.md)。

# 自定义镜像
<a name="code-editor-custom-images"></a>

如果您需要的功能与 SageMaker 发行版提供的功能不同，则可以自带带有自定义扩展和软件包的镜像。您还可以使用它来个性化 Code Editor 用户界面，以满足自己的品牌或合规需求。

下一页将提供特定于代码编辑器的信息和模板，供您创建自己的自定义 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`。
+ Health check 端点 — 你必须将代码编辑器服务器托管在 0.0.0.0 端口 8888， SageMaker AI 才能对其进行检测。
+  身份验证 — 您必须在打开`--without-connection-token`时通过`sagemaker-code-editor`才能允许 SageMaker AI 对您的用户进行身份验证。

**注意**  
如果您使用 Amazon Distrib SageMaker ution 作为基础图片，则这些要求已作为随附`entrypoint-code-editor`脚本的一部分得到满足。

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

以下示例是符合上述信息与[自定义映像规范](studio-updated-byoi-specs.md)的 `Dockerfile`。

**注意**  
如果您要将自己的镜像带到 SageMaker Unified Studio，则需要遵循*亚马逊 Unifie SageMaker d Studio 用户*[指南中的 Dockerfile 规范](https://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/byoi-specifications.html)。  
`Dockerfile` SageMaker Unified Studio 的示例可以在*亚马逊 Unified Studio 用户指南的 Docker SageMaker f* [ile 示例](https://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/byoi-specifications.html#byoi-specifications-example)中找到。

------
#### [ 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 ]

以下是基于[亚马逊 A SageMaker I](https://github.com/aws/sagemaker-distribution/tree/main) Distribution 创建映像的 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"]
```

------