

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Code Editor 管理者ガイド
<a name="code-editor-admin"></a>

オンデマンドインスタンスで Code Editor を使用し、起動時間を短縮したり、ストレージを構成したりできます。Code Editor アプリケーションは、Amazon SageMaker Studio または AWS CLIから起動できます。ドメインコンソール内で Code Editor のデフォルト設定を編集することもできます。詳細については、「[ドメイン設定を編集する](domain-edit.md)」を参照してください。以下のトピックでは、管理者がストレージオプションの変更、環境のカスタマイズ、ユーザーアクセスの管理を行ったり、Code Editor の使用に必要な前提条件に関する情報を指定したりして、Code-OSS (Visual Studio Code - Open Source) に基づく 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 - Open Source) に基づく Code Editor を使用するには、次の前提条件を満たす必要があります。

1. まず Amazon SageMaker AI ドメインにオンボードし、ユーザープロファイルを作成する必要があります。詳細については、「[Amazon SageMaker AI ドメインの概要](gs-studio-onboard.md)」を参照してください。

1. を使用して Code Editor アプリケーションを操作する場合は AWS CLI、次の前提条件も満たす必要があります。

   1.  [現在の AWS CLI バージョンのインストール](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv1.html#install-tool-bundled)の AWS CLI 手順に従って、 を更新します。

   1.  ローカルマシンで `aws configure` を実行し、 AWS 認証情報を入力します。 AWS 認証情報の詳細については、[AWS 「認証情報の理解と取得](https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html)」を参照してください。

1. (オプション) アプリケーションのストレージとコンピューティングを増やすには、 AWS クォータの引き上げをリクエストできます。クォータの引き上げリクエストの詳細については、「[Amazon SageMaker エンドポイントとクォータ](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. ユーザープロファイルを作成し、その中でストレージ設定を指定します。

ドメインを更新するには、次の 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)
+ [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` であることを確認してください。次の例は、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
```

# Studio でライフサイクル設定をデタッチする
<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"
   ```

   使用可能な Code Editor のイメージ ARN については、「[Code Editor アプリケーションインスタンスとイメージ](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/ja_jp/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"
   ```

   使用可能な Code Editor のイメージ ARN については、「[Code Editor アプリケーションインスタンスとイメージ](code-editor-use-instances.md)」を参照してください。接続と拡張機能については、「[Code Editor の接続と拡張機能](code-editor-use-connections-and-extensions.md)」を参照してください。

# カスタムイメージ
<a name="code-editor-custom-images"></a>

SageMaker Distribution が提供する機能とは異なる機能が必要な場合は、カスタムの拡張機能とパッケージを使用した独自のイメージを持ち込むことができます。これを使用して、独自のブランドやコンプライアンスのニーズに合わせて Code Editor UI をパーソナライズすることもできます。

次のページでは、独自のカスタム SageMaker AI イメージを作成するための Code Editor-固有の情報とテンプレートについて説明します。これは、独自の Amazon SageMaker Studio の情報と、独自の SageMaker AI イメージを作成して Studio に取り込む (BYOI) 手順を補足することを目的としています。カスタム Amazon SageMaker AI イメージと Studio に独自のイメージを取り込む (BYOI) 方法については、「[Bring your own image (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` である必要があります。保持できるアプリケーションは 1 つのみで、必ず `default` という名前にする必要があります。
+ ヘルスチェックエンドポイント – SageMaker AI で検出するには、Code Editor サーバーを 0.0.0.0 ポート 8888 でホストする必要があります。
+  認証 - SageMaker AI がユーザーを認証できるようにするには、`sagemaker-code-editor` を開く際に `--without-connection-token` を渡す必要があります。

**注記**  
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 に取り込む (BYOI) 場合は、「*Amazon SageMaker Unified Studio ユーザーガイド*」の「[Dockerfile 仕様](https://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/byoi-specifications.html)」に従う必要があります。  
SageMaker Unified Studio の `Dockerfile` の例については、「*Amazon SageMaker Unified Studio ユーザーガイド*」の「[Dockerfile の例](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 ]

[Amazon SageMaker AI ディストリビューション](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"]
```

------