in assets/lambdas/lambdas.py [0:0]
def lambda_handler(event, context):
if event['RequestType'] == 'Delete':
try:
print('Started deleting SageMaker...')
print(str(event))
instance_name = event['ResourceProperties']['NotebookInstanceName']
delete_notebook_instance(instance_name)
delete_model(make_model_name(event))
delete_endpoint(make_endpoint_name(event))
delete_lifecycle_config(make_lifecycle_config_name(instance_name))
send_cfnresponse(event, context, CFN_SUCCESS, {})
except Exception as inst:
print(inst)
send_cfnresponse(event, context, CFN_FAILED, {})
elif event['RequestType'] == 'Create':
try:
region = os.environ['AWS_REGION']
instance_name = event['ResourceProperties']['NotebookInstanceName']
input_dict = {
'NotebookInstanceName': event['ResourceProperties']['NotebookInstanceName'],
'InstanceType': event['ResourceProperties']['NotebookInstanceType'],
'RoleArn': event['ResourceProperties']['SageMakerRoleArn'],
}
config_with_data = create_lifecycle_config(input_dict['NotebookInstanceName'], event,
region)
input_dict['LifecycleConfigName'] = config_with_data['config_name']
instance = sm_client.create_notebook_instance(**input_dict)
waiter = sm_client.get_waiter('notebook_instance_in_service')
waiter.wait(NotebookInstanceName=event['ResourceProperties']['NotebookInstanceName'])
print('Sagemager CLI response for creating instance')
print(str(instance))
response_data = {
'SageMakerNotebookURL': f'https://{instance_name}.notebook.{region}.sagemaker.aws/tree?',
}
send_cfnresponse(event, context, CFN_SUCCESS, response_data)
except Exception as ex:
print('Error!')
print(ex)
send_cfnresponse(event, context, CFN_FAILED, {})