How can I use a Lambda function to create an Amazon SageMaker notebook instance?

Last updated: 2020-06-16

How can I create an Amazon SageMaker notebook instance using an AWS Lambda function?

Resolution

1.    Create an AWS Identity and Access Management (IAM) Lambda execution role with an attached policy that allows the sagemaker:CreateNotebookInstance action. For example, you can create a role that has the AmazonSageMakerFullAccess policy attached to it.

2.    Create an Amazon SageMaker execution role. For more information about the permissions required for Amazon SageMaker execution roles, see CreateNotebookInstance API: Execution Role Permissions.

3.    Open the Lambda console.

4.    Choose Create function.

5.    Choose Author from scratch, and then configure the following options:

For Name, enter a name for your function.
For Runtime, choose one of the Python options.
For Role, choose 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.

6.    Choose Create function.

7.    In the Function code section, paste code similar to the following:

import os
import 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.")

8.    Scroll down to the Environment variables section, and then choose Edit.

9.    Choose Add environment variable, and then create three environment variables with the following options. Environment variables allow you to store configuration settings without changing the function code.

For Key, enter ROLE.
For Value, enter the Amazon Resource Name (ARN) for the Amazon SageMaker execution role. This is the role that you created in step 2.

For Key, enter INSTANCE_TYPE.
For Value, enter the instance type for the notebook instance. For more information, see Supported Instance Types and Availability Zones.

For Key, enter NOTEBOOK_NAME.
For Value, enter a name for your notebook.

10.    Choose Save to save the environment variables.

11.    In the top-right corner of the function configuration page, choose Save, and then choose Test. The test event can be empty ("{}").

12.    Open the Amazon SageMaker console to confirm that a notebook instance is initializing.

Note: If the Lambda function test times out, open the Lambda function again. Scroll down to the Basic settings section, and then increase the timeout value. The default is three seconds.

After you confirm that the function works, you can create a trigger to automatically run the function based on an event in another AWS service. For more information, see Using AWS Lambda with other services.


Did this article help you?

Anything we could improve?


Need more help?