以下是一个示例Lambda函数代码,可用于此解决方案:
import boto3
import os
def lambda_handler(event, context):
sm_client = boto3.client('sagemaker')
notebook_instance_name = event['detail']['NotebookInstanceName']
notebook_instance_id = event['detail']['NotebookInstanceLifecycleConfigArn'].split('/')[-1]
sm_client.stop_notebook_instance(
NotebookInstanceName=notebook_instance_name
)
return {
'statusCode': 200,
'body': f'Successfully stopped notebook instance {notebook_instance_name}'
}
您可以使用此代码作为Lambda函数并在SageMaker生命周期配置中将其作为停止操作指定。此函数将在Notebook实例停止时自动运行,从而避免出现意外的费用。