

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

# 使用 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
  ```