in assets/lambdas/lambdas.py [0:0]
def delete_notebook_instance(instance_name):
def _exists_with_status(instance_name, status):
notebooks_with_status = sm_client.list_notebook_instances(
NameContains=instance_name,
StatusEquals=status
)
return instance_name in [
ntbk['NotebookInstanceName'] for ntbk in notebooks_with_status['NotebookInstances']
]
if _exists_with_status(instance_name, 'Pending'):
waiter = sm_client.get_waiter('notebook_instance_in_service')
waiter.wait(NotebookInstanceName=instance_name)
if _exists_with_status(instance_name, 'InService'):
sm_client.stop_notebook_instance(
NotebookInstanceName=instance_name
)
waiter = sm_client.get_waiter('notebook_instance_stopped')
waiter.wait(NotebookInstanceName=instance_name)
if _exists_with_status(instance_name, 'Stopped'):
sm_client.delete_notebook_instance(NotebookInstanceName=instance_name)
else:
print(f'Warning! Cannot delete {instance_name}: not found in none of the states: pending, in service or stopped.')