通过 Amazon CLI 创建生命周期配置
以下主题介绍如何使用 Amazon CLI 创建生命周期配置,以便自动自定义您的 Studio 环境。
先决条件
在开始之前,请满足以下先决条件:
-
按照安装最新 Amazon CLI 版本中的步骤更新 Amazon CLI。
-
在本地计算机上运行
aws configure
并提供您的 Amazon 凭证。有关 Amazon 凭证的信息,请参阅了解并获取您的 Amazon 凭证。 -
按照 加入 Amazon SageMaker 域 中的步骤加入 SageMaker 域。
步骤 1:创建生命周期配置
以下过程演示如何创建打印 Hello World
的生命周期配置脚本。
注意
每个脚本最多可以包含 16384 个字符。
-
在本地计算机上,创建一个名为
my-script.sh
的文件,内容如下。#!/bin/bash set -eux echo 'Hello World!'
-
将
my-script.sh
文件转换为 base64 格式。此要求可防止因空格和换行编码而出现错误。LCC_CONTENT=`openssl base64 -A -in my-script.sh`
-
创建用于 Studio 的生命周期配置。下面的命令创建一个生命周期配置,该配置在启动关联的
KernelGateway
应用程序时运行。aws sagemaker create-studio-lifecycle-config \ --region
region
\ --studio-lifecycle-config-namemy-studio-lcc
\ --studio-lifecycle-config-content $LCC_CONTENT \ --studio-lifecycle-config-app-type KernelGateway请记录为新创建的生命周期配置返回的 ARN。将生命周期配置附加到应用程序时需要此 ARN。
步骤 2:将生命周期配置附加到域、用户配置文件或共享空间
要附加生命周期配置,必须更新域的 UserSettings
或用户配置文件或共享空间的 SpaceSettings
。在域级别关联的生命周期配置脚本由所有用户继承。不过,在用户配置文件级别关联的脚本的作用域是特定用户,而在共享空间级别关联的脚本的作用域是共享空间。
下面的示例说明如何创建一个附加生命周期配置的新用户配置文件。您也可以分别使用 create-domain
将上一步中的生命周期配置 ARN 添加到相应应用程序类型的设置中。例如,将此项放在用户的 JupyterServerAppSettings
中。您可以通过传递生命周期配置列表来同时添加多个生命周期配置。当用户使用 Amazon CLI 启动 JupyterServer 应用程序时,他们可以传递要使用的生命周期配置,而不是默认配置。用户传递的生命周期配置必须属于 JupyterServerAppSettings
中的生命周期配置列表。
# Create a new UserProfile aws sagemaker create-user-profile --domain-id
domain-id
\ --user-profile-nameuser-profile-name
\ --regionregion
\ --user-settings '{ "JupyterServerAppSettings": { "LifecycleConfigArns": [lifecycle-configuration-arn-list
] } }'
以下示例演示如何更新现有共享空间以附加生命周期配置。您还可以使用 update-domain
aws sagemaker update-space --domain-id
domain-id
\ --space-namespace-name
\ --regionregion
\ --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
\ --regionregion
\ --user-profile-nameuser-profile-name
\ --app-type JupyterServer \ --resource-spec LifecycleConfigArn=lifecycle-configuration-arn
\ --app-name default -
下面的示例演示如何创建关联生命周期配置的
KernelGateway
应用程序。aws sagemaker create-app --domain-id
domain-id
\ --regionregion
\ --user-profile-nameuser-profile-name
\ --app-type KernelGateway \ --resource-spec LifecycleConfigArn=lifecycle-configuration-arn
,SageMakerImageArn=sagemaker-image-arn
,InstanceType=instance-type
\ --app-nameapp-name