Sagemaker Notebook Example using AWS CDK

Alien
Aug 9, 2022

In this article we are going to cover some of the most common properties we use to create and configure an Sagemaker notebook in AWS CDK.

At the time of writing I was using CDK version: 2.34.2

Importing Dependencies

from aws_cdk import (aws_sagemaker as sagemaker,)
import base64
import json

Notebook Instance LifecycleConfig

  • The script needs to be encoded
  • Limitation from the CDK — hence need to use CFN
with open(
"resources/startup.sh", "r"
) as fp:
script = fp.read()
lifecycle_config = sagemaker.CfnNotebookInstanceLifecycleConfig(
self,
"InstanceLifeCycleConfig",
notebook_instance_lifecycle_config_name="InstanceLifeCycleConfig",
on_start=[
sagemaker.CfnNotebookInstanceLifecycleConfig.NotebookInstanceLifecycleHookProperty(
content=base64.b64encode(script.encode("utf-8")).decode("utf-8")
)
],
)

Notebook Instance

notebook = sagemaker.CfnNotebookInstance(
self,
"NotebookInstance",
instance_type="ml.t3.medium",
lifecycle_config_name=lifecycle_config.notebook_instance_lifecycle_config_name,
platform_identifier="notebook-al2-v1",
notebook_instance_name="Notebook-Instance",
volume_size_in_gb=25,
)

In case you are not going to use lifecycle config while creating notebook instance you can comment out that part.

--

--