

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

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

您可以使用 AWS 管理主控台 或 建立和連接生命週期組態 AWS Command Line Interface。

**Topics**
+ [建立和連接生命週期組態 (AWS CLI)](#studio-lifecycle-configurations-create-cli)
+ [建立和連接生命週期組態 (主控台)](#studio-lifecycle-configurations-create-console)

## 建立和連接生命週期組態 (AWS CLI)
<a name="studio-lifecycle-configurations-create-cli"></a>

**重要**  
開始之前，請先完成以下先決條件：  
 AWS CLI 按照[安裝目前 AWS CLI 版本](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv1.html#install-tool-bundled)中的步驟更新 。
從您的本機電腦執行`aws configure`並提供您的 AWS 登入資料。如需 AWS 登入資料的資訊，請參閱[了解並取得您的 AWS 登入](https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html)資料。
加入 Amazon SageMaker AI 網域。如需相關概念資訊，請參閱 [Amazon SageMaker AI 網域概觀](gs-studio-onboard.md)。如需快速入門指南，請參閱[使用 Amazon SageMaker AI 的快速設定](onboard-quick-start.md)。

下列程序展示如何建立生命週期組態指令碼，列印程式碼編輯器或 JupyterLab 內的 `Hello World`。

**注意**  
每個指令碼最多可以有 **16,384** 個字元。

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

   ```
   #!/bin/bash
   set -eux
   echo 'Hello World!'
   ```

1. 使用下列內容將您的 `my-script.sh` 檔案轉換為 base64 格式。此要求可防止由於間距和換行編碼而發生的錯誤。

   ```
   LCC_CONTENT=`openssl base64 -A -in my-script.sh`
   ```

1. 建立與 Studio 搭配使用的生命週期組態。下列命令會建立在您啟動相關聯 `JupyterLab` 應用程式時執行的生命週期組態：

   ```
   aws sagemaker create-studio-lifecycle-config \
   --region region \
   --studio-lifecycle-config-name my-lcc \
   --studio-lifecycle-config-content $LCC_CONTENT \
   --studio-lifecycle-config-app-type application-type
   ```

   針對 `studio-lifecycle-config-app-type`，指定 *CodeEditor* 或 *JupyterLab*。
**注意**  
傳回的新建立生命週期組態的 ARN。需要此 ARN 才能將生命週期組態附加至您的應用程式。

為了確保環境已正確自訂，使用者和管理員會使用不同的命令來連接生命週期組態。

### 連接預設生命週期組態 (管理員)
<a name="studio-lifecycle-configurations-attach-cli-administrator"></a>

若要連接生命週期組態，您必須針對網域或使用者設定檔更新 `UserSettings`。所有使用者都會繼承在網域層級關聯的生命週期組態指令碼。但是，在使用者設定檔層級關聯的指令碼範圍是特定使用者。

您可以使用下列命令，使用連接的生命週期組態建立新的使用者設定檔、網域或空間：
+ [create-user-profile](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/create-user-profile.html)
+ [create-domain](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/create-domain.html)
+ [create-space](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/create-space.html)

下列命令為 JupyterLab 應用程式建立一個具有生命週期組態的使用者設定檔。將來自上述步驟的生命週期組態 ARN 新增至使用者的 `JupyterLabAppSettings`。您可以傳遞生命週期組態清單，同時新增多個生命週期組態。當使用者使用 啟動 JupyterLab 應用程式時 AWS CLI，他們可以指定生命週期組態，而不是使用預設組態。使用者傳遞的生命週期組態必須屬於 `JupyterLabAppSettings` 中的生命週期組態清單。

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

下列命令為程式碼編輯器應用程式建立一個具有生命週期組態的使用者設定檔。將來自上述步驟的生命週期組態 ARN 新增至使用者的 `CodeEditorAppSettings`。您可以傳遞生命週期組態清單，同時新增多個生命週期組態。當使用者透過 AWS CLI啟動程式碼編輯器應用程式時，他們可以指定生命週期組態，而非使用預設生命週期組態。使用者傳遞的生命週期組態必須屬於 `CodeEditorAppSettings` 中的生命週期組態清單。

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

### 連接內建生命週期組態 (使用者)
<a name="studio-lifecycle-configurations-attach-cli-user"></a>

若要連接生命週期組態，您的使用者設定檔必須更新 `UserSettings`。

下列命令為 JupyterLab 應用程式建立一個具有生命週期組態的使用者設定檔。將來自上述步驟的生命週期組態 ARN 新增至使用者設定檔的 `JupyterLabAppSettings`。

```
# Update a UserProfile
aws sagemaker update-user-profile --domain-id domain-id \
--user-profile-name user-profile-name \
--region region \
--user-settings '{
"JupyterLabAppSettings": {
  "BuiltInLifecycleConfigArn":"lifecycle-configuration-arn"
  }
}'
```

下列命令為程式碼編輯器應用程式建立一個具有生命週期組態的使用者設定檔。將來自上述步驟的生命週期組態 ARN 新增至使用者設定檔的 `CodeEditorAppSettings`。使用者傳遞的生命週期組態必須屬於 `CodeEditorAppSettings` 中的生命週期組態清單。

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

## 建立和連接生命週期組態 (主控台)
<a name="studio-lifecycle-configurations-create-console"></a>

若要在 中建立和連接生命週期組態 AWS 管理主控台，請導覽至 [Amazon SageMaker AI 主控台](https://console.aws.amazon.com/sagemaker)，然後在左側導覽中選擇**生命週期組態**。主控台會引導您完成建立生命週期組態的程序。