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

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

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

重要

允许 Amazon SageMaker Studio 或 Amazon SageMaker Studio Classic 创建亚马逊 SageMaker资源的自定义 IAM 策略还必须授予向这些资源添加标签的权限。需要向资源添加标签的权限,因为 Studio 和 Studio Classic 会自动标记他们创建的任何资源。如果 IAM 策略允许 Studio 和 Studio Classic 创建资源但不允许标记,则在尝试创建资源时可能会出现 AccessDenied “” 错误。有关更多信息,请参阅 提供为资源添加标签 SageMaker的权限

Amazon 适用于亚马逊的托管政策 SageMaker授予创建 SageMaker 资源的权限已经包括在创建这些资源时添加标签的权限。

重要

截至 2023 年 11 月 30 日,之前的亚马逊 SageMaker Studio 体验现在被命名为 Amazon St SageMaker udio Classic。以下部分专门介绍如何使用 Studio Classic 应用程序。有关使用更新后的 Studio 体验的信息,请参阅亚马逊 SageMaker Studio

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

先决条件

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

步骤 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 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:将生命周期配置附加到域、用户配置文件或共享空间

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

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

将上一步中的生命周期配置 ARN 添加到相应应用程序类型的设置中。例如,将此项放在用户的 JupyterServerAppSettings 中。您可以通过传递生命周期配置列表来同时添加多个生命周期配置。当用户使用启动 JupyterServer 应用程序时 Amazon 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-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