

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

# Amazon SageMaker Studio 中的生命周期配置
<a name="studio-lifecycle-configurations"></a>

生命周期配置 (LCCs) 是管理员和用户可以用来在您的 Amazon SageMaker Studio 环境中自动自定义以下应用程序的脚本：
+ 亚马逊 SageMaker AI JupyterLab
+ 基于 Code-OSS（Visual Studio Code 的开源版本）的代码编辑器
+ Studio Classic
+ 笔记本实例

自定义应用程序包括：
+ 安装自定义程序包
+ 配置扩展
+ 预载数据集
+ 设置源代码存储库

用户可以创建内置生命周期配置并将其附加到自己的用户配置文件中。管理员在域、空间或用户配置文件级别创建和附加默认或内置生命周期配置。

**重要**  
Amazon SageMaker Studio 首先运行内置的生命周期配置，然后运行默认 LCC。Amazon SageMaker AI 无法解决用户和管理员之间的包裹冲突 LCCs。例如，如果内置的 LCC 安装了 `python3.11`，而默认的 LCC 安装了 `python3.12`，Studio 则安装 `python3.12`。

# 创建和附加生命周期配置
<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 域名。有关概念性信息，请参阅 [亚马逊 SageMaker AI 域名概述](gs-studio-onboard.md)。有关快速入门指南，请参阅 [使用 Amazon A SageMaker I 的快速设置](onboard-quick-start.md)。

以下过程说明如何创建`Hello World`可在代码编辑器或中打印的生命周期配置脚本 JupyterLab。

**注意**  
每个脚本最多可以包含 **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 的生命周期配置。下面的命令创建一个生命周期配置，该配置在启动关联的 `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]
  }
}'
```

下面的命令为 Code Editor 应用程序创建了具有生命周期配置的用户配置文件。将上一步中的生命周期配置 ARN 添加到用户的 `CodeEditorAppSettings` 中。您可以通过传递列表同时添加多个生命周期配置。当用户使用 AWS CLI启动 Code Editor 应用程序时，他们可以指定生命周期配置，而不是使用默认配置。用户传递的生命周期配置必须属于 `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"
  }
}'
```

下面的命令为 Code Editor 应用程序创建了具有生命周期配置的用户配置文件。将上一步中的生命周期配置 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 A SageMaker I 控制台](https://console.aws.amazon.com/sagemaker)，然后在左侧导航栏中选择**生命周期配置**。管理控制台将引导您完成生命周期配置的创建过程。

# 调试生命周期配置
<a name="studio-lifecycle-configurations-debug"></a>

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

**Topics**
+ [从 L CloudWatch ogs 中验证生命周期配置流程](#studio-lifecycle-configurations-debug-logs)
+ [生命周期配置超时](studio-lifecycle-configurations-debug-timeout.md)

## 从 L CloudWatch ogs 中验证生命周期配置流程
<a name="studio-lifecycle-configurations-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
   ```

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

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

1. 要查看脚本执行日志，请选择附加了 `LifecycleConfigOnStart` 的日志流。

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

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

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

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