# SagerMaker Usage through AWS CLI
This document demonstrates the use of AWS CLI on SageMaker Notebook resources. It demonstrates and describes the usage for creating and querying notebooks through the AWS CLI. Additionally, a lifecycle configuration is created and applied to the created notebooks. Finally, a UML sequence diagram related to the scenario is shown.
## Requirements
The SageMaker Service can be accessed through the appropriate APIs. The API provides creating and managing Amazon SageMaker resources. Here numerous commands are provided where the user can create, delete, update, write, list, stop, and start SageMaker resources. This requires the prior installation of AWS CLI on the PC. The second version of the CLI (AWS CLI v2) was installed for this task.
However, after installing AWS CLI, some configuration is still required. The secret and access key must be initialized for the CLI and the connection setup. The AWS Secret Key and the Access Key can be accessed via the corresponding IAM user. For testing purposes, a user with administration rights was created. The following cmlets are used to initialize the keys:
```shell=
$ aws configure
AWS Access Key ID : AKIAV2MMYY7VG6G2H6UF
AWS Secret Access Key : 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
Default region name : us-west-2
Default output format : json
```
After the keys are initialized, the console is now ready to execute AWS commands:
```shell=
PS C:\Users\Atilla> aws
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
```
In this use case, the commands for SageMaker are executed as follows:
```shell=
PS C:\Users\Atilla> aws sagemaker <command>
```
The cmdlets for SageMaker are collected and explained by AWS in a [API Reference documentation.](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/index.html#cli-aws-sagemaker)
## Typical Scenario
A typical scenario is to create and start Jubyter Notebooks. The following demonstrates this scenario using the AWS CLI. Some cmdlets require the arn to be specified for execution. The corresponding arn can be found via the IAM user, with the corresponding SageMaker role.
The following figure shows the configuration of the created SageMaker role:

The needed arn-number is:
```
arn:aws:iam::400264054762:role/SageMaker-Role
```
The following cmdlet creates a Notebook instance using the AWS CLI. It passes the required parameters such as notebook-instance-name, instance-type, and role-arn. Optional parameters such as memory size, root access, and direct-internet-access can also be passed.
```shell=
aws sagemaker create-notebook-instance
--notebook-instance-name CLINotebookInstance
--instance-type ml.t2.medium
--role-arn arn:aws:iam::400264054762:role/SageMaker-Role
--direct-internet-access 'Enabled'
--tags Key=Name,Value='Created through AWS CLI'
--volume-size-in-gb 5
--root-access 'Enabled'
```
After the notebook instance has been created, it can be displayed as well as its properties:
```json=
PS C:\Users\Atilla> aws sagemaker list-notebook-instances
{
"NotebookInstances": [
{
"NotebookInstanceName": "CLINotebookInstance",
"NotebookInstanceArn": "arn:aws:sagemaker:us-west-2:400264054762:notebook-instance/clinotebookinstance",
"NotebookInstanceStatus": "Pending",
"Url": "clinotebookinstance.notebook.us-west-2.sagemaker.aws",
"InstanceType": "ml.t2.medium",
"CreationTime": "2021-02-08T12:09:04.675000+01:00",
"LastModifiedTime": "2021-02-08T12:09:16.263000+01:00"
}
]
}
```
In addition to the created Jupyter Notebook Instance, a lifecycle configuration was created with a script.
This scripts checks if a notebook is idle for 60 minutes, if it does, it'll stop the notebook.
The corresponding script is available on [GitHub.](https://github.com/aws-samples/amazon-sagemaker-notebook-instance-lifecycle-config-samples/tree/master/scripts/auto-stop-idle)
```shell=
aws sagemaker create-notebook-instance-lifecycle-config --notebook-instance-lifecycle-config-name Lifecycle-config-group-b --on-start "\C:\Users\Atilla\Desktop\Autostop.py"
```
After the lifecycle configuration has been created, it can be queried:
```json=
PS C:\Users\Atilla> aws sagemaker list-notebook-instance-lifecycle-configs
{
"NotebookInstanceLifecycleConfigs": [
{
"NotebookInstanceLifecycleConfigName": "Autostop-group-b",
"NotebookInstanceLifecycleConfigArn": "arn:aws:sagemaker:us-west-2:400264054762:notebook-instance-lifecycle-config/autostop-group-b",
"CreationTime": "2021-02-04T14:08:21+01:00",
"LastModifiedTime": "2021-02-04T14:08:21+01:00"
}
]
}
```
The AWS console shows the corresponding configuration:

## UML Sequence Diagram
In the following, the previous scenario is represented by a UML sequence diagram:
