

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

# 创建和附加生命周期配置
<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)，然后在左侧导航栏中选择**生命周期配置**。管理控制台将引导您完成生命周期配置的创建过程。