<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tanmay Prashant Rane</title>
    <description>The latest articles on DEV Community by Tanmay Prashant Rane (@tanmay8266).</description>
    <link>https://dev.to/tanmay8266</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F927988%2Fd032219e-089d-46d3-89e4-da679694f005.png</url>
      <title>DEV Community: Tanmay Prashant Rane</title>
      <link>https://dev.to/tanmay8266</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tanmay8266"/>
    <language>en</language>
    <item>
      <title>SageMaker Lifecycle Configurations for custom environment</title>
      <dc:creator>Tanmay Prashant Rane</dc:creator>
      <pubDate>Sun, 02 Oct 2022 11:33:38 +0000</pubDate>
      <link>https://dev.to/aws-builders/sagemaker-lifecycle-configurations-for-custom-environments-51k6</link>
      <guid>https://dev.to/aws-builders/sagemaker-lifecycle-configurations-for-custom-environments-51k6</guid>
      <description>&lt;h2&gt;
  
  
  Preface
&lt;/h2&gt;

&lt;p&gt;Amazon SageMaker is great for us data scientists and machine learning engineers for exploring data, building models. The range of preinstalled packages is great and enough for normal scenarios. But for having new and specific versions of packages "pip" or "conda" managers can be used. &lt;/p&gt;

&lt;h2&gt;
  
  
  What this blog will give you?
&lt;/h2&gt;

&lt;p&gt;A template to bend and flex according to your requirements and get lifecycle configurations done fast. 🚀&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem 🧠
&lt;/h2&gt;

&lt;p&gt;The sole issue is every time I turn the notebook instance off, the libraries installed manually are lost. This becomes a routine every time you start instance. I would very much like to have my environment and libraries setup as I start instance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preferences 🧑‍💻
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;I like to get my new environment started with fresh python installation using conda. Here's a one liner for that:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;conda create -n &amp;lt;env_name&amp;gt; python=&amp;lt;python version&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;conda create -n dev_env python=3.9&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Then we can activate the environment as:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;conda activate &amp;lt;env_name&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;conda activate dev_env&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;After getting into environment I like to install packages using the pip manager.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;pip install &amp;lt;package name&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install pyarrow&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's put this in a lifecycle configuration shell script shall we! 📜
&lt;/h2&gt;

&lt;p&gt;In the following script we will take care of following tasks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Creation of environment&lt;/strong&gt; with desired version of python. In this case python 3.9&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Installation of required libraries&lt;/strong&gt; with pip manager. In this case pandas, pyarrow and TensorFlow.&lt;/li&gt;
&lt;li&gt;Make newly created environment &lt;strong&gt;accessible for notebooks&lt;/strong&gt;.
Please read the comments in the script.&lt;/li&gt;
&lt;/ol&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&lt;h1&gt;
  
  
  !/bin/bash
&lt;/h1&gt;

&lt;p&gt;set -e&lt;/p&gt;
&lt;h1&gt;
  
  
  use ec2-user for operations
&lt;/h1&gt;

&lt;p&gt;sudo -u ec2-user -i &amp;lt;&amp;lt;'EOF'&lt;/p&gt;
&lt;h1&gt;
  
  
  environment creation
&lt;/h1&gt;

&lt;p&gt;conda create -n dev_env python=3.9 -y&lt;br&gt;
source activate dev_env&lt;/p&gt;
&lt;h1&gt;
  
  
  library installation
&lt;/h1&gt;

&lt;p&gt;pip install pandas&lt;br&gt;
pip install pyarrow&lt;br&gt;
pip install tensorflow==2.9&lt;/p&gt;
&lt;h1&gt;
  
  
  make new environment accessible
&lt;/h1&gt;

&lt;p&gt;conda install ipykernel -y&lt;/p&gt;

&lt;p&gt;source deactivate&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Script placement 🪄&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Locate lifecycle configurations in console&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn7v5w4zwkatgk2v9jkt4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn7v5w4zwkatgk2v9jkt4.png" alt="Lifecycle configuration in SageMaker console"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Create lifecycle configuration to run on notebook start&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff8w3lzj1iuv7azlyjjq9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff8w3lzj1iuv7azlyjjq9.png" alt="Lifecycle configuration creation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Add lifecycle configuration to notebook instance configurations&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fru1g07dub8051xgl211j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fru1g07dub8051xgl211j.png" alt="SageMaker notebook instance lifecycle configuration"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Results 💡
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Kernel Selection!&lt;/strong&gt;&lt;br&gt;
See the kernel listed in the dropdown 👀 &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F75h3gja50vkq7akfbf9v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F75h3gja50vkq7akfbf9v.png" alt="Kernel dropdown check"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Couple of checks on terminal&lt;/strong&gt;&lt;br&gt;
And we have what we need!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa6wxssjmkzl08mpjsarn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa6wxssjmkzl08mpjsarn.png" alt="Terminal environment check"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There we have it 🪄, let me know if it helped anyone. If anyone knows better method please comment and let me know! This is only one example of endless applications of lifecycle configurations. &lt;strong&gt;You can access them here:&lt;/strong&gt; &lt;a href="https://github.com/aws-samples/amazon-sagemaker-notebook-instance-lifecycle-config-samples/tree/master/scripts" rel="noopener noreferrer"&gt;SageMaker Lifecycle Configuration scripts&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>datascience</category>
      <category>python</category>
      <category>machinelearning</category>
    </item>
  </channel>
</rss>
