

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

# 使用生命周期配置自定义 Amazon SageMaker Studio 经典版
<a name="studio-lcc"></a>

**重要**  
截至 2023 年 11 月 30 日，之前的亚马逊 SageMaker Studio 体验现在被命名为 Amazon St SageMaker udio Classic。以下部分专门介绍如何使用 Studio Classic 应用程序。有关使用更新的 Studio 体验的信息，请参阅 [亚马逊 SageMaker Studio](studio-updated.md)。  
Studio Classic 仍针对现有工作负载进行维护，但不再可供入门使用。您只能停止或删除现有的 Studio Classic 应用程序，不能创建新的应用程序。我们建议您将[工作负载迁移到全新 Studio 体验](studio-updated-migrate.md)。

Amazon SageMaker Studio Classic 会在重要的生命周期事件（例如启动新的 Studio Classic 笔记本）期间触发生命周期配置 sh 您可以使用生命周期配置来自动自定义 Studio Classic 环境。此自定义包括安装自定义软件包、配置笔记本扩展、预加载数据集以及设置源代码存储库。

使用生命周期配置可以灵活控制 Studio Classic 的配置，以满足您的特定需求。例如，您可以使用具有生命周期配置脚本的自定义容器映像来修改您的环境。首先，创建一组最基本的容器映像，然后在这些映像中安装最常用的软件包和库。完成映像后，使用生命周期配置为特定使用场景安装附加软件包。这样，您就可以根据需要灵活地修改数据科学和机器学习团队的环境。

用户只能选择其有权访问的生命周期配置脚本。虽然您可以允许访问多个生命周期配置脚本，但也可以为资源设置默认的生命周期配置脚本。根据为其设置的默认生命周期配置的资源，默认配置要么自动运行，要么是显示的第一个选项。

有关生命周期配置脚本的示例，请参阅 [Studio Classic 生命周期配置示例 GitHub 存储库](https://github.com/aws-samples/sagemaker-studio-lifecycle-config-examples)。有关实现生命周期配置的博客，请参阅[使用生命周期配置自定义 Amazon SageMaker Studio Classic](https://aws.amazon.com/blogs/machine-learning/customize-amazon-sagemaker-studio-using-lifecycle-configurations/)。

**注意**  
每个脚本的字符数上限为 **16384 个字符**。

**Topics**
+ [创建生命周期配置并将其与 Amazon SageMaker Studio Classic 关联](studio-lcc-create.md)
+ [为 Amazon SageMaker Studio 经典版设置默认生命周期配置](studio-lcc-defaults.md)
+ [在亚马逊 SageMaker Studio 经典版中调试生命周期配置](studio-lcc-debug.md)
+ [在 Amazon SageMaker Studio Classic 中更新和分离生命周期配置](studio-lcc-delete.md)

# 创建生命周期配置并将其与 Amazon SageMaker Studio Classic 关联
<a name="studio-lcc-create"></a>

**重要**  
截至 2023 年 11 月 30 日，之前的亚马逊 SageMaker Studio 体验现在被命名为 Amazon St SageMaker udio Classic。以下部分专门介绍如何使用 Studio Classic 应用程序。有关使用更新的 Studio 体验的信息，请参阅 [亚马逊 SageMaker Studio](studio-updated.md)。  
Studio Classic 仍针对现有工作负载进行维护，但不再可供入门使用。您只能停止或删除现有的 Studio Classic 应用程序，不能创建新的应用程序。我们建议您将[工作负载迁移到全新 Studio 体验](studio-updated-migrate.md)。

Amazon SageMaker AI 提供交互式应用程序，支持 Studio Classic 的可视化界面、代码创作和运行体验。本系列介绍如何创建生命周期配置并将其与 A SageMaker I 域相关联。

应用程序类型可以是 `JupyterServer` 或 `KernelGateway`。
+ **`JupyterServer` 应用程序：**这种应用程序类型支持访问 Studio Classic 的可视化界面。Studio Classic 中的每个用户和共享空间都有自己的 JupyterServer 应用程序。
+ **`KernelGateway` 应用程序：**这种应用程序类型支持访问 Studio Classic 笔记本和终端的代码运行环境和内核。有关更多信息，请参阅 [Jupyter Kernel Gateway](https://jupyter-kernel-gateway.readthedocs.io/en/latest/)。

有关 Studio Classic 架构和 Studio Classic 应用程序的更多信息，请参阅[使用亚马逊 SageMaker Studio 经典笔记本电脑](https://docs.aws.amazon.com/sagemaker/latest/dg/notebooks.html)。

**Topics**
+ [使用 AWS CLI 适用于 Amazon SageMaker Studio 的经典版创建生命周期配置](studio-lcc-create-cli.md)
+ [从 SageMaker AI 控制台为 Amazon SageMaker Studio Classic 创建生命周期配置](studio-lcc-create-console.md)

# 使用 AWS CLI 适用于 Amazon SageMaker Studio 的经典版创建生命周期配置
<a name="studio-lcc-create-cli"></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 资源的权限已经包括在创建这些资源时添加标签的权限。

**重要**  
截至 2023 年 11 月 30 日，之前的亚马逊 SageMaker Studio 体验现在被命名为 Amazon St SageMaker udio Classic。以下部分专门介绍如何使用 Studio Classic 应用程序。有关使用更新的 Studio 体验的信息，请参阅 [亚马逊 SageMaker Studio](studio-updated.md)。  
Studio Classic 仍针对现有工作负载进行维护，但不再可供入门使用。您只能停止或删除现有的 Studio Classic 应用程序，不能创建新的应用程序。我们建议您将[工作负载迁移到全新 Studio 体验](studio-updated-migrate.md)。

以下主题介绍如何使用创建生命周期配置， AWS CLI 以自动对 Studio Classic 环境进行自定义。

## 先决条件
<a name="studio-lcc-create-cli-prerequisites"></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)。
+ 按照中的步骤登录 SageMaker AI 域[亚马逊 SageMaker AI 域名概述](gs-studio-onboard.md)。

## 步骤 1：创建生命周期配置
<a name="studio-lcc-create-cli-step1"></a>

以下过程演示如何创建打印 `Hello World` 的生命周期配置脚本。

**注意**  
每个脚本最多可以包含 **16384 个字符**。

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 Classic 的生命周期配置。下面的命令创建一个生命周期配置，该配置在启动关联的 `KernelGateway` 应用程序时运行。

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

   请记录为新创建的生命周期配置返回的 ARN。将生命周期配置附加到应用程序时需要此 ARN。

## 步骤 2：将生命周期配置附加到域、用户配置文件或共享空间
<a name="studio-lcc-create-cli-step2"></a>

要附加生命周期配置，必须更新域的 `UserSettings` 或用户配置文件或共享空间的 `SpaceSettings`。在域级别关联的生命周期配置脚本由所有用户继承。不过，在用户配置文件级别关联的脚本的作用域是特定用户，而在共享空间级别关联的脚本的作用域是共享空间。

下面的示例说明如何创建一个附加生命周期配置的新用户配置文件。您也可以分别使用 [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) 命令创建附加生命周期配置的新域或空间。

将上一步中的生命周期配置 ARN 添加到相应应用程序类型的设置中。例如，将此项放在用户的 `JupyterServerAppSettings` 中。您可以通过传递生命周期配置列表来同时添加多个生命周期配置。当用户使用启动 JupyterServer 应用程序时 AWS CLI，他们可以传递生命周期配置来使用，而不是默认配置。用户传递的生命周期配置必须属于 `JupyterServerAppSettings` 中的生命周期配置列表。

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

以下示例演示如何更新现有共享空间以附加生命周期配置。您还可以使用 [update-domain 或命令更新附有生命周期配置的现有域](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/update-domain.html)或用户配置文件。[update-user-profile](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/update-user-profile.html)当您更新附加的生命周期配置列表时，必须将所有生命周期配置作为列表的一部分传递。如果生命周期配置不在此列表中，则不会附加到应用程序。

```
aws sagemaker update-space --domain-id domain-id \
--space-name space-name \
--region region \
--space-settings '{
"JupyterServerAppSettings": {
  "LifecycleConfigArns":
    [lifecycle-configuration-arn-list]
  }
}'
```

有关为资源设置默认生命周期配置的信息，请参阅 [为 Amazon SageMaker Studio 经典版设置默认生命周期配置](studio-lcc-defaults.md)。

## 步骤 3：启动带有生命周期配置的应用程序
<a name="studio-lcc-create-cli-step3"></a>

将生命周期配置附加到域、用户配置文件或空间后，用户就可以在使用 AWS CLI启动应用程序时选择该配置。本节介绍如何启动附加有生命周期配置的应用程序。有关在启动 JupyterServer应用程序后更改默认生命周期配置的信息，请参阅[为 Amazon SageMaker Studio 经典版设置默认生命周期配置](studio-lcc-defaults.md)。

使用 `create-app` 命令启动所需的应用程序类型，并在 `resource-spec` 参数中指定生命周期配置 ARN。
+ 下面的示例演示如何创建关联生命周期配置的 `JupyterServer` 应用程序。创建 `JupyterServer` 时，`app-name` 必须是 `default`。作为`resource-spec`参数一部分传递的生命周期配置 ARN 必须是中`UserSettings`为您的域或用户配置文件或共享空间 ARNs 指定的生命周期配置列表的一部分。`SpaceSettings`

  ```
  aws sagemaker create-app --domain-id domain-id \
  --region region \
  --user-profile-name user-profile-name \
  --app-type JupyterServer \
  --resource-spec LifecycleConfigArn=lifecycle-configuration-arn \
  --app-name default
  ```
+ 下面的示例演示如何创建关联生命周期配置的 `KernelGateway` 应用程序。

  ```
  aws sagemaker create-app --domain-id domain-id \
  --region region \
  --user-profile-name user-profile-name \
  --app-type KernelGateway \
  --resource-spec LifecycleConfigArn=lifecycle-configuration-arn,SageMakerImageArn=sagemaker-image-arn,InstanceType=instance-type \
  --app-name app-name
  ```

# 从 SageMaker AI 控制台为 Amazon SageMaker Studio Classic 创建生命周期配置
<a name="studio-lcc-create-console"></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 资源的权限已经包括在创建这些资源时添加标签的权限。

**重要**  
截至 2023 年 11 月 30 日，之前的亚马逊 SageMaker Studio 体验现在被命名为 Amazon St SageMaker udio Classic。以下部分专门介绍如何使用 Studio Classic 应用程序。有关使用更新的 Studio 体验的信息，请参阅 [亚马逊 SageMaker Studio](studio-updated.md)。  
Studio Classic 仍针对现有工作负载进行维护，但不再可供入门使用。您只能停止或删除现有的 Studio Classic 应用程序，不能创建新的应用程序。我们建议您将[工作负载迁移到全新 Studio 体验](studio-updated-migrate.md)。

以下主题介绍如何从 Amazon A SageMaker I 控制台创建生命周期配置，以自动对 Studio Classic 环境进行自定义。

## 先决条件
<a name="studio-lcc-create-console-prerequisites"></a>

在开始本教程之前，请满足以下先决条件：
+ 登上亚马逊 SageMaker Studio 经典版。有关更多信息，请参阅 “[登录 Amazon SageMaker Studio 经典版](https://docs.aws.amazon.com/sagemaker/latest/dg/gs-studio-onboard.html)”。

## 步骤 1：创建新的生命周期配置
<a name="studio-lcc-create-console-step1"></a>

您可以通过从 Amazon A SageMaker I 控制台输入脚本来创建生命周期配置。

**注意**  
每个脚本最多可以包含 **16384 个字符**。

以下过程演示如何创建打印 `Hello World` 的生命周期配置脚本。

1. 打开 Amazon A SageMaker I 控制台，网址为[https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)。

1. 在左侧导航窗格中，选择**管理员配置**。

1. 在**管理员配置**下，选择**生命周期配置**。

1. 选择 **Studio** 选项卡。

1. 选择**创建配置**。

1. 在**选择配置类型**下，选择生命周期配置应附加到的应用程序的类型。有关选择将生命周期配置附加到哪个应用程序的更多信息，请参阅 [为 Amazon SageMaker Studio 经典版设置默认生命周期配置](studio-lcc-defaults.md)。

1. 选择**下一步**。

1. 在名为**配置设置**的部分中，输入生命周期配置的名称。

1. 在**脚本**部分中，输入以下内容。

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

1. （可选）为您的生命周期配置创建标签。

1. 选择**提交**。

## 步骤 2：将生命周期配置附加到域或用户配置文件
<a name="studio-lcc-create-console-step2"></a>

在域级别关联的生命周期配置脚本由所有用户继承。但是，在用户配置文件级别关联的脚本的作用域限定为特定用户。

您可以将多个生命周期配置附加到域名或用户配置文件 JupyterServer 和 KernelGateway 应用程序的用户配置文件中。

**注意**  
要将生命周期配置附加到共享空间，必须使用 AWS CLI。有关更多信息，请参阅 [使用 AWS CLI 适用于 Amazon SageMaker Studio 的经典版创建生命周期配置](studio-lcc-create-cli.md)。

下面几节将介绍如何将生命周期配置附加到域或用户配置文件。

### 附加到域
<a name="studio-lcc-create-console-step2-domain"></a>

以下内容显示了如何从 SageMaker AI 控制台将生命周期配置附加到现有域。

1. 打开 Amazon A SageMaker I 控制台，网址为[https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)。

1. 在左侧导航窗格中，选择**管理员配置**。

1. 在**管理员配置**下，选择**域**。

1. 从域列表中，选择要将生命周期配置附加到的域。

1. 在**域详细信息**页面上，选择**环境**选项卡。

1. 在**个人 Studio 应用程序的生命周期配置**下，选择**附加**。

1. 在**来源**下，选择**现有配置**。

1. 在 **Studio 生命周期配置**下，选择在上一步中创建的生命周期配置。

1. 选择**附加到域**。

### 附加到用户配置文件
<a name="studio-lcc-create-console-step2-userprofile"></a>

下面显示如何将生命周期配置附加到现有用户配置文件。

1. 打开 Amazon A SageMaker I 控制台，网址为[https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)。

1. 在左侧导航窗格中，选择**管理员配置**。

1. 在**管理员配置**下，选择**域**。

1. 从域列表中，选择包含要附加生命周期配置的用户配置文件的域。

1. 在**用户配置文件**下，选择用户配置文件。

1. 在**用户详细信息**页面上，选择**编辑**。

1. 在左侧导航中，选择 **Studio 设置**。

1. 在**附加到用户的生命周期配置**下，选择**附加**。

1. 在**来源**下，选择**现有配置**。

1. 在 **Studio 生命周期配置**下，选择在上一步中创建的生命周期配置。

1. 选择**附加到用户配置文件**。

## 步骤 3：使用生命周期配置启动应用程序
<a name="studio-lcc-create-console-step3"></a>

将生命周期配置附加到域或用户配置文件之后，您可以使用附加的生命周期配置启动应用程序。选择使用哪种生命周期配置启动取决于应用程序类型。
+ **JupyterServer**：从控制台启动 JupyterServer应用程序时， SageMaker AI 始终使用默认的生命周期配置。从控制台启动时，不能使用不同的生命周期配置。有关在启动 JupyterServer 应用程序后更改默认生命周期配置的信息，请参阅[为 Amazon SageMaker Studio 经典版设置默认生命周期配置](studio-lcc-defaults.md)。

  要选择其他附加的生命周期配置，必须使用 AWS CLI启动。有关从中启动带有附加生命周期配置的 JupyterServer 应用程序的更多信息 AWS CLI，请参阅[使用 AWS CLI 适用于 Amazon SageMaker Studio 的经典版创建生命周期配置](studio-lcc-create-cli.md)。
+ **KernelGateway**：使用 Studio Classic Launcher 启动 KernelGateway 应用程序时，您可以选择任何附加的生命周期配置。

以下过程介绍如何从 SageMaker AI 控制台启动附加了生命周期配置的 KernelGateway 应用程序。

1. 打开 Amazon A SageMaker I 控制台，网址为[https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)。

1. 启动 Studio Classic。有关更多信息，请参阅 [推出亚马逊 SageMaker Studio 经典版](studio-launch.md)。

1. 在 Studio Classic UI 中，打开 Studio Classic 启动程序。有关更多信息，请参阅 [使用 Amazon SageMaker Studio 经典启动器](studio-launcher.md)。

1. 在 Studio Classic 启动程序中，导航到**笔记本和计算资源**部分。

1. 单击**更改环境**按钮。

1. 在**更改环境**对话框中，使用下拉菜单选择**映像**、**内核**、**实例类型**和**启动脚本**。如果没有默认生命周期配置，则**启动脚本**值默认为 `No script`。否则，**启动脚本**值就是您的默认生命周期配置。选择生命周期配置后，可以查看整个脚本。

1. 单击**选择**。

1. 返回至启动程序，单击**创建笔记本**，使用所选映像和生命周期配置启动新的笔记本内核。

## 步骤 4：查看生命周期配置的日志
<a name="studio-lcc-create-console-step4"></a>

您可以在生命周期配置附加到域或用户配置文件后查看其日志。

1. 首先， CloudWatch 为您的 AWS Identity and Access Management (IAM) 角色提供访问权限。添加对以下日志组和日志流的读取权限。
   + **日志组：**`/aws/sagemaker/studio`
   + **日志流：**`domain/user-profile/app-type/app-name/LifecycleConfigOnStart`

    有关添加权限的信息，请参阅[启用某些 AWS 服务的日志记录](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AWS-logs-and-resource-policy.html)。

1. 在 Studio Classic 中，导航到**运行的终端和内核**图标 (![\[Black square icon representing a placeholder or empty image.\]](http://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/images/studio/icons/running-terminals-kernels.png)) 以监控您的生命周期配置。

1. 从正在运行的应用程序列表中选择一个应用程序。附加生命周期配置的应用程序有一个附加指示图标 ![\[Code brackets symbol representing programming or markup languages.\]](http://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/images/studio/studio-lcc-indicator-icon.png)。

1. 选择应用程序的指示图标。这将打开一个新的面板，其中列出了生命周期配置。

1. 在新面板中，选择 `View logs`。这将打开一个显示日志的新选项卡。

# 为 Amazon SageMaker Studio 经典版设置默认生命周期配置
<a name="studio-lcc-defaults"></a>

**重要**  
截至 2023 年 11 月 30 日，之前的亚马逊 SageMaker Studio 体验现在被命名为 Amazon St SageMaker udio Classic。以下部分专门介绍如何使用 Studio Classic 应用程序。有关使用更新的 Studio 体验的信息，请参阅 [亚马逊 SageMaker Studio](studio-updated.md)。  
Studio Classic 仍针对现有工作负载进行维护，但不再可供入门使用。您只能停止或删除现有的 Studio Classic 应用程序，不能创建新的应用程序。我们建议您将[工作负载迁移到全新 Studio 体验](studio-updated-migrate.md)。

尽管您可以将多个生命周期配置脚本附加到单个资源，但只能为每个资源 JupyterServer 或 KernelGateway应用程序设置一个默认生命周期配置。默认生命周期配置的行为取决于它是为 JupyterServer KernelGateway 应用程序设置的。
+ **JupyterServer 应用程序：**如果设置为 JupyterServer 应用程序的默认生命周期配置脚本，则生命周期配置脚本会在用户首次登录 Studio Classic 或重新启动 Studio Classic 时自动运行。使用此默认生命周期配置可自动执行 Studio Classic 开发者环境的一次性设置操作，例如安装笔记本扩展或设置 GitHub 存储库。有关示例，请参阅[使用生命周期配置自定义 Amazon SageMaker Studio](https://aws.amazon.com/blogs/machine-learning/customize-amazon-sagemaker-studio-using-lifecycle-configurations/)。
+ **KernelGateway 应用程序：**如果设置为 KernelGateway 应用程序的默认生命周期配置脚本，则在 Studio Classic 启动器中默认选择生命周期配置。用户可以在选择默认脚本的情况下启动笔记本或终端，也可以从生命周期配置列表中选择其他脚本。

SageMaker AI 支持为以下资源设置默认生命周期配置：
+ Domains
+ 用户配置文件
+ 共享空间

虽然域和用户配置文件支持从 Amazon AI 控制台和 Amazon SageMaker AI 控制台设置默认生命周期配置 AWS Command Line Interface，但共享空间仅支持从中设置默认生命周期配置 AWS CLI。

创建新资源或更新现有资源时，可以将生命周期配置设置为默认配置。以下主题演示如何使用 SageMaker AI 控制台和设置默认生命周期配置 AWS CLI。

## 默认生命周期配置继承
<a name="studio-lcc-defaults-inheritance"></a>

在*域*级别设置的默认生命周期配置由所有用户和共享空间继承。在*用户*和*共享空间*级别设置的默认生命周期配置的作用域仅限定为该用户或共享空间。用户和空间的默认配置优先于在域级别设置的默认配置。

为域设置的默认 KernelGateway 生命周期配置适用于在该域中启动的所有 KernelGateway 应用程序。除非用户从 Studio Classic 启动程序中显示的列表中选择不同的生命周期配置，否则将使用默认的生命周期配置。如果用户选择了 `No Script`，默认脚本也会运行。有关选择脚本的更多信息，请参阅 [步骤 3：使用生命周期配置启动应用程序](studio-lcc-create-console.md#studio-lcc-create-console-step3)。

**Topics**
+ [默认生命周期配置继承](#studio-lcc-defaults-inheritance)
+ [为 Amazon SageMaker Studio AWS CLI Classic 设置默认值](studio-lcc-defaults-cli.md)
+ [从 SageMaker AI 控制台为亚马逊 SageMaker Studio Classic 设置默认值](studio-lcc-defaults-console.md)

# 为 Amazon SageMaker Studio AWS CLI Classic 设置默认值
<a name="studio-lcc-defaults-cli"></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 资源的权限已经包括在创建这些资源时添加标签的权限。

**重要**  
截至 2023 年 11 月 30 日，之前的亚马逊 SageMaker Studio 体验现在被命名为 Amazon St SageMaker udio Classic。以下部分专门介绍如何使用 Studio Classic 应用程序。有关使用更新的 Studio 体验的信息，请参阅 [亚马逊 SageMaker Studio](studio-updated.md)。  
Studio Classic 仍针对现有工作负载进行维护，但不再可供入门使用。您只能停止或删除现有的 Studio Classic 应用程序，不能创建新的应用程序。我们建议您将[工作负载迁移到全新 Studio 体验](studio-updated-migrate.md)。

您可以从中 AWS CLI 为以下资源设置默认生命周期配置脚本：
+ Domains
+ 用户配置文件
+ 共享空间

下面几节概述了如何从 AWS CLI设置默认生命周期配置脚本。

**Topics**
+ [先决条件](#studio-lcc-defaults-cli-prereq)
+ [创建新资源时设置默认生命周期配置](#studio-lcc-defaults-cli-new)
+ [为现有资源设置默认生命周期配置](#studio-lcc-defaults-cli-existing)

## 先决条件
<a name="studio-lcc-defaults-cli-prereq"></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)。
+ 按照中的步骤登录 SageMaker AI 域[亚马逊 SageMaker AI 域名概述](gs-studio-onboard.md)。
+ 按照 [创建生命周期配置并将其与 Amazon SageMaker Studio Classic 关联](studio-lcc-create.md) 中的步骤创建生命周期配置。

## 创建新资源时设置默认生命周期配置
<a name="studio-lcc-defaults-cli-new"></a>

要在创建新域、用户配置文件或空间时设置默认生命周期配置，请将之前创建的生命周期配置的 ARN 作为以下 AWS CLI 命令之一的一部分传递：
+ [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/opensearch/create-domain.html)
+ [create-space](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/create-space.html)

您必须为 KernelGateway 或 JupyterServer 默认设置中的以下值传递生命周期配置 ARN：
+ `DefaultResourceSpec`:`LifecycleConfigArn` - 这指定应用程序类型的默认生命周期配置。
+ `LifecycleConfigArns` - 这是附加到应用程序类型的所有生命周期配置的列表。默认生命周期配置也必须包含在此列表中。

例如，以下 API 调用使用默认生命周期配置创建一个新的用户配置文件。

```
aws sagemaker create-user-profile --domain-id domain-id \
--user-profile-name user-profile-name \
--region region \
--user-settings '{
"KernelGatewayAppSettings": {
    "DefaultResourceSpec": { 
            "InstanceType": "ml.t3.medium",
            "LifecycleConfigArn": "lifecycle-configuration-arn"
         },
    "LifecycleConfigArns": [lifecycle-configuration-arn-list]
  }
}'
```

## 为现有资源设置默认生命周期配置
<a name="studio-lcc-defaults-cli-existing"></a>

要设置或更新现有资源的默认生命周期配置，请将之前创建的生命周期配置的 ARN 作为以下 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)
+ [update-space](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/update-space.html)

您必须为 KernelGateway 或 JupyterServer 默认设置中的以下值传递生命周期配置 ARN：
+ `DefaultResourceSpec`:`LifecycleConfigArn` - 这指定应用程序类型的默认生命周期配置。
+ `LifecycleConfigArns` - 这是附加到应用程序类型的所有生命周期配置的列表。默认生命周期配置也必须包含在此列表中。

例如，以下 API 调用使用默认生命周期配置更新用户配置文件。

```
aws sagemaker update-user-profile --domain-id domain-id \
--user-profile-name user-profile-name \
--region region \
--user-settings '{
"KernelGatewayAppSettings": {
    "DefaultResourceSpec": {
            "InstanceType": "ml.t3.medium",
            "LifecycleConfigArn": "lifecycle-configuration-arn"
         },
    "LifecycleConfigArns": [lifecycle-configuration-arn-list]
  }
}'
```

以下 API 调用可更新域以设置新的默认生命周期配置。

```
aws sagemaker update-domain --domain-id domain-id \
--region region \
--default-user-settings '{
"JupyterServerAppSettings": {
    "DefaultResourceSpec": {
            "InstanceType": "system",
            "LifecycleConfigArn": "lifecycle-configuration-arn"
         },
    "LifecycleConfigArns": [lifecycle-configuration-arn-list]
  }
}'
```

# 从 SageMaker AI 控制台为亚马逊 SageMaker Studio Classic 设置默认值
<a name="studio-lcc-defaults-console"></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 资源的权限已经包括在创建这些资源时添加标签的权限。

**重要**  
截至 2023 年 11 月 30 日，之前的亚马逊 SageMaker Studio 体验现在被命名为 Amazon St SageMaker udio Classic。以下部分专门介绍如何使用 Studio Classic 应用程序。有关使用更新的 Studio 体验的信息，请参阅 [亚马逊 SageMaker Studio](studio-updated.md)。  
Studio Classic 仍针对现有工作负载进行维护，但不再可供入门使用。您只能停止或删除现有的 Studio Classic 应用程序，不能创建新的应用程序。我们建议您将[工作负载迁移到全新 Studio 体验](studio-updated-migrate.md)。

您可以从 SageMaker AI 控制台为以下资源设置默认生命周期配置脚本。
+ Domains
+ 用户配置文件

您无法从 SageMaker AI 控制台为共享空间设置默认生命周期配置脚本。有关为共享空间设置默认配置的信息，请参阅 [为 Amazon SageMaker Studio AWS CLI Classic 设置默认值](studio-lcc-defaults-cli.md)。

以下各节概述了如何从 SageMaker AI 控制台设置默认生命周期配置脚本。

**Topics**
+ [先决条件](#studio-lcc-defaults-cli-prerequisites)
+ [为域设置默认生命周期配置](#studio-lcc-defaults-cli-domain)
+ [为用户配置文件设置默认生命周期配置](#studio-lcc-defaults-cli-user-profile)

## 先决条件
<a name="studio-lcc-defaults-cli-prerequisites"></a>

在开始之前，请满足以下先决条件：
+ 按照中的步骤登录 SageMaker AI 域[亚马逊 SageMaker AI 域名概述](gs-studio-onboard.md)。
+ 按照 [创建生命周期配置并将其与 Amazon SageMaker Studio Classic 关联](studio-lcc-create.md) 中的步骤创建生命周期配置。

## 为域设置默认生命周期配置
<a name="studio-lcc-defaults-cli-domain"></a>

以下过程说明如何从 SageMaker AI 控制台为域设置默认生命周期配置。

1. 打开 Amazon A SageMaker I 控制台，网址为[https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)。

1. 从域列表中，选择要为其设置默认生命周期配置的域的名称。

1. 在**域详细信息**页面上，选择**环境**选项卡。

1. 在**个人 Studio 应用程序的生命周期配置**下，选择要设置为该域的默认配置的生命周期配置。您可以为 JupyterServer 和 KernelGateway 应用程序设置不同的默认值。

1. 选择**设置为默认配置**。这将打开一个弹出窗口，其中列出了 JupyterServer 和 KernelGateway应用程序的当前默认值。

1. 选择**设置为默认配置**，将生命周期配置设置为其各自应用程序类型的默认配置。

## 为用户配置文件设置默认生命周期配置
<a name="studio-lcc-defaults-cli-user-profile"></a>

以下过程说明如何从 SageMaker AI 控制台为用户配置文件设置默认生命周期配置。

1. 打开 Amazon A SageMaker I 控制台，网址为[https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)。

1. 从域列表中，选择包含要为其设置默认生命周期配置的用户配置文件的域的名称。

1. 在**域详细信息**页面上，选择**用户配置文件**选项卡。

1. 选择要为其设置默认生命周期配置的用户配置文件的名称。这将打开**用户详细信息**页面。

1. 在**用户详细信息**页面上，选择**编辑**。这将打开**编辑用户配置文件**页面。

1. 在**编辑用户配置文件**页面上，选择**步骤 2 Studio 设置**。

1. 在**附加到用户的生命周期配置**下，选择要设置为用户配置文件的默认配置的生命周期配置。您可以为 JupyterServer 和 KernelGateway 应用程序设置不同的默认值。

1. 选择**设置为默认配置**。这将打开一个弹出窗口，其中列出了 JupyterServer 和 KernelGateway应用程序的当前默认值。

1. 选择**设置为默认配置**，将生命周期配置设置为其各自应用程序类型的默认配置。

# 在亚马逊 SageMaker Studio 经典版中调试生命周期配置
<a name="studio-lcc-debug"></a>

**重要**  
截至 2023 年 11 月 30 日，之前的亚马逊 SageMaker Studio 体验现在被命名为 Amazon St SageMaker udio Classic。以下部分专门介绍如何使用 Studio Classic 应用程序。有关使用更新的 Studio 体验的信息，请参阅 [亚马逊 SageMaker Studio](studio-updated.md)。  
Studio Classic 仍针对现有工作负载进行维护，但不再可供入门使用。您只能停止或删除现有的 Studio Classic 应用程序，不能创建新的应用程序。我们建议您将[工作负载迁移到全新 Studio 体验](studio-updated-migrate.md)。

以下主题介绍了如何获取生命周期配置的相关信息并进行调试。

**Topics**
+ [通过 CloudWatch 日志验证生命周期配置流程](#studio-lcc-debug-logs)
+ [JupyterServer 应用程序失败](#studio-lcc-debug-jupyterserver)
+ [KernelGateway 应用程序失败](#studio-lcc-debug-kernel)
+ [生命周期配置超时](#studio-lcc-debug-timeout)

## 通过 CloudWatch 日志验证生命周期配置流程
<a name="studio-lcc-debug-logs"></a>

生命周期配置仅记录 `STDOUT` 和 `STDERR`。

`STDOUT` 是 bash 脚本的默认输出。您可以通过在 bash 命令的末尾追加 `>&2` 来写入 `STDERR`。例如 `echo 'hello'>&2`。

您的生命周期配置日志将 AWS 账户 使用 Amazon 发布给您 CloudWatch。这些日志可以在 CloudWatch 控制台的`/aws/sagemaker/studio`日志流中找到。

1. 打开 CloudWatch 控制台，网址为[https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/)。

1. 从左侧选择**日志**。从下拉菜单中，选择**日志组**。

1. 在**日志组**页面上，搜索 `aws/sagemaker/studio`。

1. 选择日志组。

1. 在**日志组详细信息**页面上，选择**日志流**选项卡。

1. 要查找特定应用程序的日志，请使用以下格式搜索日志流：

   ```
   domain-id/space-name/app-type/default/LifecycleConfigOnStart
   ```

   例如，要查找域 `d-m85lcu8vbqmz`、空间名称 `i-sonic-js` 和应用程序类型 `JupyterLab` 的生命周期配置日志，请使用以下搜索字符串：

   ```
   d-m85lcu8vbqmz/i-sonic-js/JupyterLab/default/LifecycleConfigOnStart
   ```

## JupyterServer 应用程序失败
<a name="studio-lcc-debug-jupyterserver"></a>

如果您的 JupyterServer 应用程序因附加的生命周期配置出现问题而崩溃，Studio Classic 会在 Studio Classic 启动屏幕上显示以下错误消息。

```
Failed to create SageMaker Studio due to start-up script failure
```

选择该`View script logs`链接以查看您的 JupyterServer 应用程序的 CloudWatch 日志。

如果在域、用户配置文件或共享空间的 `DefaultResourceSpec` 中指定了错误的生命周期配置，那么即使重新启动 Studio Classic，Studio Classic 也会继续使用该生命周期配置。

要解决此错误，请按照 [为 Amazon SageMaker Studio 经典版设置默认生命周期配置](studio-lcc-defaults.md) 中的步骤从 `DefaultResourceSpec` 中删除生命周期配置脚本，或选择其他脚本作为默认脚本。然后启动一个新 JupyterServer 应用程序。

## KernelGateway 应用程序失败
<a name="studio-lcc-debug-kernel"></a>

如果您的 KernelGateway 应用程序因附加的生命周期配置出现问题而崩溃，Studio Classic 会在您的 Studio Classic 笔记本中显示错误消息。

选择`View script logs`查看您的 KernelGateway应用程序的 CloudWatch 日志。

在这种情况下，您的生命周期配置是在启动新的 Studio Classic 笔记本时在 Studio Classic 启动程序中指定的。

要解决此错误，请使用 Studio Classic 启动程序选择不同的生命周期配置或选择 `No script`。

**注意**  
中指定的默认 KernelGateway 生命周期配置`DefaultResourceSpec`适用于域、用户配置文件或共享空间中的所有 KernelGateway 图像，除非用户从 Studio Classic 启动器中显示的列表中选择不同的脚本。如果用户选择了 `No Script`，默认脚本也会运行。有关选择脚本的更多信息，请参阅 [步骤 3：使用生命周期配置启动应用程序](studio-lcc-create-console.md#studio-lcc-create-console-step3)。

## 生命周期配置超时
<a name="studio-lcc-debug-timeout"></a>

生命周期配置超时限制为 5 分钟。如果生命周期配置脚本的运行时间超过 5 分钟，Studio Classic 就会抛出错误。

要解决此错误，请确保生命周期配置脚本在 5 分钟内完成。

为了缩短脚本的运行时间，请尝试以下方法：
+ 减少所需的步骤。例如，限制在哪些 conda 环境中安装大型软件包。
+ 在并行进程中运行任务。
+ 在脚本中使用 `nohup` 命令可确保忽略挂起信号，并且不会停止脚本的执行。

# 在 Amazon SageMaker Studio Classic 中更新和分离生命周期配置
<a name="studio-lcc-delete"></a>

**重要**  
截至 2023 年 11 月 30 日，之前的亚马逊 SageMaker Studio 体验现在被命名为 Amazon St SageMaker udio Classic。以下部分专门介绍如何使用 Studio Classic 应用程序。有关使用更新的 Studio 体验的信息，请参阅 [亚马逊 SageMaker Studio](studio-updated.md)。  
Studio Classic 仍针对现有工作负载进行维护，但不再可供入门使用。您只能停止或删除现有的 Studio Classic 应用程序，不能创建新的应用程序。我们建议您将[工作负载迁移到全新 Studio 体验](studio-updated-migrate.md)。

生命周期配置脚本创建后不能更改。要更新脚本，必须创建一个新的生命周期配置脚本，并将其附加到相应的域、用户配置文件或共享空间。有关创建和附加生命周期配置的更多信息，请参阅 [创建生命周期配置并将其与 Amazon SageMaker Studio Classic 关联](studio-lcc-create.md)。

以下主题说明如何使用 AWS CLI 和 SageMaker AI 控制台分离生命周期配置。

**Topics**
+ [先决条件](#studio-lcc-delete-pre)
+ [使用 “分离” AWS CLI](#studio-lcc-delete-cli)

## 先决条件
<a name="studio-lcc-delete-pre"></a>

在分离生命周期配置之前，必须满足以下先决条件。
+ 要成功分离生命周期配置，任何正在运行的应用程序都不能使用生命周期配置。必须先关闭正在运行的应用程序，如 [关闭并更新 Amazon SageMaker Studio 经典版和应用程序](studio-tasks-update.md) 中所示。

## 使用 “分离” AWS CLI
<a name="studio-lcc-delete-cli"></a>

要使用分离生命周期配置 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)
+ [update-space](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/update-space.html)

例如，以下命令删除 KernelGateways 附加到域的所有生命周期配置。

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