

# Use Lifecycle Configurations to Customize Amazon SageMaker Studio Classic
<a name="studio-lcc"></a>

**Important**  
As of November 30, 2023, the previous Amazon SageMaker Studio experience is now named Amazon SageMaker Studio Classic. The following section is specific to using the Studio Classic application. For information about using the updated Studio experience, see [Amazon SageMaker Studio](studio-updated.md).  
Studio Classic is still maintained for existing workloads but is no longer available for onboarding. You can only stop or delete existing Studio Classic applications and cannot create new ones. We recommend that you [migrate your workload to the new Studio experience](studio-updated-migrate.md).

Amazon SageMaker Studio Classic triggers lifecycle configurations shell scripts during important lifecycle events, such as starting a new Studio Classic notebook. You can use lifecycle configurations to automate customization for your Studio Classic environment. This customization includes installing custom packages, configuring notebook extensions, preloading datasets, and setting up source code repositories.

Using lifecycle configurations gives you flexibility and control to configure Studio Classic to meet your specific needs. For example, you can use customized container images with lifecycle configuration scripts to modify your environment. First, create a minimal set of base container images, then install the most commonly used packages and libraries in those images. After you have completed your images, use lifecycle configurations to install additional packages for specific use cases. This gives you the flexibility to modify your environment across your data science and machine learning teams based on need.

Users can only select lifecycle configuration scripts that they are given access to. While you can give access to multiple lifecycle configuration scripts, you can also set default lifecycle configuration scripts for resources. Based on the resource that the default lifecycle configuration is set for, the default either runs automatically or is the first option shown.

For example lifecycle configuration scripts, see the [Studio Classic Lifecycle Configuration examples GitHub repository](https://github.com/aws-samples/sagemaker-studio-lifecycle-config-examples). For a blog on implementing lifecycle configuration, see [Customize Amazon SageMaker Studio Classic using Lifecycle Configurations](https://aws.amazon.com/blogs/machine-learning/customize-amazon-sagemaker-studio-using-lifecycle-configurations/).

**Note**  
Each script has a limit of **16384 characters**.

**Topics**
+ [Create and Associate a Lifecycle Configuration with Amazon SageMaker Studio Classic](studio-lcc-create.md)
+ [Set Default Lifecycle Configurations for Amazon SageMaker Studio Classic](studio-lcc-defaults.md)
+ [Debug Lifecycle Configurations in Amazon SageMaker Studio Classic](studio-lcc-debug.md)
+ [Update and Detach Lifecycle Configurations in Amazon SageMaker Studio Classic](studio-lcc-delete.md)

# Create and Associate a Lifecycle Configuration with Amazon SageMaker Studio Classic
<a name="studio-lcc-create"></a>

**Important**  
As of November 30, 2023, the previous Amazon SageMaker Studio experience is now named Amazon SageMaker Studio Classic. The following section is specific to using the Studio Classic application. For information about using the updated Studio experience, see [Amazon SageMaker Studio](studio-updated.md).  
Studio Classic is still maintained for existing workloads but is no longer available for onboarding. You can only stop or delete existing Studio Classic applications and cannot create new ones. We recommend that you [migrate your workload to the new Studio experience](studio-updated-migrate.md).

Amazon SageMaker AI provides interactive applications that enable Studio Classic's visual interface, code authoring, and run experience. This series shows how to create a lifecycle configuration and associate it with a SageMaker AI domain.

Application types can be either `JupyterServer` or `KernelGateway`. 
+ **`JupyterServer` applications:** This application type enables access to the visual interface for Studio Classic. Every user and shared space in Studio Classic gets its own JupyterServer application.
+ **`KernelGateway` applications:** This application type enables access to the code run environment and kernels for your Studio Classic notebooks and terminals. For more information, see [Jupyter Kernel Gateway](https://jupyter-kernel-gateway.readthedocs.io/en/latest/).

For more information about Studio Classic's architecture and Studio Classic applications, see [Use Amazon SageMaker Studio Classic Notebooks](https://docs.aws.amazon.com/sagemaker/latest/dg/notebooks.html).

**Topics**
+ [Create a Lifecycle Configuration from the AWS CLI for Amazon SageMaker Studio Classic](studio-lcc-create-cli.md)
+ [Create a Lifecycle Configuration from the SageMaker AI Console for Amazon SageMaker Studio Classic](studio-lcc-create-console.md)

# Create a Lifecycle Configuration from the AWS CLI for Amazon SageMaker Studio Classic
<a name="studio-lcc-create-cli"></a>

**Important**  
Custom IAM policies that allow Amazon SageMaker Studio or Amazon SageMaker Studio Classic to create Amazon SageMaker resources must also grant permissions to add tags to those resources. The permission to add tags to resources is required because Studio and Studio Classic automatically tag any resources they create. If an IAM policy allows Studio and Studio Classic to create resources but does not allow tagging, "AccessDenied" errors can occur when trying to create resources. For more information, see [Provide permissions for tagging SageMaker AI resources](security_iam_id-based-policy-examples.md#grant-tagging-permissions).  
[AWS managed policies for Amazon SageMaker AI](security-iam-awsmanpol.md) that give permissions to create SageMaker resources already include permissions to add tags while creating those resources.

**Important**  
As of November 30, 2023, the previous Amazon SageMaker Studio experience is now named Amazon SageMaker Studio Classic. The following section is specific to using the Studio Classic application. For information about using the updated Studio experience, see [Amazon SageMaker Studio](studio-updated.md).  
Studio Classic is still maintained for existing workloads but is no longer available for onboarding. You can only stop or delete existing Studio Classic applications and cannot create new ones. We recommend that you [migrate your workload to the new Studio experience](studio-updated-migrate.md).

The following topic shows how to create a lifecycle configuration using the AWS CLI to automate customization for your Studio Classic environment.

## Prerequisites
<a name="studio-lcc-create-cli-prerequisites"></a>

Before you begin, complete the following prerequisites: 
+ Update the AWS CLI by following the steps in [Installing the current AWS CLI Version](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv1.html#install-tool-bundled).
+ From your local machine, run `aws configure` and provide your AWS credentials. For information about AWS credentials, see [Understanding and getting your AWS credentials](https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html). 
+ Onboard to SageMaker AI domain by following the steps in [Amazon SageMaker AI domain overview](gs-studio-onboard.md).

## Step 1: Create a lifecycle configuration
<a name="studio-lcc-create-cli-step1"></a>

The following procedure shows how to create a lifecycle configuration script that prints `Hello World`.

**Note**  
Each script can have up to **16,384 characters**.

1. From your local machine, create a file named `my-script.sh` with the following content.

   ```
   #!/bin/bash
   set -eux
   echo 'Hello World!'
   ```

1. Convert your `my-script.sh` file into base64 format. This requirement prevents errors that occur from spacing and line break encoding.

   ```
   LCC_CONTENT=`openssl base64 -A -in my-script.sh`
   ```

1. Create a lifecycle configuration for use with Studio Classic. The following command creates a lifecycle configuration that runs when you launch an associated `KernelGateway` application. 

   ```
   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
   ```

   Note the ARN of the newly created lifecycle configuration that is returned. This ARN is required to attach the lifecycle configuration to your application.

## Step 2: Attach the lifecycle configuration to your domain, user profile, or shared space
<a name="studio-lcc-create-cli-step2"></a>

To attach the lifecycle configuration, you must update the `UserSettings` for your domain or user profile, or the `SpaceSettings` for a shared space. Lifecycle configuration scripts that are associated at the domain level are inherited by all users. However, scripts that are associated at the user profile level are scoped to a specific user, while scripts that are associated at the shared space level are scoped to the shared space. 

The following example shows how to create a new user profile with the lifecycle configuration attached. You can also create a new domain or space with a lifecycle configuration attached by using the [create-domain](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/create-domain.html) and [create-space](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/create-space.html) commands, respectively.

Add the lifecycle configuration ARN from the previous step to the settings for the appropriate app type. For example, place it in the `JupyterServerAppSettings` of the user. You can add multiple lifecycle configurations at the same time by passing a list of lifecycle configurations. When a user launches a JupyterServer application with the AWS CLI, they can pass a lifecycle configuration to use instead of the default. The lifecycle configuration that the user passes must belong to the list of lifecycle configurations in `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]
  }
}'
```

The following example shows how to update an existing shared space to attach the lifecycle configuration. You can also update an existing domain or user profile with a lifecycle configuration attached by using the [update-domain](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/update-domain.html) or [update-user-profile](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/update-user-profile.html) command. When you update the list of lifecycle configurations attached, you must pass all lifecycle configurations as part of the list. If a lifecycle configuration is not part of this list, it will not be attached to the application.

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

For information about setting a default lifecycle configuration for a resource, see [Set Default Lifecycle Configurations for Amazon SageMaker Studio Classic](studio-lcc-defaults.md).

## Step 3: Launch application with lifecycle configuration
<a name="studio-lcc-create-cli-step3"></a>

After you attach a lifecycle configuration to a domain, user profile, or space, the user can select it when launching an application with the AWS CLI. This section describes how to launch an application with an attached lifecycle configuration. For information about changing the default lifecycle configuration after launching a JupyterServer application, see [Set Default Lifecycle Configurations for Amazon SageMaker Studio Classic](studio-lcc-defaults.md).

Launch the desired application type using the `create-app` command and specify the lifecycle configuration ARN in the `resource-spec` argument. 
+ The following example shows how to create a `JupyterServer` application with an associated lifecycle configuration. When creating the `JupyterServer`, the `app-name` must be `default`. The lifecycle configuration ARN passed as part of the `resource-spec` parameter must be part of the list of lifecycle configuration ARNs specified in `UserSettings` for your domain or user profile, or `SpaceSettings` for a shared space.

  ```
  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
  ```
+ The following example shows how to create a `KernelGateway` application with an associated lifecycle configuration.

  ```
  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
  ```

# Create a Lifecycle Configuration from the SageMaker AI Console for Amazon SageMaker Studio Classic
<a name="studio-lcc-create-console"></a>

**Important**  
Custom IAM policies that allow Amazon SageMaker Studio or Amazon SageMaker Studio Classic to create Amazon SageMaker resources must also grant permissions to add tags to those resources. The permission to add tags to resources is required because Studio and Studio Classic automatically tag any resources they create. If an IAM policy allows Studio and Studio Classic to create resources but does not allow tagging, "AccessDenied" errors can occur when trying to create resources. For more information, see [Provide permissions for tagging SageMaker AI resources](security_iam_id-based-policy-examples.md#grant-tagging-permissions).  
[AWS managed policies for Amazon SageMaker AI](security-iam-awsmanpol.md) that give permissions to create SageMaker resources already include permissions to add tags while creating those resources.

**Important**  
As of November 30, 2023, the previous Amazon SageMaker Studio experience is now named Amazon SageMaker Studio Classic. The following section is specific to using the Studio Classic application. For information about using the updated Studio experience, see [Amazon SageMaker Studio](studio-updated.md).  
Studio Classic is still maintained for existing workloads but is no longer available for onboarding. You can only stop or delete existing Studio Classic applications and cannot create new ones. We recommend that you [migrate your workload to the new Studio experience](studio-updated-migrate.md).

The following topic shows how to create a lifecycle configuration from the Amazon SageMaker AI console to automate customization for your Studio Classic environment.

## Prerequisites
<a name="studio-lcc-create-console-prerequisites"></a>

Before you can begin this tutorial, complete the following prerequisite:
+ Onboard to Amazon SageMaker Studio Classic. For more information, see [Onboard to Amazon SageMaker Studio Classic](https://docs.aws.amazon.com/sagemaker/latest/dg/gs-studio-onboard.html).

## Step 1: Create a new lifecycle configuration
<a name="studio-lcc-create-console-step1"></a>

You can create a lifecycle configuration by entering a script from the Amazon SageMaker AI console.

**Note**  
Each script can have up to **16,384 characters**.

The following procedure shows how to create a lifecycle configuration script that prints `Hello World`.

1. Open the Amazon SageMaker AI console at [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/).

1. On the left navigation pane, choose **Admin configurations**.

1. Under **Admin configurations**, choose **Lifecycle configurations**. 

1. Choose the **Studio** tab.

1. Choose **Create configuration**.

1. Under **Select configuration type**, select the type of application that the lifecycle configuration should be attached to. For more information about selecting which application to attach the lifecycle configuration to, see [Set Default Lifecycle Configurations for Amazon SageMaker Studio Classic](studio-lcc-defaults.md).

1. Choose **Next**.

1. In the section called **Configuration settings**, enter a name for your lifecycle configuration.

1. In the **Scripts** section, enter the following content.

   ```
   #!/bin/bash
   set -eux
   echo 'Hello World!'
   ```

1. (Optional) Create a tag for your lifecycle configuration.

1. Choose **Submit**.

## Step 2: Attach the lifecycle configuration to a domain or user profile
<a name="studio-lcc-create-console-step2"></a>

Lifecycle configuration scripts associated at the domain level are inherited by all users. However, scripts that are associated at the user profile level are scoped to a specific user. 

You can attach multiple lifecycle configurations to a domain or user profile for both JupyterServer and KernelGateway applications.

**Note**  
To attach a lifecycle configuration to a shared space, you must use the AWS CLI. For more information, see [Create a Lifecycle Configuration from the AWS CLI for Amazon SageMaker Studio Classic](studio-lcc-create-cli.md).

The following sections show how to attach a lifecycle configuration to your domain or user profile.

### Attach to a domain
<a name="studio-lcc-create-console-step2-domain"></a>

The following shows how to attach a lifecycle configuration to your existing domain from the SageMaker AI console.

1. Open the Amazon SageMaker AI console at [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/).

1. On the left navigation pane, choose **Admin configurations**.

1. Under **Admin configurations**, choose **domains**. 

1. From the list of domains, select the domain to attach the lifecycle configuration to.

1. From the **Domain details**, choose the **Environment** tab.

1. Under **Lifecycle configurations for personal Studio apps**, choose **Attach**.

1. Under **Source**, choose **Existing configuration**.

1. Under **Studio lifecycle configurations**, select the lifecycle configuration that you created in the previous step.

1. Select **Attach to domain**.

### Attach to your user profile
<a name="studio-lcc-create-console-step2-userprofile"></a>

The following shows how to attach a lifecycle configuration to your existing user profile.

1. Open the Amazon SageMaker AI console at [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/).

1. On the left navigation pane, choose **Admin configurations**.

1. Under **Admin configurations**, choose **domains**. 

1. From the list of domains, select the domain that contains the user profile to attach the lifecycle configuration to.

1. Under **User profiles**, select the user profile.

1. From the **User Details** page, choose **Edit**.

1. On the left navigation, choose **Studio settings**.

1. Under **Lifecycle configurations attached to user**, choose **Attach**.

1. Under **Source**, choose **Existing configuration**.

1. Under **Studio lifecycle configurations**, select the lifecycle configuration that you created in the previous step.

1. Choose **Attach to user profile**.

## Step 3: Launch an application with the lifecycle configuration
<a name="studio-lcc-create-console-step3"></a>

After you attach a lifecycle configuration to a domain or user profile, you can launch an application with that attached lifecycle configuration. Choosing which lifecycle configuration to launch with depends on the application type.
+ **JupyterServer**: When launching a JupyterServer application from the console, SageMaker AI always uses the default lifecycle configuration. You can't use a different lifecycle configuration when launching from the console. For information about changing the default lifecycle configuration after launching a JupyterServer application, see [Set Default Lifecycle Configurations for Amazon SageMaker Studio Classic](studio-lcc-defaults.md).

  To select a different attached lifecycle configuration, you must launch with the AWS CLI. For more information about launching a JupyterServer application with an attached lifecycle configuration from the AWS CLI, see [Create a Lifecycle Configuration from the AWS CLI for Amazon SageMaker Studio Classic](studio-lcc-create-cli.md).
+ **KernelGateway**: You can select any of the attached lifecycle configurations when launching a KernelGateway application using the Studio Classic Launcher.

The following procedure describes how to launch a KernelGateway application with an attached lifecycle configuration from the SageMaker AI console.

1. Open the Amazon SageMaker AI console at [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/).

1. Launch Studio Classic. For more information, see [Launch Amazon SageMaker Studio Classic](studio-launch.md).

1. In the Studio Classic UI, open the Studio Classic Launcher. For more information, see [Use the Amazon SageMaker Studio Classic Launcher](studio-launcher.md). 

1. In the Studio Classic Launcher, navigate to the **Notebooks and compute resources** section. 

1. Click the **Change environment** button.

1. On the **Change environment** dialog, use the dropdown menus to select your **Image**, **Kernel**, **Instance type**, and a **Start-up script**. If there is no default lifecycle configuration, the **Start-up script** value defaults to `No script`. Otherwise, the **Start-up script** value is your default lifecycle configuration. After you select a lifecycle configuration, you can view the entire script.

1. Click **Select**.

1. Back to the Launcher, click the **Create notebook** to launch a new notebook kernel with your selected image and lifecycle configuration.

## Step 4: View logs for a lifecycle configuration
<a name="studio-lcc-create-console-step4"></a>

You can view the logs for your lifecycle configuration after it has been attached to a domain or user profile. 

1. First, provide access to CloudWatch for your AWS Identity and Access Management (IAM) role. Add read permissions for the following log group and log stream.
   + **Log group:**`/aws/sagemaker/studio`
   + **Log stream:**`domain/user-profile/app-type/app-name/LifecycleConfigOnStart`

    For information about adding permissions, see [Enabling logging from certain AWS services](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AWS-logs-and-resource-policy.html).

1. From within Studio Classic, navigate to the **Running Terminals and Kernels** icon (![\[Black square icon representing a placeholder or empty image.\]](http://docs.aws.amazon.com/sagemaker/latest/dg/images/studio/icons/running-terminals-kernels.png)) to monitor your lifecycle configuration.

1. Select an application from the list of running applications. Applications with attached lifecycle configurations have an attached indicator icon ![\[Code brackets symbol representing programming or markup languages.\]](http://docs.aws.amazon.com/sagemaker/latest/dg/images/studio/studio-lcc-indicator-icon.png).

1. Select the indicator icon for your application. This opens a new panel that lists the lifecycle configuration.

1. From the new panel, select `View logs`. This opens a new tab that displays the logs.

# Set Default Lifecycle Configurations for Amazon SageMaker Studio Classic
<a name="studio-lcc-defaults"></a>

**Important**  
As of November 30, 2023, the previous Amazon SageMaker Studio experience is now named Amazon SageMaker Studio Classic. The following section is specific to using the Studio Classic application. For information about using the updated Studio experience, see [Amazon SageMaker Studio](studio-updated.md).  
Studio Classic is still maintained for existing workloads but is no longer available for onboarding. You can only stop or delete existing Studio Classic applications and cannot create new ones. We recommend that you [migrate your workload to the new Studio experience](studio-updated-migrate.md).

Although you can attach multiple lifecycle configuration scripts to a single resource, you can only set one default lifecycle configuration for each JupyterServer or KernelGateway application. The behavior of the default lifecycle configuration depends on whether it is set for JupyterServer or KernelGateway apps. 
+ **JupyterServer apps:** When set as the default lifecycle configuration script for JupyterServer apps, the lifecycle configuration script runs automatically when the user signs in to Studio Classic for the first time or restarts Studio Classic. Use this default lifecycle configuration to automate one-time setup actions for the Studio Classic developer environment, such as installing notebook extensions or setting up a GitHub repo. For an example of this, see [Customize Amazon SageMaker Studio using Lifecycle Configurations](https://aws.amazon.com/blogs/machine-learning/customize-amazon-sagemaker-studio-using-lifecycle-configurations/).
+ **KernelGateway apps:** When set as the default lifecycle configuration script for KernelGateway apps, the lifecycle configuration is selected by default in the Studio Classic launcher. Users can launch a notebook or terminal with the default script selected, or they can select a different one from the list of lifecycle configurations.

SageMaker AI supports setting a default lifecycle configuration for the following resources:
+ Domains
+ User profiles
+ Shared spaces

While domains and user profiles support setting a default lifecycle configuration from both the Amazon SageMaker AI console and AWS Command Line Interface, shared spaces only support setting a default lifecycle configuration from the AWS CLI.

You can set a lifecycle configuration as the default when creating a new resource or updating an existing resource. The following topics demonstrate how to set a default lifecycle configuration using the SageMaker AI console and AWS CLI.

## Default lifecycle configuration inheritance
<a name="studio-lcc-defaults-inheritance"></a>

Default lifecycle configurations set at the *domain* level are inherited by all users and shared spaces. Default lifecycle configurations set at the *user* and *shared space* level are scoped to only that user or shared space. User and space defaults override defaults set at the domain level.

A default KernelGateway lifecycle configuration set for a domain applies to all KernelGateway applications launched in the domain. Unless the user selects a different lifecycle configuration from the list presented in the Studio Classic launcher, the default lifecycle configuration is used. The default script also runs if `No Script` is selected by the user. For more information about selecting a script, see [Step 3: Launch an application with the lifecycle configuration](studio-lcc-create-console.md#studio-lcc-create-console-step3).

**Topics**
+ [Default lifecycle configuration inheritance](#studio-lcc-defaults-inheritance)
+ [Set Defaults from the AWS CLI for Amazon SageMaker Studio Classic](studio-lcc-defaults-cli.md)
+ [Set Defaults from the SageMaker AI Console for Amazon SageMaker Studio Classic](studio-lcc-defaults-console.md)

# Set Defaults from the AWS CLI for Amazon SageMaker Studio Classic
<a name="studio-lcc-defaults-cli"></a>

**Important**  
Custom IAM policies that allow Amazon SageMaker Studio or Amazon SageMaker Studio Classic to create Amazon SageMaker resources must also grant permissions to add tags to those resources. The permission to add tags to resources is required because Studio and Studio Classic automatically tag any resources they create. If an IAM policy allows Studio and Studio Classic to create resources but does not allow tagging, "AccessDenied" errors can occur when trying to create resources. For more information, see [Provide permissions for tagging SageMaker AI resources](security_iam_id-based-policy-examples.md#grant-tagging-permissions).  
[AWS managed policies for Amazon SageMaker AI](security-iam-awsmanpol.md) that give permissions to create SageMaker resources already include permissions to add tags while creating those resources.

**Important**  
As of November 30, 2023, the previous Amazon SageMaker Studio experience is now named Amazon SageMaker Studio Classic. The following section is specific to using the Studio Classic application. For information about using the updated Studio experience, see [Amazon SageMaker Studio](studio-updated.md).  
Studio Classic is still maintained for existing workloads but is no longer available for onboarding. You can only stop or delete existing Studio Classic applications and cannot create new ones. We recommend that you [migrate your workload to the new Studio experience](studio-updated-migrate.md).

You can set default lifecycle configuration scripts from the AWS CLI for the following resources:
+ Domains
+ User profiles
+ Shared spaces

The following sections outline how to set default lifecycle configuration scripts from the AWS CLI.

**Topics**
+ [Prerequisites](#studio-lcc-defaults-cli-prereq)
+ [Set a default lifecycle configuration when creating a new resource](#studio-lcc-defaults-cli-new)
+ [Set a default lifecycle configuration for an existing resource](#studio-lcc-defaults-cli-existing)

## Prerequisites
<a name="studio-lcc-defaults-cli-prereq"></a>

Before you begin, complete the following prerequisites:
+ Update the AWS CLI by following the steps in [Installing the current AWS CLI version](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv1.html#install-tool-bundled).
+ From your local machine, run `aws configure` and provide your AWS credentials. For information about AWS credentials, see [Understanding and getting your AWS credentials](https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html). 
+ Onboard to SageMaker AI domain by following the steps in [Amazon SageMaker AI domain overview](gs-studio-onboard.md).
+ Create a lifecycle configuration following the steps in [Create and Associate a Lifecycle Configuration with Amazon SageMaker Studio Classic](studio-lcc-create.md).

## Set a default lifecycle configuration when creating a new resource
<a name="studio-lcc-defaults-cli-new"></a>

To set a default lifecycle configuration when creating a new domain, user profile, or space, pass the ARN of your previously created lifecycle configuration as part of one of the following AWS CLI commands:
+ [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/opensearch/create-domain.html)
+ [create-space](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/create-space.html)

You must pass the lifecycle configuration ARN for the following values in the KernelGateway or JupyterServer default settings:
+ `DefaultResourceSpec`:`LifecycleConfigArn` - This specifies the default lifecycle configuration for the application type.
+ `LifecycleConfigArns` - This is the list of all lifecycle configurations attached to the application type. The default lifecycle configuration must also be part of this list.

For example, the following API call creates a new user profile with a default lifecycle configuration.

```
aws sagemaker create-user-profile --domain-id domain-id \
--user-profile-name user-profile-name \
--region region \
--user-settings '{
"KernelGatewayAppSettings": {
    "DefaultResourceSpec": { 
            "InstanceType": "ml.t3.medium",
            "LifecycleConfigArn": "lifecycle-configuration-arn"
         },
    "LifecycleConfigArns": [lifecycle-configuration-arn-list]
  }
}'
```

## Set a default lifecycle configuration for an existing resource
<a name="studio-lcc-defaults-cli-existing"></a>

To set or update the default lifecycle configuration for an existing resource, pass the ARN of your previously created lifecycle configuration as part of one of the following AWS CLI commands:
+ [update-user-profile](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/update-user-profile.html)
+ [update-domain](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/update-domain.html)
+ [update-space](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/update-space.html)

You must pass the lifecycle configuration ARN for the following values in the KernelGateway or JupyterServer default settings:
+ `DefaultResourceSpec`:`LifecycleConfigArn` - This specifies the default lifecycle configuration for the application type.
+ `LifecycleConfigArns` - This is the list of all lifecycle configurations attached to the application type. The default lifecycle configuration must also be part of this list.

For example, the following API call updates a user profile with a default lifecycle configuration.

```
aws sagemaker update-user-profile --domain-id domain-id \
--user-profile-name user-profile-name \
--region region \
--user-settings '{
"KernelGatewayAppSettings": {
    "DefaultResourceSpec": {
            "InstanceType": "ml.t3.medium",
            "LifecycleConfigArn": "lifecycle-configuration-arn"
         },
    "LifecycleConfigArns": [lifecycle-configuration-arn-list]
  }
}'
```

The following API call updates a domain to set a new default lifecycle configuration.

```
aws sagemaker update-domain --domain-id domain-id \
--region region \
--default-user-settings '{
"JupyterServerAppSettings": {
    "DefaultResourceSpec": {
            "InstanceType": "system",
            "LifecycleConfigArn": "lifecycle-configuration-arn"
         },
    "LifecycleConfigArns": [lifecycle-configuration-arn-list]
  }
}'
```

# Set Defaults from the SageMaker AI Console for Amazon SageMaker Studio Classic
<a name="studio-lcc-defaults-console"></a>

**Important**  
Custom IAM policies that allow Amazon SageMaker Studio or Amazon SageMaker Studio Classic to create Amazon SageMaker resources must also grant permissions to add tags to those resources. The permission to add tags to resources is required because Studio and Studio Classic automatically tag any resources they create. If an IAM policy allows Studio and Studio Classic to create resources but does not allow tagging, "AccessDenied" errors can occur when trying to create resources. For more information, see [Provide permissions for tagging SageMaker AI resources](security_iam_id-based-policy-examples.md#grant-tagging-permissions).  
[AWS managed policies for Amazon SageMaker AI](security-iam-awsmanpol.md) that give permissions to create SageMaker resources already include permissions to add tags while creating those resources.

**Important**  
As of November 30, 2023, the previous Amazon SageMaker Studio experience is now named Amazon SageMaker Studio Classic. The following section is specific to using the Studio Classic application. For information about using the updated Studio experience, see [Amazon SageMaker Studio](studio-updated.md).  
Studio Classic is still maintained for existing workloads but is no longer available for onboarding. You can only stop or delete existing Studio Classic applications and cannot create new ones. We recommend that you [migrate your workload to the new Studio experience](studio-updated-migrate.md).

You can set default lifecycle configuration scripts from the SageMaker AI console for the following resources.
+ Domains
+ User profiles

You cannot set default lifecycle configuration scripts for shared spaces from the SageMaker AI console. For information about setting defaults for shared spaces, see [Set Defaults from the AWS CLI for Amazon SageMaker Studio Classic](studio-lcc-defaults-cli.md).

The following sections outline how to set default lifecycle configuration scripts from the SageMaker AI console.

**Topics**
+ [Prerequisites](#studio-lcc-defaults-cli-prerequisites)
+ [Set a default lifecycle configuration for a domain](#studio-lcc-defaults-cli-domain)
+ [Set a default lifecycle configuration for a user profile](#studio-lcc-defaults-cli-user-profile)

## Prerequisites
<a name="studio-lcc-defaults-cli-prerequisites"></a>

Before you begin, complete the following prerequisites:
+ Onboard to SageMaker AI domain by following the steps in [Amazon SageMaker AI domain overview](gs-studio-onboard.md).
+ Create a lifecycle configuration following the steps in [Create and Associate a Lifecycle Configuration with Amazon SageMaker Studio Classic](studio-lcc-create.md).

## Set a default lifecycle configuration for a domain
<a name="studio-lcc-defaults-cli-domain"></a>

The following procedure shows how to set a default lifecycle configuration for a domain from the SageMaker AI console.

1. Open the Amazon SageMaker AI console at [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/).

1. From the list of domains, select the name of the domain to set the default lifecycle configuration for.

1. From the **Domain details** page, choose the **Environment** tab.

1. Under **Lifecycle configurations for personal Studio apps**, select the lifecycle configuration that you want to set as the default for the domain. You can set distinct defaults for JupyterServer and KernelGateway applications.

1. Choose **Set as default**. This opens a pop up window that lists the current defaults for JupyterServer and KernelGateway applications.

1. Choose **Set as default** to set the lifecycle configuration as the default for its respective application type.

## Set a default lifecycle configuration for a user profile
<a name="studio-lcc-defaults-cli-user-profile"></a>

The following procedure shows how to set a default lifecycle configuration for a user profile from the SageMaker AI console.

1. Open the Amazon SageMaker AI console at [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/).

1. From the list of domains, select the name of the domain that contains the user profile that you want to set the default lifecycle configuration for.

1. From the **Domain details** page, choose the **User profiles** tab.

1. Select the name of the user profile to set the default lifecycle configuration for. This opens a **User Details** page.

1. From the **User Details** page, choose **Edit**. This opens an **Edit user profile** page.

1. From the **Edit user profile** page, choose **Step 2 Studio settings**.

1. Under **Lifecycle configurations attached to user**, select the lifecycle configuration that you want to set as the default for the user profile. You can set distinct defaults for JupyterServer and KernelGateway applications.

1. Choose **Set as default**. This opens a pop up window that lists the current defaults for JupyterServer and KernelGateway applications.

1. Choose **Set as default** to set the lifecycle configuration as the default for its respective application type.

# Debug Lifecycle Configurations in Amazon SageMaker Studio Classic
<a name="studio-lcc-debug"></a>

**Important**  
As of November 30, 2023, the previous Amazon SageMaker Studio experience is now named Amazon SageMaker Studio Classic. The following section is specific to using the Studio Classic application. For information about using the updated Studio experience, see [Amazon SageMaker Studio](studio-updated.md).  
Studio Classic is still maintained for existing workloads but is no longer available for onboarding. You can only stop or delete existing Studio Classic applications and cannot create new ones. We recommend that you [migrate your workload to the new Studio experience](studio-updated-migrate.md).

The following topics show how to get information about and debug your lifecycle configurations.

**Topics**
+ [Verify lifecycle configuration process from CloudWatch Logs](#studio-lcc-debug-logs)
+ [JupyterServer app failure](#studio-lcc-debug-jupyterserver)
+ [KernelGateway app failure](#studio-lcc-debug-kernel)
+ [Lifecycle configuration timeout](#studio-lcc-debug-timeout)

## Verify lifecycle configuration process from CloudWatch Logs
<a name="studio-lcc-debug-logs"></a>

Lifecycle configurations only log `STDOUT` and `STDERR`.

`STDOUT` is the default output for bash scripts. You can write to `STDERR` by appending `>&2` to the end of a bash command. For example, `echo 'hello'>&2`. 

Logs for your lifecycle configurations are published to your AWS account using Amazon CloudWatch. These logs can be found in the `/aws/sagemaker/studio` log stream in the CloudWatch console.

1. Open the CloudWatch console at [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/).

1. Choose **Logs** from the left side. From the dropdown menu, select **Log groups**.

1. On the **Log groups** page, search for `aws/sagemaker/studio`. 

1. Select the log group.

1. On the **Log group details** page, choose the **Log streams** tab.

1. To find the logs for a specific app, search the log streams using the following format:

   ```
   domain-id/space-name/app-type/default/LifecycleConfigOnStart
   ```

   For example, to find the lifecycle configuration logs for domain `d-m85lcu8vbqmz`, space name `i-sonic-js`, and application type `JupyterLab`, use the following search string:

   ```
   d-m85lcu8vbqmz/i-sonic-js/JupyterLab/default/LifecycleConfigOnStart
   ```

## JupyterServer app failure
<a name="studio-lcc-debug-jupyterserver"></a>

If your JupyterServer app crashes because of an issue with the attached lifecycle configuration, Studio Classic displays the following error message on the Studio Classic startup screen. 

```
Failed to create SageMaker Studio due to start-up script failure
```

Select the `View script logs` link to view the CloudWatch logs for your JupyterServer app.

In the case where the faulty lifecycle configuration is specified in the `DefaultResourceSpec` of your domain, user profile, or shared space, Studio Classic continues to use the lifecycle configuration even after restarting Studio Classic. 

To resolve this error, follow the steps in [Set Default Lifecycle Configurations for Amazon SageMaker Studio Classic](studio-lcc-defaults.md) to remove the lifecycle configuration script from the `DefaultResourceSpec` or select another script as the default. Then launch a new JupyterServer app.

## KernelGateway app failure
<a name="studio-lcc-debug-kernel"></a>

If your KernelGateway app crashes because of an issue with the attached lifecycle configuration, Studio Classic displays the error message in your Studio Classic Notebook. 

Choose `View script logs` to view the CloudWatch logs for your KernelGateway app.

In this case, your lifecycle configuration is specified in the Studio Classic Launcher when launching a new Studio Classic Notebook. 

To resolve this error, use the Studio Classic launcher to select a different lifecycle configuration or select `No script`.

**Note**  
A default KernelGateway lifecycle configuration specified in `DefaultResourceSpec` applies to all KernelGateway images in the domain, user profile, or shared space unless the user selects a different script from the list presented in the Studio Classic launcher. The default script also runs if `No Script` is selected by the user. For more information on selecting a script, see [Step 3: Launch an application with the lifecycle configuration](studio-lcc-create-console.md#studio-lcc-create-console-step3).

## Lifecycle configuration timeout
<a name="studio-lcc-debug-timeout"></a>

There is a lifecycle configuration timeout limitation of 5 minutes. If a lifecycle configuration script takes longer than 5 minutes to run, Studio Classic throws an error.

To resolve this error, ensure that your lifecycle configuration script completes in less than 5 minutes. 

To help decrease the run time of scripts, try the following:
+ Cut down on necessary steps. For example, limit which conda environments to install large packages in.
+ Run tasks in parallel processes.
+ Use the `nohup` command in your script to ensure that hangup signals are ignored and do not stop the execution of the script.

# Update and Detach Lifecycle Configurations in Amazon SageMaker Studio Classic
<a name="studio-lcc-delete"></a>

**Important**  
As of November 30, 2023, the previous Amazon SageMaker Studio experience is now named Amazon SageMaker Studio Classic. The following section is specific to using the Studio Classic application. For information about using the updated Studio experience, see [Amazon SageMaker Studio](studio-updated.md).  
Studio Classic is still maintained for existing workloads but is no longer available for onboarding. You can only stop or delete existing Studio Classic applications and cannot create new ones. We recommend that you [migrate your workload to the new Studio experience](studio-updated-migrate.md).

A lifecycle configuration script can't be changed after it's created. To update your script, you must create a new lifecycle configuration script and attach it to the respective domain, user profile, or shared space. For more information about creating and attaching the lifecycle configuration, see [Create and Associate a Lifecycle Configuration with Amazon SageMaker Studio Classic](studio-lcc-create.md).

The following topic shows how to detach a lifecycle configuration using the AWS CLI and SageMaker AI console.

**Topics**
+ [Prerequisites](#studio-lcc-delete-pre)
+ [Detach using the AWS CLI](#studio-lcc-delete-cli)

## Prerequisites
<a name="studio-lcc-delete-pre"></a>

Before detaching a lifecycle configuration, you must complete the following prerequisite.
+ To successfully detach a lifecycle configuration, no running application can be using the lifecycle configuration. You must first shut down the running applications as shown in [Shut Down and Update Amazon SageMaker Studio Classic and Apps](studio-tasks-update.md).

## Detach using the AWS CLI
<a name="studio-lcc-delete-cli"></a>

To detach a lifecycle configuration using the AWS CLI, remove the desired lifecycle configuration from the list of lifecycle configurations attached to the resource and pass the list as part of the respective command:
+ [update-user-profile](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/update-user-profile.html)
+ [update-domain](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/update-domain.html)
+ [update-space](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/update-space.html)

For example, the following command removes all lifecycle configurations for KernelGateways attached to the domain.

```
aws sagemaker update-domain --domain-id domain-id \
--region region \
--default-user-settings '{
"KernelGatewayAppSettings": {
  "LifecycleConfigArns":
    []
  }
}'
```