def on_delete()

in deployment/solution-assistant/src/lambda_function.py [0:0]


def on_delete(event, __):
    solution_prefix = event["ResourceProperties"]["SolutionPrefix"]
    # Delete Monitoring Schedule
    delete_monitoring_schedule(f"{solution_prefix}-schedule")

    # remove sagemaker endpoints
    endpoint_names = [
        "{}-xgb-endpoint".format(solution_prefix)
    ]
    for endpoint_name in endpoint_names:
        delete_sagemaker_model(endpoint_name)
        delete_sagemaker_endpoint_config(endpoint_name)
        delete_sagemaker_endpoint(endpoint_name)

    # Try to empty the bucket then delete the model-data bucket 5 times
    # This is needed because the thread we open
    model_data_bucket = event["ResourceProperties"]["ModelDataBucketName"]
    s3_client = boto3.client("s3")
    for _ in range(5):
        delete_s3_objects(model_data_bucket)
        delete_s3_bucket(model_data_bucket)

        # Give the delete op time to finish
        time.sleep(10)

        try:
            _ = s3_client.head_bucket(Bucket=model_data_bucket)
        except s3_client.exceptions.ClientError:
            break # This is good, the bucket was deleted, so we just exit the loop

        # Otherwise wait a minute and try again
        time.sleep(60)