Skip to content

Commit bdefae1

Browse files
Update Lambda Step Notebook with content table and few updates (#3298)
* updates to lambda step notebook * minor formatting changes * Change text based on feedback Co-authored-by: Shreya <[email protected]>
1 parent 76b9e4b commit bdefae1

File tree

1 file changed

+67
-25
lines changed

1 file changed

+67
-25
lines changed

sagemaker-pipelines/tabular/lambda-step/sagemaker-pipelines-lambda-step.ipynb

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"#### SageMaker Pipelines Lambda Step\n",
7+
"# SageMaker Pipelines Lambda Step\n",
88
"\n",
99
"This notebook illustrates how a Lambda function can be run as a step in a SageMaker Pipeline. \n",
1010
"\n",
1111
"The steps in this pipeline include -\n",
12-
"* Preprocessing the abalone dataset\n",
12+
"* Preprocessing the Abalone dataset\n",
1313
"* Train an XGBoost Model\n",
1414
"* Evaluate the model performance\n",
1515
"* Create a model\n",
16-
"* Deploy the model to a SageMaker Hosted Endpoint using a Lambda Function\n",
16+
"* Deploy the model to a SageMaker Hosted Endpoint using a Lambda Function, through SageMaker Pipelines\n",
1717
"\n",
1818
"A step to register the model into a Model Registry can be added to the pipeline using the `RegisterModel` step."
1919
]
@@ -22,11 +22,26 @@
2222
"cell_type": "markdown",
2323
"metadata": {},
2424
"source": [
25-
"#### Prerequisites\n",
25+
"## Contents\n",
2626
"\n",
27-
"The notebook execution role should have policies which enable the notebook to create a Lambda function. The Amazon managed policy `AmazonSageMakerPipelinesIntegrations` can be added to the notebook execution role. \n",
27+
"1. [Prerequisites](#Prerequisites)\n",
28+
"1. [Configuration Setup](#Configuration-Setup)\n",
29+
"1. [Data Preparation](#Data-Preparation)\n",
30+
"1. [Model Training and Evaluation](#Model-Training-and-Evaluation)\n",
31+
"1. [Setting up Lambda](#Setting-up-Lambda)\n",
32+
"1. [Execute the Pipeline](#Execute-the-Pipeline)\n",
33+
"1. [Clean up resources](#Clean-up-resources)"
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"metadata": {},
39+
"source": [
40+
"## Prerequisites\n",
41+
"\n",
42+
"The notebook execution role should have policies which enable the notebook to create a Lambda function. The Amazon managed policy `AmazonSageMakerPipelinesIntegrations` can be added to the notebook execution role to achieve the same effect\n",
2843
"\n",
29-
"The policy description is -\n",
44+
"The policy description is as follows:\n",
3045
"\n",
3146
"```\n",
3247
"\n",
@@ -80,14 +95,10 @@
8095
]
8196
},
8297
{
83-
"cell_type": "code",
84-
"execution_count": null,
98+
"cell_type": "markdown",
8599
"metadata": {},
86-
"outputs": [],
87100
"source": [
88-
"import sys\n",
89-
"\n",
90-
"!{sys.executable} -m pip install \"sagemaker>=2.51.0\""
101+
"Lets start by importing necessary packages and installing the SageMaker Python SDK"
91102
]
92103
},
93104
{
@@ -132,7 +143,31 @@
132143
"from sagemaker.workflow.condition_step import ConditionStep\n",
133144
"from sagemaker.workflow.functions import JsonGet\n",
134145
"\n",
135-
"from sagemaker.lambda_helper import Lambda"
146+
"from sagemaker.lambda_helper import Lambda\n",
147+
"import sys"
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": null,
153+
"metadata": {},
154+
"outputs": [],
155+
"source": [
156+
"!{sys.executable} -m pip install \"sagemaker>=2.51.0\""
157+
]
158+
},
159+
{
160+
"cell_type": "markdown",
161+
"metadata": {},
162+
"source": [
163+
"## Configuration Setup"
164+
]
165+
},
166+
{
167+
"cell_type": "markdown",
168+
"metadata": {},
169+
"source": [
170+
"Let's now configure the setup we need, which includes the session object from the SageMaker Python SDK, and neccessary configurations for the pipelines, such as object types, input and output buckets and so on."
136171
]
137172
},
138173
{
@@ -188,9 +223,9 @@
188223
"cell_type": "markdown",
189224
"metadata": {},
190225
"source": [
191-
"#### Data Preparation\n",
226+
"## Data Preparation\n",
192227
"\n",
193-
"An SKLearn processor is used to prepare the dataset for the Hyperparameter Tuning job. Using the script `preprocess.py`, the dataset is featurized and split into train, test, and validation datasets. \n",
228+
"A SKLearn processor is used to prepare the dataset for the Hyperparameter Tuning job. Using the script `preprocess.py`, the dataset is featurized and split into train, test, and validation datasets. \n",
194229
"\n",
195230
"The output of this step is used as the input to the TrainingStep"
196231
]
@@ -363,9 +398,16 @@
363398
"cell_type": "markdown",
364399
"metadata": {},
365400
"source": [
366-
"#### Model Training\n",
401+
"## Model Training and Evaluation\n",
367402
"\n",
368-
"Train an XGBoost model with the output of the ProcessingStep."
403+
"We will now train a XGBoost model using the SageMaker python SDK using the output of the ProcessingStep."
404+
]
405+
},
406+
{
407+
"cell_type": "markdown",
408+
"metadata": {},
409+
"source": [
410+
"#### Training the Model"
369411
]
370412
},
371413
{
@@ -429,7 +471,7 @@
429471
"cell_type": "markdown",
430472
"metadata": {},
431473
"source": [
432-
"#### Evaluate the model\n",
474+
"#### Evaluating the model\n",
433475
"\n",
434476
"Use a processing job to evaluate the model from the TrainingStep. If the output of the evaluation is True, a model will be created and a Lambda will be invoked to deploy the model to a SageMaker Endpoint. "
435477
]
@@ -553,7 +595,7 @@
553595
"cell_type": "markdown",
554596
"metadata": {},
555597
"source": [
556-
"#### Create the model\n",
598+
"#### Creating the final model object\n",
557599
"\n",
558600
"The model is created and the name of the model is provided to the Lambda function for deployment. The `CreateModelStep` dynamically assigns a name to the model."
559601
]
@@ -584,7 +626,7 @@
584626
"cell_type": "markdown",
585627
"metadata": {},
586628
"source": [
587-
"#### Create the Lambda Step\n",
629+
"## Setting up Lambda\n",
588630
"\n",
589631
"When defining the LambdaStep, the SageMaker Lambda helper class provides helper functions for creating the Lambda function. Users can either use the `lambda_func` argument to provide the function ARN to an already deployed Lambda function OR use the `Lambda` class to create a Lambda function by providing a script, function name and role for the Lambda function. \n",
590632
"\n",
@@ -621,7 +663,7 @@
621663
"\n",
622664
"def lambda_handler(event, context):\n",
623665
" \"\"\" \"\"\"\n",
624-
" sm_client = sagemaker.Session().sagemaker_client\n",
666+
" sm_client = boto3.client(\"sagemaker\")\n",
625667
"\n",
626668
" # The name of the model created in the Pipeline CreateModelStep\n",
627669
" model_name = event[\"model_name\"]\n",
@@ -657,9 +699,9 @@
657699
"cell_type": "markdown",
658700
"metadata": {},
659701
"source": [
660-
"#### IAM Role\n",
702+
"#### Setting up the custom IAM Role\n",
661703
"\n",
662-
"The Lambda function needs an IAM role that will allow it to deploy a SageMaker Endpoint. The role ARN must be provided in the LambdaStep. \n",
704+
"The Lambda function needs an IAM role that allows it to deploy a SageMaker Endpoint. The role ARN must be provided in the LambdaStep. \n",
663705
"\n",
664706
"The Lambda role should at minimum have policies to allow `sagemaker:CreateModel`, `sagemaker:CreateEndpointConfig`, `sagemaker:CreateEndpoint` in addition to the based Lambda execution policies. \n",
665707
"\n",
@@ -770,7 +812,7 @@
770812
"cell_type": "markdown",
771813
"metadata": {},
772814
"source": [
773-
"#### Execute the Pipeline"
815+
"## Execute the Pipeline"
774816
]
775817
},
776818
{
@@ -830,7 +872,7 @@
830872
"cell_type": "markdown",
831873
"metadata": {},
832874
"source": [
833-
"#### Cleaning up resources\n",
875+
"## Clean up resources\n",
834876
"\n",
835877
"Running the following cell will delete the following resources created in this notebook -\n",
836878
"* SageMaker Model\n",

0 commit comments

Comments
 (0)