Skip to content

How do I use a Lambda function to create a SageMaker AI notebook instance?

3 minute read
0

I want to use an AWS Lambda function to create an Amazon SageMaker AI notebook instance.

Resolution

To build a Lambda function that creates a SageMaker AI notebook instance, complete the following steps:

  1. 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.

  2. Create an IAM execution role for SageMaker AI. Make sure that you attach the required permissions.

  3. Open the Lambda console.

  4. Choose Create function.

  5. Choose Author from scratch, and then set the following parameters:
    For Function name, enter a name for your function.
    For Runtime, select one of the Python options.
    Under Change default execution role, choose Use an existing role.
    For Existing role, select the IAM role that you created in step 1.

  6. Choose Create function.

  7. In the Code section, enter the following code:

    import osimport boto3import 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. Choose the Configuration tab, and then choose Environment variables.

  9. Choose Edit.

  10. 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 AI execution 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 Amazon Elastic Compute Cloud (Amazon EC2) instance type for the notebook instance.
    Enter the following values for the third environmental variable:
    For Key, enter NOTEBOOK_NAME.
    For Value, enter a name for your notebook.

  11. Choose Save.

  12. Choose the Test tab.

  13. 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.

  14. Choose Save, and then choose Test.

  15. Open the SageMaker AI console to confirm that a notebook instance is initializing.
    Note: If the Lambda function test times out, open the Lambda function. Choose Configuration, and go to the General Configuration 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.

AWS OFFICIALUpdated 3 months ago