By using AWS re:Post, you agree to the AWS re:Post Terms of Use

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

3 minute read
0

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

Resolution

To build a Lambda function that creates a SageMaker notebook instance, follow these 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 a SageMaker execution role. For more information about the permissions required for 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 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.

  6. Choose Create function.

  7. 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.")
  8. Choose the Configuration tab, then choose Environment variables.

  9. Under Environment variables, 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 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.

  11. Choose Save to save the environment variables.

  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. On top-right corner of the Test tab, choose Save.

  15. Choose Test.

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

AWS OFFICIAL
AWS OFFICIALUpdated 3 months ago