From 90d89ef20df247b8683b09d9a98ea646e6f18080 Mon Sep 17 00:00:00 2001 From: "Santiago L. Valdarrama" Date: Wed, 18 Oct 2023 13:46:41 -0400 Subject: [PATCH] ... --- penguins/studio-setup.ipynb | 111 ++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 55 deletions(-) diff --git a/penguins/studio-setup.ipynb b/penguins/studio-setup.ipynb index d40f1cf..7b73101 100644 --- a/penguins/studio-setup.ipynb +++ b/penguins/studio-setup.ipynb @@ -14,12 +14,30 @@ "This notebook is part of the [Machine Learning School](https://www.ml.school) program." ] }, + { + "cell_type": "code", + "execution_count": 1, + "id": "10aa4a4f", + "metadata": {}, + "outputs": [], + "source": [ + "import sys\n", + "from pathlib import Path\n", + "\n", + "CODE_FOLDER = Path(\"code\")\n", + "CODE_FOLDER.mkdir(parents=True, exist_ok=True)\n", + "\n", + "sys.path.append(f\"./{CODE_FOLDER}\")" + ] + }, { "cell_type": "markdown", "id": "78e86c92-8c57-4b4c-b6a8-1b8246dedf2a", "metadata": {}, "source": [ - "
Note: \n", + "## Step 1 - Update Domain and User Profile\n", + "\n", + "
\n", " Update the following constants with your SageMaker Domain Id and User Profile. You can find them in your Amazon SageMaker dashboard under \"Domains\".\n", "
" ] @@ -39,37 +57,20 @@ }, { "cell_type": "markdown", - "id": "843c0a29-57b3-4bb0-bb12-0c0225938ba7", - "metadata": { - "tags": [] - }, + "id": "48ba9189", + "metadata": {}, "source": [ - "## Step 1 - Permissions \n", - "\n", - "To run the notebooks we use during the program, you need to update the Execution Policy assigned to SageMaker's Execution Role and add the appropriate permissions.\n", - "\n", "The following cell displays the name of Execution Role you are currently using." ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "099706c2-b9a8-471e-a21d-3a132febc184", "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "'arn:aws:iam::325223348818:role/service-role/AmazonSageMaker-ExecutionRole-20230312T160501'" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "import sagemaker\n", "\n", @@ -77,17 +78,25 @@ "role" ] }, + { + "cell_type": "markdown", + "id": "843c0a29-57b3-4bb0-bb12-0c0225938ba7", + "metadata": { + "tags": [] + }, + "source": [ + "## Step 2 - Edit Execution Policy Permissions\n", + "\n", + "
\n", + " Open the Amazon IAM service, find the role and edit the custom Execution Policy assigned to it. You can edit the permissions of the Execution Policy and use the following definition:\n", + "
" + ] + }, { "cell_type": "markdown", "id": "46bc8e33-ea4f-4219-8501-1decad22653a", "metadata": {}, "source": [ - "
Note: \n", - " Open the Amazon IAM service, find the role and edit the custom Execution Policy assigned to it. You can edit the permissions of the Execution Policy and use the following definition:\n", - "
\n", - "\n", - "\n", - "\n", "```json\n", "{\n", " \"Version\": \"2012-10-17\",\n", @@ -206,7 +215,9 @@ "id": "018a9140-93d0-400e-846f-ccbd1d17355a", "metadata": {}, "source": [ - "
Note: \n", + "## Step 3 - Update IAM Role Trust Relationship\n", + "\n", + "
\n", " Open the Amazon IAM service, find the role, go to the \"Trust relationships\" section and edit the configuration using the JSON below. Here, we want to ensure we have the \"events.amazonaws.com\" as part of the list of services.\n", "
\n", "\n", @@ -237,7 +248,7 @@ "tags": [] }, "source": [ - "## Step 2 - Lifecycle Configuration\n", + "## Step 4 - Customize Kernel Libraries\n", "\n", "You can customize SageMaker Studio using Lifecycle configurations. These are shell scripts that will be triggered by lifecycle events, such as starting a new Studio notebook.\n", "\n", @@ -256,12 +267,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "Overwriting packages.sh\n" + "Writing code/packages.sh\n" ] } ], "source": [ - "%%writefile packages.sh\n", + "%%writefile {CODE_FOLDER}/packages.sh\n", "\n", "#!/bin/bash\n", "# This script upgrades the packages on a SageMaker \n", @@ -274,8 +285,7 @@ "pip install -q --upgrade scikit-learn==1.3.1\n", "pip install -q --upgrade PyYAML==6.0\n", "pip install -q --upgrade sagemaker\n", - "pip install -q --upgrade ipytest\n", - "pip install -q --upgrade pandas" + "pip install -q --upgrade ipytest" ] }, { @@ -288,7 +298,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 11, "id": "c7b41df5-d6e7-4abe-a04d-8d390a3dea4f", "metadata": { "tags": [] @@ -298,20 +308,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "arn:aws:sagemaker:us-east-1:325223348818:studio-lifecycle-config/packages\n", - "{\n", - " \"UserProfileArn\": \"arn:aws:sagemaker:us-east-1:325223348818:user-profile/d-givocgtibv1g/default-1682182522641\"\n", - "}\n" + "#!/bin/bash # This script upgrades the packages on a SageMaker # Studio Kernel Application. set -eux pip install -q --upgrade pip pip install -q --upgrade awscli boto3 pip install -q --upgrade scikit-learn==1.3.1 pip install -q --upgrade PyYAML==6.0 pip install -q --upgrade sagemaker pip install -q --upgrade ipytest\n" ] } ], "source": [ - "%%bash -s \"$DOMAIN_ID\" \"$USER_PROFILE\"\n", + "%%bash -s \"$DOMAIN_ID\" \"$USER_PROFILE\" \"$CODE_FOLDER\"\n", "\n", "DOMAIN_ID=$(echo \"$1\")\n", "USER_PROFILE=$(echo \"$2\")\n", "\n", - "LCC_CONTENT=`openssl base64 -A -in packages.sh`\n", + "LCC_CONTENT=`openssl base64 -A -in $3/packages.sh`\n", "\n", "aws sagemaker delete-studio-lifecycle-config \\\n", " --studio-lifecycle-config-name packages\n", @@ -338,6 +345,8 @@ "id": "b42c75df-6592-47ac-867a-1ab0955e1653", "metadata": {}, "source": [ + "## Step 5 - Set up Auto-Shutdown\n", + "\n", "The following script configures auto-shutdown of inactive kernels." ] }, @@ -358,7 +367,7 @@ } ], "source": [ - "%%writefile autoshutdown.sh\n", + "%%writefile {CODE_FOLDER}/autoshutdown.sh\n", "\n", "#!/bin/bash\n", "# This script installs the idle notebook auto-checker server extension to SageMaker Studio\n", @@ -454,12 +463,12 @@ }, "outputs": [], "source": [ - "%%bash -s \"$DOMAIN_ID\" \"$USER_PROFILE\"\n", + "%%bash -s \"$DOMAIN_ID\" \"$USER_PROFILE\" \"$CODE_FOLDER\"\n", "\n", "DOMAIN_ID=$(echo \"$1\")\n", "USER_PROFILE=$(echo \"$2\")\n", "\n", - "LCC_CONTENT=`openssl base64 -A -in autoshutdown.sh`\n", + "LCC_CONTENT=`openssl base64 -A -in $3/autoshutdown.sh`\n", "\n", "aws sagemaker delete-studio-lifecycle-config \\\n", " --studio-lifecycle-config-name autoshutdown 2> /dev/null\n", @@ -484,14 +493,6 @@ " }\n", " }'" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "85dde144-cc46-47b1-a6ed-09f6e8eafa05", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -1071,9 +1072,9 @@ } ], "kernelspec": { - "display_name": "Python 3 (TensorFlow 2.6 Python 3.8 CPU Optimized)", + "display_name": ".venv", "language": "python", - "name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-east-1:081325390199:image/tensorflow-2.6-cpu-py38-ubuntu20.04-v1" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -1085,7 +1086,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.2" + "version": "3.9.13" }, "toc-autonumbering": false, "toc-showcode": false,