{"id":13626242,"url":"https://github.com/aws-samples/amazon-sagemaker-notebook-instance-lifecycle-config-samples","last_synced_at":"2025-04-16T14:32:48.980Z","repository":{"id":37324074,"uuid":"187894150","full_name":"aws-samples/amazon-sagemaker-notebook-instance-lifecycle-config-samples","owner":"aws-samples","description":"A collection of sample scripts to customize Amazon SageMaker Notebook Instances using Lifecycle Configurations","archived":false,"fork":false,"pushed_at":"2024-04-02T02:06:07.000Z","size":101,"stargazers_count":422,"open_issues_count":12,"forks_count":250,"subscribers_count":20,"default_branch":"master","last_synced_at":"2024-11-08T16:42:46.631Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit-0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aws-samples.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-21T18:39:34.000Z","updated_at":"2024-11-05T20:23:01.000Z","dependencies_parsed_at":"2024-06-11T17:18:01.906Z","dependency_job_id":null,"html_url":"https://github.com/aws-samples/amazon-sagemaker-notebook-instance-lifecycle-config-samples","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aws-samples%2Famazon-sagemaker-notebook-instance-lifecycle-config-samples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aws-samples%2Famazon-sagemaker-notebook-instance-lifecycle-config-samples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aws-samples%2Famazon-sagemaker-notebook-instance-lifecycle-config-samples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aws-samples%2Famazon-sagemaker-notebook-instance-lifecycle-config-samples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aws-samples","download_url":"https://codeload.github.com/aws-samples/amazon-sagemaker-notebook-instance-lifecycle-config-samples/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249250820,"owners_count":21237961,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T21:02:13.804Z","updated_at":"2025-04-16T14:32:48.623Z","avatar_url":"https://github.com/aws-samples.png","language":"Shell","funding_links":[],"categories":["Shell"],"sub_categories":[],"readme":"## SageMaker Notebook Instance Lifecycle Config Samples\n\n### Overview\n\nA collection of sample scripts to customize [Amazon SageMaker Notebook Instances](https://docs.aws.amazon.com/sagemaker/latest/dg/nbi.html) using [Lifecycle Configurations](https://docs.aws.amazon.com/sagemaker/latest/dg/notebook-lifecycle-config.html)\n\nLifecycle Configurations provide a mechanism to customize Notebook Instances via shell scripts that are executed during the lifecycle of a Notebook Instance.\n\n#### Sample Scripts\n\n* [add-pypi-repository](scripts/add-pypi-repository) - This script adds a private PyPi repository in addition to or instead of pypi.org.\n* [auto-stop-idle](scripts/auto-stop-idle) - This script stops a SageMaker notebook once it's idle for more than 1 hour. (default time)\n* [connect-emr-cluster](scripts/connect-emr-cluster) - This script connects an EMR cluster to the Notebook Instance using SparkMagic.\n* [disable-uninstall-ssm-agent](scripts/disable-uninstall-ssm-agent) - This script disables and uninstalls the SSM Agent at startup.\n* [enable-fips-openssl-provider](scripts/enable-fips-openssl-provider) - This script enables the OpenSSL FIPS provider in each conda environment.\n* [execute-notebook-on-startup](scripts/execute-notebook-on-startup) - This script executes a Notebook file on the instance during startup.\n* [export-to-pdf-enable](scripts/export-to-pdf-enable) - This script enables Jupyter to export a notebook directly to PDF.\n* [install-conda-package-all-environments](scripts/install-conda-package-all-environments) - This script installs a single conda package in all SageMaker conda environments, apart from the JupyterSystemEnv which is a system environment reserved for Jupyter.\n* [install-conda-package-single-environment](scripts/install-conda-package-single-environment) - This script installs a single conda package in a single SageMaker conda environment.\n* [install-lab-extension](scripts/install-lab-extension) - This script installs a jupyterlab extension package in SageMaker Notebook Instance.\n* [install-nb-extension](scripts/install-nb-extension) - This script installs a single jupyter notebook extension package in SageMaker Notebook Instance.\n* [install-pip-package-all-environments](scripts/install-pip-package-all-environments) - This script installs a single pip package in all SageMaker conda environments, apart from the JupyterSystemEnv which is a system environment reserved for Jupyter.\n* [install-pip-package-single-environment](scripts/install-pip-package-single-environment) - This script installs a single pip package in a single SageMaker conda environments.\n* [install-r-package](scripts/install-r-package) - This script installs a single R package in SageMaker R environment.\n* [install-server-extension](scripts/install-server-extension) - This script installs a single jupyter notebook server extension package in SageMaker Notebook Instance.\n* [migrate-ebs-data-backup](scripts/migrate-ebs-data-backup) - This script backs up content in `/home/ec2-user/SageMaker/` to a S3 bucket specified in a tag on the notebook instance.\n* [migrate-ebs-data-sync](scripts/migrate-ebs-data-sync) - This script downloads a snapshot created by [migrate-ebs-data-backup](scripts/migrate-ebs-data-backup) to `/home/ec2-user/SageMaker/` in a new notebook instance. You specify the snapshop using tags on the notebook instance.\n* [mount-efs-file-system](scripts/mount-efs-file-system) - This script mounts an EFS file system to the Notebook Instance at the ~/SageMaker/efs directory based off the DNS name.\n* [mount-fsx-lustre-file-system](scripts/mount-fsx-lustre-file-system) - This script mounts an FSx for Lustre file system to the Notebook Instance at the /fsx directory based off the DNS and Mount name parameters.\n* [notebook-history-s3](scripts/notebook-history-s3) - This script persists the underlying sqlite database of commands and cells executed for S3.\n* [persistent-conda-ebs](scripts/persistent-conda-ebs) - This script installs a custom, persistent installation of conda on the Notebook Instance's EBS volume, and ensures that these custom environments are available as kernels in Jupyter.\n* [proxy-for-jupyter](scripts/proxy-for-jupyter) - This script configures proxy settings for your Jupyter notebooks and the SageMaker Notebook Instance.\n* [publish-instance-metrics](scripts/publish-instance-metrics) - This script publishes the system-level metrics from the Notebook Instance to CloudWatch.\n* [set-codecommit-cross-account-access](scripts/set-codecommit-cross-account-access) - This script sets cross-account CodeCommit access, so you can work on repositories hosted in another account.\n* [set-env-variable](scripts/set-env-variable) - This script gets a value from the Notebook Instance's tags and sets it as an environment variable for all processes including Jupyter.\n* [set-git-config](scripts/set-git-config) - This script sets the username and email address in Git config.\n\n### Development\n\nFor contributors looking to develop scripts, they can be developed directly on SageMaker Notebook Instances since that is the environment that they are run with. Lifecycle Configuration scripts run as `root`, the working directory is `/`.  To simulate the execution environment, you may use\n\n```bash\nsudo su\nexport PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin\ncd /\n```\n\nEdit the script in a file such as `my-script-on-start.sh` and execute it as\n\n```bash\nsh my-script-on-start.sh\n```\n\nThe directory structure followed is:\n\n```\nscripts/\n    my-script-name/\n        on-start.sh\n        on-create.sh\n```\n\n### Testing\n\nTo test the script end-to-end:\n\nCreate a [Lifecycle Configuration](https://docs.aws.amazon.com/sagemaker/latest/dg/API_CreateNotebookInstanceLifecycleConfig.html) with the script content and\na [Notebook Instance](https://docs.aws.amazon.com/sagemaker/latest/dg/API_CreateNotebookInstance.html) with the Lifecycle Configuration\n\n```bash\n# If the scripts are in a directory \"scripts/my-script-name/*\"\nSCRIPT_NAME=my-script-name\nROLE_ARN=my-role-arn\n\nRESOURCE_NAME=\"$SCRIPT_NAME-$RANDOM\"\n\n# Add any script specific options such as subnet-id\naws sagemaker create-notebook-instance-lifecycle-config \\\n    --notebook-instance-lifecycle-config-name \"$RESOURCE_NAME\" \\\n    --on-start Content=$((cat scripts/$SCRIPT_NAME/on-start.sh || echo \"\")| base64) \\\n    --on-create Content=$((cat scripts/$SCRIPT_NAME/on-create.sh || echo \"\")| base64)\n\naws sagemaker create-notebook-instance \\\n    --notebook-instance-name \"$RESOURCE_NAME\" \\\n    --instance-type ml.t2.medium \\\n    --role-arn \"$ROLE_ARN\" \\\n    --lifecycle-config-name \"$RESOURCE_NAME\"\n\naws sagemaker wait \\\n    notebook-instance-in-service \\\n    --notebook-instance-name \"$RESOURCE_NAME\"\n```\n\n* [Access](https://docs.aws.amazon.com/sagemaker/latest/dg/API_CreatePresignedNotebookInstanceUrl.html) the Notebook Instance and perform any validation specific to the script.\n\n```bash\naws sagemaker create-presigned-notebook-instance-url \\\n    --notebook-instance-name \"$RESOURCE_NAME\"\n```\n\n* [Stop](https://docs.aws.amazon.com/sagemaker/latest/dg/API_StopNotebookInstance.html) and [Start](https://docs.aws.amazon.com/sagemaker/latest/dg/API_StartNotebookInstance.html) the Notebook Instance\n\n```bash\naws sagemaker stop-notebook-instance \\\n    --notebook-instance-name \"$RESOURCE_NAME\"\n\naws sagemaker wait \\\n    notebook-instance-stopped \\\n    --notebook-instance-name \"$RESOURCE_NAME\"\n\naws sagemaker start-notebook-instance \\\n    --notebook-instance-name \"$RESOURCE_NAME\"\n\naws sagemaker wait \\\n    notebook-instance-in-service \\\n    --notebook-instance-name \"$RESOURCE_NAME\"\n```\n\n* [Access](https://docs.aws.amazon.com/sagemaker/latest/dg/API_CreatePresignedNotebookInstanceUrl.html) the Notebook Instance again and perform any validation specific to the script.\n\n```bash\naws sagemaker create-presigned-notebook-instance-url \\\n    --notebook-instance-name \"$RESOURCE_NAME\"\n```\n\nFile a Pull Request following the instructions in the [Contribution Guidelines](CONTRIBUTING.md).\n\n## License Summary\n\nThis sample code is made available under the MIT-0 license. See the LICENSE file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faws-samples%2Famazon-sagemaker-notebook-instance-lifecycle-config-samples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faws-samples%2Famazon-sagemaker-notebook-instance-lifecycle-config-samples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faws-samples%2Famazon-sagemaker-notebook-instance-lifecycle-config-samples/lists"}