Thursday, 21 February 2019

boto3 'create_notebook_instance_lifecycle_config()' produces error in Notebook startup

I am trying to set up lifecycle configurations for Sagemaker notebooks over the aws api via boto3. From the docs it reads that a base64 encoded string of the configuration has to be provided.

I am using the following code:

with open(lifecycleconfig.sh, 'rb') as fp:
    file_content = fp.read()
config_string = base64.b64encode(file_content).decode('utf-8')
boto3.client('sagemaker').create_notebook_instance_lifecycle_config(
    NotebookInstanceLifecycleConfigName='mylifecycleconfig1',
    OnCreate=[
        {
            'Content': config_string
        },
    ],
)

With some lifecycleconfig.sh:

#!/bin/bash

set -e


This creates a lifecycle configuration which shows up in the web interface and whose content is seemingly identical to creating a config by hand: image.

However Notebooks using the lifecycle config created via boto3 will not start and the log file will show error:

/home/ec2-user/SageMaker/create_script.sh: line 2: $'\r': command not found
/home/ec2-user/SageMaker/create_script.sh: line 3: set: -
set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]

Moreover, if I copy paste the content of the corrupted config and create a new config by hand, the new one will now also not start.

How do I have to encode a bash script for a working aws lifecycle configuration?



from boto3 'create_notebook_instance_lifecycle_config()' produces error in Notebook startup

No comments:

Post a Comment