As well as many other features, Amazon SageMaker provides Jupyter-based data science notebook environments on the cloud in two flavours:
- Classic SageMaker Notebook Instances, for a straightforward managed Jupyter/JupyterLab+conda experience
- Amazon SageMaker Studio, a fully integrated development environment (IDE) for machine learning: Extending JupyterLab with additional features and integrations
In either case, users may wish to automatically trigger the execution of code in SageMaker notebook environments - to customise the out-of-the-box setup or run any other required jobs or processes.
This repository demonstrates some basic code examples using the same Jupyter Server REST API and Jupyter Client WebSocket API (plus some SageMaker Studio extensions).
- SMStudio Remote Notebook.ipynb: Demo code to run all code cells of an (existing) notebook as a SageMaker Studio user
- SMStudio Remote Terminal.ipynb: Demo code to open a System Terminal and run commands as a SageMaker Studio user
- SMStudio-Run-On-Start: A NBI lifecycle-configuration-like example for SageMaker Studio - running a setup script whenever a user re/starts their JupyterServer app.
Many of the concepts in these examples are extensible and transferable (e.g. to classic NBIs), but in some settings alternative patterns may be more appropriate:
- To execute code on creation or start-up of a classic notebook instance, use Lifecycle configuration scripts
- For scheduling notebooks to "run" as parameterized SageMaker processing jobs, see this AWS blog post with Papermill
As these examples highlight, providing the IAM sagemaker:CreatePresignedDomainUrl
(for Studio) and sagemaker:CreatePresignedNotebookInstanceUrl
(for NBI) permissions is sufficient to grant login to the target SageMaker environment and also code execution on that environment.
For more information on conditions you can apply to scope down these permissions, refer to the IAM reference for Amazon SageMaker.