通过 Amazon CLI 创建生命周期配置 - Amazon SageMaker
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

通过 Amazon CLI 创建生命周期配置

以下主题介绍如何使用 Amazon CLI 创建生命周期配置,以便自动自定义您的 Studio 环境。

先决条件

在开始之前,请满足以下先决条件:

步骤 1:创建生命周期配置

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

注意

每个脚本最多可以包含 16384 个字符

  1. 在本地计算机上,创建一个名为 my-script.sh 的文件,内容如下。

    #!/bin/bash set -eux echo 'Hello World!'
  2. my-script.sh 文件转换为 base64 格式。此要求可防止因空格和换行编码而出现错误。

    LCC_CONTENT=`openssl base64 -A -in my-script.sh`
  3. 创建用于 Studio 的生命周期配置。下面的命令创建一个生命周期配置,该配置在启动关联的 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:将生命周期配置附加到域、用户配置文件或共享空间

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

下面的示例说明如何创建一个附加生命周期配置的新用户配置文件。您也可以分别使用 create-domaincreate-space 命令创建附加生命周期配置的新域或空间。

将上一步中的生命周期配置 ARN 添加到相应应用程序类型的设置中。例如,将此项放在用户的 JupyterServerAppSettings 中。您可以通过传递生命周期配置列表来同时添加多个生命周期配置。当用户使用 Amazon CLI 启动 JupyterServer 应用程序时,他们可以传递要使用的生命周期配置,而不是默认配置。用户传递的生命周期配置必须属于 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-domainupdate-user-profile 命令更新附加了生命周期配置的现有域或用户配置文件。当您更新附加的生命周期配置列表时,必须将所有生命周期配置作为列表的一部分传递。如果生命周期配置不在此列表中,则不会附加到应用程序。

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

有关为资源设置默认生命周期配置的信息,请参阅 设置默认生命周期配置

步骤 3:启动带有生命周期配置的应用程序

将生命周期配置附加到域、用户配置文件或空间后,用户就可以在使用 Amazon CLI 启动应用程序时选择该配置。本节介绍如何启动附加有生命周期配置的应用程序。有关在启动 JupyterServer 应用程序后更改默认生命周期配置的信息,请参阅 设置默认生命周期配置

使用 create-app 命令启动所需的应用程序类型,并在 resource-spec 参数中指定生命周期配置 ARN。

  • 下面的示例演示如何创建关联生命周期配置的 JupyterServer 应用程序。创建 JupyterServer 时,app-name 必须是 default。作为 resource-spec 参数一部分传递的生命周期配置 ARN 必须是域 UserSettings 或用户配置文件或共享空间 SpaceSettings 中指定的生命周期配置 ARN 列表的一部分。

    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