Skip to content

Instantly share code, notes, and snippets.

@AllardQuek
Last active July 11, 2021 18:15
Show Gist options
  • Save AllardQuek/830c6d11d5a8d82a26f1dbd2999a4d44 to your computer and use it in GitHub Desktop.
Save AllardQuek/830c6d11d5a8d82a26f1dbd2999a4d44 to your computer and use it in GitHub Desktop.
Run SageMaker notebook from AWS Lambda
import boto3
import time
from botocore.vendored import requests
from websocket_client import websocket
def lambda_handler(event, context):
sm_client = boto3.client('sagemaker')
notebook_instance_name = 'automate-nb'
url = sm_client.create_presigned_notebook_instance_url(NotebookInstanceName=notebook_instance_name)['AuthorizedUrl']
url_tokens = url.split('/')
http_proto = url_tokens[0]
http_hn = url_tokens[2].split('?')[0].split('#')[0]
s = requests.Session()
r = s.get(url)
cookies = "; ".join(key + "=" + value for key, value in s.cookies.items())
ws = websocket.create_connection(
"wss://{}/terminals/websocket/1".format(http_hn),
cookie=cookies,
host=http_hn,
origin=http_proto + "//" + http_hn
)
ws.send("""[ "stdin", "jupyter nbconvert --execute --to notebook --inplace /home/ec2-user/SageMaker/hello_world.ipynb --ExecutePreprocessor.kernel_name=python3 --ExecutePreprocessor.timeout=1500\\r" ]""")
# time.sleep(1)
ws.close()
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment