Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
svpino committed Oct 18, 2023
1 parent da70e46 commit 90d89ef
Showing 1 changed file with 56 additions and 55 deletions.
111 changes: 56 additions & 55 deletions penguins/studio-setup.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
"<div class=\"alert\" style=\"background-color:#6e420c; color: #fff\"><strong>Note:</strong> \n",
"## Step 1 - Update Domain and User Profile\n",
"\n",
"<div class=\"alert\" style=\"background-color:#6e420c; color: #fff\">\n",
" Update the following constants with your SageMaker <strong>Domain Id</strong> and <strong>User Profile</strong>. You can find them in your Amazon SageMaker dashboard under \"Domains\".\n",
"</div>"
]
Expand All @@ -39,55 +57,46 @@
},
{
"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",
"role = sagemaker.get_execution_role()\n",
"role"
]
},
{
"cell_type": "markdown",
"id": "843c0a29-57b3-4bb0-bb12-0c0225938ba7",
"metadata": {
"tags": []
},
"source": [
"## Step 2 - Edit Execution Policy Permissions\n",
"\n",
"<div class=\"alert\" style=\"background-color:#6e420c; color: #fff\">\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",
"</div>"
]
},
{
"cell_type": "markdown",
"id": "46bc8e33-ea4f-4219-8501-1decad22653a",
"metadata": {},
"source": [
"<div class=\"alert\" style=\"background-color:#6e420c; color: #fff\"><strong>Note:</strong> \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",
"</div>\n",
"\n",
"\n",
"\n",
"```json\n",
"{\n",
" \"Version\": \"2012-10-17\",\n",
Expand Down Expand Up @@ -206,7 +215,9 @@
"id": "018a9140-93d0-400e-846f-ccbd1d17355a",
"metadata": {},
"source": [
"<div class=\"alert\" style=\"background-color:#6e420c; color: #fff\"><strong>Note:</strong> \n",
"## Step 3 - Update IAM Role Trust Relationship\n",
"\n",
"<div class=\"alert\" style=\"background-color:#6e420c; color: #fff\">\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",
"</div>\n",
"\n",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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"
]
},
{
Expand All @@ -288,7 +298,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 11,
"id": "c7b41df5-d6e7-4abe-a04d-8d390a3dea4f",
"metadata": {
"tags": []
Expand All @@ -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",
Expand All @@ -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."
]
},
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -484,14 +493,6 @@
" }\n",
" }'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85dde144-cc46-47b1-a6ed-09f6e8eafa05",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down Expand Up @@ -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": {
Expand All @@ -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,
Expand Down

0 comments on commit 90d89ef

Please sign in to comment.