To build a Lambda function that creates a SageMaker notebook instance, follow these steps:
-
Create an AWS Identity and Access Management (IAM) Lambda execution role with an attached policy that allows the sagemaker:CreateNotebookInstance action. For example, create a role that has the AmazonSageMakerFullAccess policy attached that will allow the sagemaker:CreateNotebookInstance action.
-
Create a SageMaker execution role. For more information about the permissions required for SageMaker execution roles, see CreateNotebookInstance API: execution role permissions.
-
Open the Lambda console.
-
Choose Create function.
-
Choose Author from scratch, and then set the following parameters:
For Name, enter a name for your function.
For Runtime, select one of the Python options.
For Role, select Choose an existing role.
For Existing role, select the IAM role that allows the sagemaker:CreateNotebookInstance action. This is the role that you created in step 1.
-
Choose Create function.
-
In the Function code section, paste the following code:
import osimport boto3
import time
INSTANCE_TYPE = os.environ['INSTANCE_TYPE']
NOTEBOOK_NAME = os.environ['NOTEBOOK_NAME']
ROLE=os.environ['ROLE']
sagemaker = boto3.client('sagemaker')
def lambda_handler(event, context):
sagemaker_notebook = sagemaker.create_notebook_instance(
NotebookInstanceName = NOTEBOOK_NAME +'-'+str(int(time.time())),
InstanceType = INSTANCE_TYPE,
RoleArn=ROLE
)
print("New Amazon SageMaker notebook instance created.")
-
Choose the Configuration tab, then choose Environment variables.
-
Under Environment variables, choose Edit.
-
Choose Add environment variable, and then create three environment variables with the following options.
Enter the following values for the first environmental variable.
For Key, enter ROLE.
For Value, enter the Amazon Resource Name (ARN) for the SageMaker execution role. This is the role that you created in step 2.
Enter the following values for the second environmental variable.
For Key, enter INSTANCE_TYPE.
For Value, enter the instance type for the notebook instance. For more information, see Amazon EC2 Instance types.
Enter the following values for the third environmental variable.
For Key, enter NOTEBOOK_NAME.
For Value, enter a name for your notebook.
-
Choose Save to save the environment variables.
-
Choose the Test tab.
-
Under Test event, choose Create new event or Edit saved event, and then choose the saved event that you want to use. Or, use an empty ("{}") test event.
-
On top-right corner of the Test tab, choose Save.
-
Choose Test.
-
Open the SageMaker console to confirm that a notebook instance is initializing.
Note: If the Lambda function test times out, open the Lambda function. Scroll down to the Basic settings section, and then increase the timeout value. The default is three seconds.
After you create the Lambda function, you can create a trigger to automatically run the function based on an event in another AWS service. For more information, see Invoking Lambda with events from other AWS services.